56942f5f7d277ac12c00dc17ba4bc9ff3e31aa75
[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 void Thread::setContext() {
90         set_state(THREAD_RUNNING);
91         setcontext(&context);
92 }
93
94 /**
95  * Swaps the current context to another thread of execution. This form switches
96  * from a user Thread to a system context.
97  * @param t Thread representing the currently-running thread. The current
98  * context is saved here.
99  * @param ctxt Context to which we will swap. Must hold a valid system context.
100  * @return Does not return, unless we return to Thread t's context. See
101  * swapcontext(3) (returns 0 for success, -1 for failure).
102  */
103 int Thread::swap(Thread *t, ucontext_t *ctxt)
104 {
105         t->set_state(THREAD_READY);
106         return model_swapcontext(&t->context, ctxt);
107 }
108
109 /**
110  * Swaps the current context to another thread of execution. This form switches
111  * from a system context to a user Thread.
112  * @param ctxt System context variable to which to save the current context.
113  * @param t Thread to which we will swap. Must hold a valid user context.
114  * @return Does not return, unless we return to the system context (ctxt). See
115  * swapcontext(3) (returns 0 for success, -1 for failure).
116  */
117 int Thread::swap(ucontext_t *ctxt, Thread *t)
118 {
119         t->set_state(THREAD_RUNNING);
120         return model_swapcontext(ctxt, &t->context);
121 }
122
123
124 /** Terminate a thread and free its stack. */
125 void Thread::complete()
126 {
127         ASSERT(!is_complete());
128         DEBUG("completed thread %d\n", id_to_int(get_id()));
129         state = THREAD_COMPLETED;
130         if (stack)
131                 stack_free(stack);
132 }
133
134 /**
135  * @brief Construct a new model-checker Thread
136  *
137  * A model-checker Thread is used for accounting purposes only. It will never
138  * have its own stack, and it should never be inserted into the Scheduler.
139  *
140  * @param tid The thread ID to assign
141  */
142 Thread::Thread(thread_id_t tid) :
143         parent(NULL),
144         creation(NULL),
145         pending(NULL),
146         start_routine(NULL),
147         arg(NULL),
148         stack(NULL),
149         user_thread(NULL),
150         id(tid),
151         state(THREAD_READY),    /* Thread is always ready? */
152         last_action_val(0),
153         model_thread(true)
154 {
155         memset(&context, 0, sizeof(context));
156 }
157
158 /**
159  * Construct a new thread.
160  * @param t The thread identifier of the newly created thread.
161  * @param func The function that the thread will call.
162  * @param a The parameter to pass to this function.
163  */
164 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
165         parent(parent),
166         creation(NULL),
167         pending(NULL),
168         start_routine(func),
169         pstart_routine(NULL),
170         arg(a),
171         user_thread(t),
172         id(tid),
173         state(THREAD_CREATED),
174         last_action_val(VALUE_NONE),
175         model_thread(false)
176 {
177         int ret;
178
179         /* Initialize state */
180         ret = create_context();
181         if (ret)
182                 model_print("Error in create_context\n");
183
184         user_thread->priv = this;       // WL
185 }
186
187 /**
188  * Construct a new thread for pthread.
189  * @param t The thread identifier of the newly created thread.
190  * @param func The function that the thread will call.
191  * @param a The parameter to pass to this function.
192  */
193 Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
194         parent(parent),
195         creation(NULL),
196         pending(NULL),
197         start_routine(NULL),
198         pstart_routine(func),
199         arg(a),
200         user_thread(t),
201         id(tid),
202         state(THREAD_CREATED),
203         last_action_val(VALUE_NONE),
204         model_thread(false)
205 {
206         int ret;
207
208         /* Initialize state */
209         ret = create_context();
210         if (ret)
211                 model_print("Error in create_context\n");
212 }
213
214
215 /** Destructor */
216 Thread::~Thread()
217 {
218         if (!is_complete())
219                 complete();
220 }
221
222 /** @return The thread_id_t corresponding to this Thread object. */
223 thread_id_t Thread::get_id() const
224 {
225         return id;
226 }
227
228 /**
229  * Set a thread's THREAD_* state (@see thread_state)
230  * @param s The state to enter
231  */
232 void Thread::set_state(thread_state s)
233 {
234         ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
235         state = s;
236 }
237
238 /**
239  * Get the Thread that this Thread is immediately waiting on
240  * @return The thread we are waiting on, if any; otherwise NULL
241  */
242 Thread * Thread::waiting_on() const
243 {
244         if (!pending)
245                 return NULL;
246
247         if (pending->get_type() == THREAD_JOIN)
248                 return pending->get_thread_operand();
249         else if (pending->get_type() == PTHREAD_JOIN)
250                 return pending->get_thread_operand();
251         else if (pending->is_lock())
252                 return (Thread *)pending->get_mutex()->get_state()->locked;
253         return NULL;
254 }
255
256 /**
257  * Check if this Thread is waiting (blocking) on a given Thread, directly or
258  * indirectly (via a chain of waiting threads)
259  *
260  * @param t The Thread on which we may be waiting
261  * @return True if we are waiting on Thread t; false otherwise
262  */
263 bool Thread::is_waiting_on(const Thread *t) const
264 {
265         Thread *wait;
266         for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())
267                 if (wait == t)
268                         return true;
269         return false;
270 }