2 * @brief Thread functions.
10 #include "threads-model.h"
13 /* global "model" object */
15 #include "execution.h"
20 uintptr_t get_tls_addr() {
22 asm ("mov %%fs:0, %0" : "=r" (addr));
26 #include <asm/prctl.h>
27 #include <sys/prctl.h>
29 int arch_prctl(int code, unsigned long addr);
31 static void set_tls_addr(uintptr_t addr) {
32 arch_prctl(ARCH_SET_FS, addr);
33 asm ("mov %0, %%fs:0" : : "r" (addr) : "memory");
37 /** Allocate a stack for a new thread. */
38 static void * stack_allocate(size_t size)
40 return Thread_malloc(size);
43 /** Free a stack for a terminated thread. */
44 static void stack_free(void *stack)
50 * @brief Get the current Thread
52 * Must be called from a user context
54 * @return The currently executing thread
56 Thread * thread_current(void)
59 return model->get_current_thread();
64 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, thread_current()));
68 void initMainThread() {
70 Thread * curr_thread = thread_current();
71 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
75 * Provides a startup wrapper for each thread, allowing some initial
76 * model-checking data to be recorded. This method also gets around makecontext
77 * not being 64-bit clean
81 Thread * curr_thread = thread_current();
83 /* Add dummy "start" action, just to create a first clock vector */
84 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
87 /* Call the actual thread function */
88 if (curr_thread->start_routine != NULL) {
89 curr_thread->start_routine(curr_thread->arg);
90 } else if (curr_thread->pstart_routine != NULL) {
91 // set pthread return value
92 void *retval = curr_thread->pstart_routine(curr_thread->arg);
93 curr_thread->set_pthread_return(retval);
96 /* Finish thread properly */
97 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
101 static int (*pthread_mutex_init_p)(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr) = NULL;
103 int real_pthread_mutex_init(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr) {
104 return pthread_mutex_init_p(__mutex, __mutexattr);
107 static int (*pthread_mutex_lock_p) (pthread_mutex_t *__mutex) = NULL;
109 int real_pthread_mutex_lock (pthread_mutex_t *__mutex) {
110 return pthread_mutex_lock_p(__mutex);
113 static int (*pthread_mutex_unlock_p) (pthread_mutex_t *__mutex) = NULL;
115 int real_pthread_mutex_unlock (pthread_mutex_t *__mutex) {
116 return pthread_mutex_unlock_p(__mutex);
119 static int (*pthread_create_p) (pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void * __restrict) = NULL;
121 int real_pthread_create (pthread_t *__restrict __newthread, const pthread_attr_t *__restrict __attr, void *(*__start_routine)(void *), void *__restrict __arg) {
122 return pthread_create_p(__newthread, __attr, __start_routine, __arg);
125 static int (*pthread_join_p) (pthread_t __th, void ** __thread_return) = NULL;
127 int real_pthread_join (pthread_t __th, void ** __thread_return) {
128 return pthread_join_p(__th, __thread_return);
131 static void (*pthread_exit_p)(void *) __attribute__((noreturn))= NULL;
133 void real_pthread_exit (void * value_ptr) {
134 pthread_exit_p(value_ptr);
137 void real_init_all() {
139 if (!pthread_mutex_init_p) {
140 pthread_mutex_init_p = (int (*)(pthread_mutex_t *__mutex, const pthread_mutexattr_t *__mutexattr))dlsym(RTLD_NEXT, "pthread_mutex_init");
141 if ((error = dlerror()) != NULL) {
142 fputs(error, stderr);
146 if (!pthread_mutex_lock_p) {
147 pthread_mutex_lock_p = (int (*)(pthread_mutex_t *__mutex))dlsym(RTLD_NEXT, "pthread_mutex_lock");
148 if ((error = dlerror()) != NULL) {
149 fputs(error, stderr);
153 if (!pthread_mutex_unlock_p) {
154 pthread_mutex_unlock_p = (int (*)(pthread_mutex_t *__mutex))dlsym(RTLD_NEXT, "pthread_mutex_unlock");
155 if ((error = dlerror()) != NULL) {
156 fputs(error, stderr);
160 if (!pthread_create_p) {
161 pthread_create_p = (int (*)(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict))dlsym(RTLD_NEXT, "pthread_create");
162 if ((error = dlerror()) != NULL) {
163 fputs(error, stderr);
167 if (!pthread_join_p) {
168 pthread_join_p = (int (*)(pthread_t __th, void ** __thread_return))dlsym(RTLD_NEXT, "pthread_join");
169 if ((error = dlerror()) != NULL) {
170 fputs(error, stderr);
175 if (!pthread_exit_p) {
176 *((void (**)(void *)) &pthread_exit_p) = (void (*)(void *))dlsym(RTLD_NEXT, "pthread_exit");
177 if ((error = dlerror()) != NULL) {
178 fputs(error, stderr);
185 void finalize_helper_thread() {
186 Thread * curr_thread = thread_current();
187 real_pthread_mutex_lock(&curr_thread->mutex);
188 curr_thread->tls = (char *) get_tls_addr();
189 real_pthread_mutex_unlock(&curr_thread->mutex);
190 //Wait in the kernel until it is time for us to finish
191 real_pthread_mutex_lock(&curr_thread->mutex2);
192 real_pthread_mutex_unlock(&curr_thread->mutex2);
193 //return to helper thread function
194 setcontext(&curr_thread->context);
197 void * helper_thread(void * ptr) {
198 Thread * curr_thread = thread_current();
200 //build a context for this real thread so we can take it's context
201 int ret = getcontext(&curr_thread->helpercontext);
205 if (pthread_setspecific(model->get_execution()->getPthreadKey(), (const void *)4)) {
206 printf("Destructor setup failed\n");
211 /* Initialize new managed context */
212 curr_thread->helper_stack = stack_allocate(STACK_SIZE);
213 curr_thread->helpercontext.uc_stack.ss_sp = curr_thread->helper_stack;
214 curr_thread->helpercontext.uc_stack.ss_size = STACK_SIZE;
215 curr_thread->helpercontext.uc_stack.ss_flags = 0;
216 curr_thread->helpercontext.uc_link = model->get_system_context();
217 makecontext(&curr_thread->helpercontext, finalize_helper_thread, 0);
219 model_swapcontext(&curr_thread->context, &curr_thread->helpercontext);
222 //start the real thread
229 void tlsdestructor(void *v) {
230 uintptr_t count = (uintptr_t) v;
232 if (pthread_setspecific(model->get_execution()->getPthreadKey(), (const void *)(count - 1))) {
233 printf("Destructor setup failed\n");
238 /* Finish thread properly */
239 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, thread_current()));
243 void setup_context() {
244 Thread * curr_thread = thread_current();
246 /* Add dummy "start" action, just to create a first clock vector */
247 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
251 /* Initialize our lock */
252 real_pthread_mutex_init(&curr_thread->mutex, NULL);
253 real_pthread_mutex_init(&curr_thread->mutex2, NULL);
254 real_pthread_mutex_lock(&curr_thread->mutex2);
256 /* Create the real thread */
257 real_pthread_create(&curr_thread->thread, NULL, helper_thread, NULL);
260 real_pthread_mutex_lock(&curr_thread->mutex);
261 if (curr_thread->tls != NULL)
263 real_pthread_mutex_unlock(&curr_thread->mutex);
266 set_tls_addr((uintptr_t)curr_thread->tls);
267 setcontext(&curr_thread->context);
272 * Create a thread context for a new thread so we can use
273 * setcontext/getcontext/swapcontext to swap it out.
274 * @return 0 on success; otherwise, non-zero error condition
276 int Thread::create_context()
280 ret = getcontext(&context);
284 /* Initialize new managed context */
285 stack = stack_allocate(STACK_SIZE);
286 context.uc_stack.ss_sp = stack;
287 context.uc_stack.ss_size = STACK_SIZE;
288 context.uc_stack.ss_flags = 0;
289 context.uc_link = model->get_system_context();
291 makecontext(&context, setup_context, 0);
293 makecontext(&context, thread_startup, 0);
300 * Swaps the current context to another thread of execution. This form switches
301 * from a user Thread to a system context.
302 * @param t Thread representing the currently-running thread. The current
303 * context is saved here.
304 * @param ctxt Context to which we will swap. Must hold a valid system context.
305 * @return Does not return, unless we return to Thread t's context. See
306 * swapcontext(3) (returns 0 for success, -1 for failure).
308 int Thread::swap(Thread *t, ucontext_t *ctxt)
310 t->set_state(THREAD_READY);
312 set_tls_addr((uintptr_t)model->getInitThread()->tls);
314 return model_swapcontext(&t->context, ctxt);
318 * Swaps the current context to another thread of execution. This form switches
319 * from a system context to a user Thread.
320 * @param ctxt System context variable to which to save the current context.
321 * @param t Thread to which we will swap. Must hold a valid user context.
322 * @return Does not return, unless we return to the system context (ctxt). See
323 * swapcontext(3) (returns 0 for success, -1 for failure).
325 int Thread::swap(ucontext_t *ctxt, Thread *t)
327 t->set_state(THREAD_RUNNING);
330 set_tls_addr((uintptr_t)t->tls);
332 return model_swapcontext(ctxt, &t->context);
336 /** Terminate a thread and free its stack. */
337 void Thread::complete()
339 ASSERT(!is_complete());
340 DEBUG("completed thread %d\n", id_to_int(get_id()));
341 state = THREAD_COMPLETED;
345 if (this != model->getInitThread()) {
346 ASSERT(thread_current()==NULL);
347 real_pthread_mutex_unlock(&mutex2);
348 real_pthread_join(thread, NULL);
349 stack_free(helper_stack);
355 * @brief Construct a new model-checker Thread
357 * A model-checker Thread is used for accounting purposes only. It will never
358 * have its own stack, and it should never be inserted into the Scheduler.
360 * @param tid The thread ID to assign
362 Thread::Thread(thread_id_t tid) :
374 state(THREAD_READY), /* Thread is always ready? */
378 memset(&context, 0, sizeof(context));
382 * Construct a new thread.
383 * @param t The thread identifier of the newly created thread.
384 * @param func The function that the thread will call.
385 * @param a The parameter to pass to this function.
387 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
392 pstart_routine(NULL),
399 state(THREAD_CREATED),
400 last_action_val(VALUE_NONE),
405 /* Initialize state */
406 ret = create_context();
408 model_print("Error in create_context\n");
410 user_thread->priv = this; // WL
414 * Construct a new thread for pthread.
415 * @param t The thread identifier of the newly created thread.
416 * @param func The function that the thread will call.
417 * @param a The parameter to pass to this function.
419 Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
424 pstart_routine(func),
431 state(THREAD_CREATED),
432 last_action_val(VALUE_NONE),
437 /* Initialize state */
438 ret = create_context();
440 model_print("Error in create_context\n");
451 /** @return The thread_id_t corresponding to this Thread object. */
452 thread_id_t Thread::get_id() const
458 * Set a thread's THREAD_* state (@see thread_state)
459 * @param s The state to enter
461 void Thread::set_state(thread_state s)
463 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
468 * Get the Thread that this Thread is immediately waiting on
469 * @return The thread we are waiting on, if any; otherwise NULL
471 Thread * Thread::waiting_on() const
476 if (pending->get_type() == THREAD_JOIN)
477 return pending->get_thread_operand();
478 else if (pending->get_type() == PTHREAD_JOIN)
479 return pending->get_thread_operand();
480 else if (pending->is_lock())
481 return (Thread *)pending->get_mutex()->get_state()->locked;
486 * Check if this Thread is waiting (blocking) on a given Thread, directly or
487 * indirectly (via a chain of waiting threads)
489 * @param t The Thread on which we may be waiting
490 * @return True if we are waiting on Thread t; false otherwise
492 bool Thread::is_waiting_on(const Thread *t) const
495 for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())