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