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