2 * @brief Thread functions.
10 #include "threads-model.h"
13 /* global "model" object */
15 #include "execution.h"
19 uintptr_t get_tls_addr() {
21 asm ("mov %%fs:0, %0" : "=r" (addr));
25 #include <asm/prctl.h>
26 #include <sys/prctl.h>
28 int arch_prctl(int code, unsigned long addr);
30 static void set_tls_addr(uintptr_t addr) {
31 arch_prctl(ARCH_SET_FS, addr);
32 asm ("mov %0, %%fs:0" : : "r" (addr) : "memory");
36 /** Allocate a stack for a new thread. */
37 static void * stack_allocate(size_t size)
39 return Thread_malloc(size);
42 /** Free a stack for a terminated thread. */
43 static void stack_free(void *stack)
49 * @brief Get the current Thread
51 * Must be called from a user context
53 * @return The currently executing thread
55 Thread * thread_current(void)
58 return model->get_current_thread();
61 void main_thread_startup() {
63 Thread * curr_thread = thread_current();
64 /* Add dummy "start" action, just to create a first clock vector */
65 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
71 * Provides a startup wrapper for each thread, allowing some initial
72 * model-checking data to be recorded. This method also gets around makecontext
73 * not being 64-bit clean
77 Thread * curr_thread = thread_current();
79 /* Add dummy "start" action, just to create a first clock vector */
80 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
83 /* Call the actual thread function */
84 if (curr_thread->start_routine != NULL) {
85 curr_thread->start_routine(curr_thread->arg);
86 } else if (curr_thread->pstart_routine != NULL) {
87 // set pthread return value
88 void *retval = curr_thread->pstart_routine(curr_thread->arg);
89 curr_thread->set_pthread_return(retval);
91 /* Finish thread properly */
92 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
96 int real_pthread_mutex_init(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr) {
97 static int (*pthread_mutex_init_p) (pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr) = NULL;
99 if (!pthread_mutex_init_p) {
100 pthread_mutex_init_p = (int (*)(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr))dlsym(RTLD_NEXT, "pthread_mutex_init");
101 if ((error = dlerror()) != NULL) {
102 fputs(error, stderr);
106 return pthread_mutex_init_p(__mutex, __mutexattr);
109 int real_pthread_mutex_lock (pthread_mutex_t *__mutex) {
110 static int (*pthread_mutex_lock_p) (pthread_mutex_t *__mutex) = NULL;
112 if (!pthread_mutex_lock_p) {
113 pthread_mutex_lock_p = (int (*)(pthread_mutex_t *__mutex))dlsym(RTLD_NEXT, "pthread_mutex_lock");
114 if ((error = dlerror()) != NULL) {
115 fputs(error, stderr);
119 return pthread_mutex_lock_p(__mutex);
122 int real_pthread_mutex_unlock (pthread_mutex_t *__mutex) {
123 static int (*pthread_mutex_unlock_p) (pthread_mutex_t *__mutex) = NULL;
125 if (!pthread_mutex_unlock_p) {
126 pthread_mutex_unlock_p = (int (*)(pthread_mutex_t *__mutex))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
127 if ((error = dlerror()) != NULL) {
128 fputs(error, stderr);
132 return pthread_mutex_unlock_p(__mutex);
135 int real_pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine)(void *), void *__restrict __arg) {
136 static int (*pthread_create_p) (pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void * __restrict) = NULL;
138 if (!pthread_create_p) {
139 pthread_create_p = (int (*)(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict))dlsym(RTLD_NEXT, "pthread_create");
140 if ((error = dlerror()) != NULL) {
141 fputs(error, stderr);
145 return pthread_create_p(__newthread, __attr, __start_routine, __arg);
148 int real_pthread_join (pthread_t __th, void ** __thread_return) {
149 static int (*pthread_join_p) (pthread_t __th, void ** __thread_return) = NULL;
151 if (!pthread_join_p) {
152 pthread_join_p = (int (*)(pthread_t __th, void ** __thread_return))dlsym(RTLD_NEXT, "pthread_join");
153 if ((error = dlerror()) != NULL) {
154 fputs(error, stderr);
158 return pthread_join_p(__th, __thread_return);
161 void finalize_helper_thread() {
162 Thread * curr_thread = thread_current();
163 real_pthread_mutex_lock(&curr_thread->mutex);
164 curr_thread->tls = (char *) get_tls_addr();
165 real_pthread_mutex_unlock(&curr_thread->mutex);
166 //Wait in the kernel until it is time for us to finish
167 real_pthread_mutex_lock(&curr_thread->mutex2);
168 real_pthread_mutex_unlock(&curr_thread->mutex2);
169 //return to helper thread function
170 setcontext(&curr_thread->context);
173 void * helper_thread(void * ptr) {
174 Thread * curr_thread = thread_current();
176 //build a context for this real thread so we can take it's context
177 int ret = getcontext(&curr_thread->helpercontext);
180 /* Initialize new managed context */
181 void *helperstack = stack_allocate(STACK_SIZE);
182 curr_thread->helpercontext.uc_stack.ss_sp = helperstack;
183 curr_thread->helpercontext.uc_stack.ss_size = STACK_SIZE;
184 curr_thread->helpercontext.uc_stack.ss_flags = 0;
185 curr_thread->helpercontext.uc_link = model->get_system_context();
186 makecontext(&curr_thread->helpercontext, finalize_helper_thread, 0);
188 model_swapcontext(&curr_thread->context, &curr_thread->helpercontext);
190 //start the real thread
193 //now the real thread has control again
194 stack_free(helperstack);
199 void setup_context() {
200 Thread * curr_thread = thread_current();
202 /* Add dummy "start" action, just to create a first clock vector */
203 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
205 /* Initialize our lock */
206 real_pthread_mutex_init(&curr_thread->mutex, NULL);
207 real_pthread_mutex_init(&curr_thread->mutex2, NULL);
208 real_pthread_mutex_lock(&curr_thread->mutex2);
210 /* Create the real thread */
211 real_pthread_create(&curr_thread->thread, NULL, helper_thread, NULL);
214 real_pthread_mutex_lock(&curr_thread->mutex);
215 if (curr_thread->tls != NULL)
217 real_pthread_mutex_unlock(&curr_thread->mutex);
220 set_tls_addr((uintptr_t)curr_thread->tls);
221 setcontext(&curr_thread->context);
226 * Create a thread context for a new thread so we can use
227 * setcontext/getcontext/swapcontext to swap it out.
228 * @return 0 on success; otherwise, non-zero error condition
230 int Thread::create_context()
234 ret = getcontext(&context);
238 /* Initialize new managed context */
239 stack = stack_allocate(STACK_SIZE);
240 context.uc_stack.ss_sp = stack;
241 context.uc_stack.ss_size = STACK_SIZE;
242 context.uc_stack.ss_flags = 0;
243 context.uc_link = model->get_system_context();
246 makecontext(&context, setup_context, 0);
248 makecontext(&context, main_thread_startup, 0);
250 makecontext(&context, thread_startup, 0);
257 * Swaps the current context to another thread of execution. This form switches
258 * from a user Thread to a system context.
259 * @param t Thread representing the currently-running thread. The current
260 * context is saved here.
261 * @param ctxt Context to which we will swap. Must hold a valid system context.
262 * @return Does not return, unless we return to Thread t's context. See
263 * swapcontext(3) (returns 0 for success, -1 for failure).
265 int Thread::swap(Thread *t, ucontext_t *ctxt)
267 t->set_state(THREAD_READY);
269 set_tls_addr((uintptr_t)model->getInitThread()->tls);
271 return model_swapcontext(&t->context, ctxt);
275 * Swaps the current context to another thread of execution. This form switches
276 * from a system context to a user Thread.
277 * @param ctxt System context variable to which to save the current context.
278 * @param t Thread to which we will swap. Must hold a valid user context.
279 * @return Does not return, unless we return to the system context (ctxt). See
280 * swapcontext(3) (returns 0 for success, -1 for failure).
282 int Thread::swap(ucontext_t *ctxt, Thread *t)
284 t->set_state(THREAD_RUNNING);
287 set_tls_addr((uintptr_t)t->tls);
289 return model_swapcontext(ctxt, &t->context);
293 /** Terminate a thread and free its stack. */
294 void Thread::complete()
296 ASSERT(!is_complete());
297 DEBUG("completed thread %d\n", id_to_int(get_id()));
298 state = THREAD_COMPLETED;
302 if (this != model->getInitThread()) {
304 real_pthread_mutex_unlock(&mutex2);
305 real_pthread_join(thread, NULL);
312 * @brief Construct a new model-checker Thread
314 * A model-checker Thread is used for accounting purposes only. It will never
315 * have its own stack, and it should never be inserted into the Scheduler.
317 * @param tid The thread ID to assign
319 Thread::Thread(thread_id_t tid) :
331 state(THREAD_READY), /* Thread is always ready? */
335 memset(&context, 0, sizeof(context));
339 * Construct a new thread.
340 * @param t The thread identifier of the newly created thread.
341 * @param func The function that the thread will call.
342 * @param a The parameter to pass to this function.
344 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
349 pstart_routine(NULL),
356 state(THREAD_CREATED),
357 last_action_val(VALUE_NONE),
362 /* Initialize state */
363 ret = create_context();
365 model_print("Error in create_context\n");
367 user_thread->priv = this; // WL
371 * Construct a new thread for pthread.
372 * @param t The thread identifier of the newly created thread.
373 * @param func The function that the thread will call.
374 * @param a The parameter to pass to this function.
376 Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
381 pstart_routine(func),
388 state(THREAD_CREATED),
389 last_action_val(VALUE_NONE),
394 /* Initialize state */
395 ret = create_context();
397 model_print("Error in create_context\n");
408 /** @return The thread_id_t corresponding to this Thread object. */
409 thread_id_t Thread::get_id() const
415 * Set a thread's THREAD_* state (@see thread_state)
416 * @param s The state to enter
418 void Thread::set_state(thread_state s)
420 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
425 * Get the Thread that this Thread is immediately waiting on
426 * @return The thread we are waiting on, if any; otherwise NULL
428 Thread * Thread::waiting_on() const
433 if (pending->get_type() == THREAD_JOIN)
434 return pending->get_thread_operand();
435 else if (pending->get_type() == PTHREAD_JOIN)
436 return pending->get_thread_operand();
437 else if (pending->is_lock())
438 return (Thread *)pending->get_mutex()->get_state()->locked;
443 * Check if this Thread is waiting (blocking) on a given Thread, directly or
444 * indirectly (via a chain of waiting threads)
446 * @param t The Thread on which we may be waiting
447 * @return True if we are waiting on Thread t; false otherwise
449 bool Thread::is_waiting_on(const Thread *t) const
452 for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())