Merge branch 'new_fuzzer' of /home/git/random-fuzzer into branch-weiyu
[c11tester.git] / threads.cc
1 /** @file threads.cc
2  *  @brief Thread functions.
3  */
4
5 #include <string.h>
6
7 #include <threads.h>
8 #include "mutex.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "action.h"
12
13 /* global "model" object */
14 #include "model.h"
15
16 /** Allocate a stack for a new thread. */
17 static void * stack_allocate(size_t size)
18 {
19         return Thread_malloc(size);
20 }
21
22 /** Free a stack for a terminated thread. */
23 static void stack_free(void *stack)
24 {
25         Thread_free(stack);
26 }
27
28 /**
29  * @brief Get the current Thread
30  *
31  * Must be called from a user context
32  *
33  * @return The currently executing thread
34  */
35 Thread * thread_current(void)
36 {
37         ASSERT(model);
38         return model->get_current_thread();
39 }
40
41 /**
42  * Provides a startup wrapper for each thread, allowing some initial
43  * model-checking data to be recorded. This method also gets around makecontext
44  * not being 64-bit clean
45  */
46 void thread_startup()
47 {
48         Thread * curr_thread = thread_current();
49
50         /* Add dummy "start" action, just to create a first clock vector */
51         model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
52
53         /* Call the actual thread function */
54         if (curr_thread->start_routine != NULL) {
55                 curr_thread->start_routine(curr_thread->arg);
56         } else if (curr_thread->pstart_routine != NULL) {
57                 // set pthread return value
58                 void *retval = curr_thread->pstart_routine(curr_thread->arg);
59                 curr_thread->set_pthread_return(retval);
60         }
61         /* Finish thread properly */
62         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
63 }
64
65 /**
66  * Create a thread context for a new thread so we can use
67  * setcontext/getcontext/swapcontext to swap it out.
68  * @return 0 on success; otherwise, non-zero error condition
69  */
70 int Thread::create_context()
71 {
72         int ret;
73
74         ret = getcontext(&context);
75         if (ret)
76                 return ret;
77
78         /* Initialize new managed context */
79         stack = stack_allocate(STACK_SIZE);
80         context.uc_stack.ss_sp = stack;
81         context.uc_stack.ss_size = STACK_SIZE;
82         context.uc_stack.ss_flags = 0;
83         context.uc_link = model->get_system_context();
84         makecontext(&context, thread_startup, 0);
85
86         return 0;
87 }
88
89 /**
90  * Swaps the current context to another thread of execution. This form switches
91  * from a user Thread to a system context.
92  * @param t Thread representing the currently-running thread. The current
93  * context is saved here.
94  * @param ctxt Context to which we will swap. Must hold a valid system context.
95  * @return Does not return, unless we return to Thread t's context. See
96  * swapcontext(3) (returns 0 for success, -1 for failure).
97  */
98 int Thread::swap(Thread *t, ucontext_t *ctxt)
99 {
100         t->set_state(THREAD_READY);
101         return model_swapcontext(&t->context, ctxt);
102 }
103
104 /**
105  * Swaps the current context to another thread of execution. This form switches
106  * from a system context to a user Thread.
107  * @param ctxt System context variable to which to save the current context.
108  * @param t Thread to which we will swap. Must hold a valid user context.
109  * @return Does not return, unless we return to the system context (ctxt). See
110  * swapcontext(3) (returns 0 for success, -1 for failure).
111  */
112 int Thread::swap(ucontext_t *ctxt, Thread *t)
113 {
114         t->set_state(THREAD_RUNNING);
115         return model_swapcontext(ctxt, &t->context);
116 }
117
118
119 /** Terminate a thread and free its stack. */
120 void Thread::complete()
121 {
122         ASSERT(!is_complete());
123         DEBUG("completed thread %d\n", id_to_int(get_id()));
124         state = THREAD_COMPLETED;
125         if (stack)
126                 stack_free(stack);
127 }
128
129 /**
130  * @brief Construct a new model-checker Thread
131  *
132  * A model-checker Thread is used for accounting purposes only. It will never
133  * have its own stack, and it should never be inserted into the Scheduler.
134  *
135  * @param tid The thread ID to assign
136  */
137 Thread::Thread(thread_id_t tid) :
138         parent(NULL),
139         creation(NULL),
140         pending(NULL),
141         start_routine(NULL),
142         arg(NULL),
143         stack(NULL),
144         user_thread(NULL),
145         id(tid),
146         state(THREAD_READY),    /* Thread is always ready? */
147         last_action_val(0),
148         model_thread(true)
149 {
150         memset(&context, 0, sizeof(context));
151 }
152
153 /**
154  * Construct a new thread.
155  * @param t The thread identifier of the newly created thread.
156  * @param func The function that the thread will call.
157  * @param a The parameter to pass to this function.
158  */
159 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
160         parent(parent),
161         creation(NULL),
162         pending(NULL),
163         start_routine(func),
164         pstart_routine(NULL),
165         arg(a),
166         user_thread(t),
167         id(tid),
168         state(THREAD_CREATED),
169         last_action_val(VALUE_NONE),
170         model_thread(false)
171 {
172         int ret;
173
174         /* Initialize state */
175         ret = create_context();
176         if (ret)
177                 model_print("Error in create_context\n");
178
179         user_thread->priv = this;       // WL
180 }
181
182 /**
183  * Construct a new thread for pthread.
184  * @param t The thread identifier of the newly created thread.
185  * @param func The function that the thread will call.
186  * @param a The parameter to pass to this function.
187  */
188 Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
189         parent(parent),
190         creation(NULL),
191         pending(NULL),
192         start_routine(NULL),
193         pstart_routine(func),
194         arg(a),
195         user_thread(t),
196         id(tid),
197         state(THREAD_CREATED),
198         last_action_val(VALUE_NONE),
199         model_thread(false)
200 {
201         int ret;
202
203         /* Initialize state */
204         ret = create_context();
205         if (ret)
206                 model_print("Error in create_context\n");
207 }
208
209
210 /** Destructor */
211 Thread::~Thread()
212 {
213         if (!is_complete())
214                 complete();
215 }
216
217 /** @return The thread_id_t corresponding to this Thread object. */
218 thread_id_t Thread::get_id() const
219 {
220         return id;
221 }
222
223 /**
224  * Set a thread's THREAD_* state (@see thread_state)
225  * @param s The state to enter
226  */
227 void Thread::set_state(thread_state s)
228 {
229         ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
230         state = s;
231 }
232
233 /**
234  * Get the Thread that this Thread is immediately waiting on
235  * @return The thread we are waiting on, if any; otherwise NULL
236  */
237 Thread * Thread::waiting_on() const
238 {
239         if (!pending)
240                 return NULL;
241
242         if (pending->get_type() == THREAD_JOIN)
243                 return pending->get_thread_operand();
244         else if (pending->get_type() == PTHREAD_JOIN)
245                 return pending->get_thread_operand();
246         else if (pending->is_lock())
247                 return (Thread *)pending->get_mutex()->get_state()->locked;
248         return NULL;
249 }
250
251 /**
252  * Check if this Thread is waiting (blocking) on a given Thread, directly or
253  * indirectly (via a chain of waiting threads)
254  *
255  * @param t The Thread on which we may be waiting
256  * @return True if we are waiting on Thread t; false otherwise
257  */
258 bool Thread::is_waiting_on(const Thread *t) const
259 {
260         Thread *wait;
261         for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())
262                 if (wait == t)
263                         return true;
264         return false;
265 }