1 #include "libthreads.h"
5 /* global "model" object */
8 #define STACK_SIZE (1024 * 1024)
10 static void * stack_allocate(size_t size)
15 static void stack_free(void *stack)
20 Thread * thread_current(void)
23 return model->scheduler->get_current_thread();
27 * Provides a startup wrapper for each thread, allowing some initial
28 * model-checking data to be recorded. This method also gets around makecontext
29 * not being 64-bit clean
31 void thread_startup() {
32 Thread * curr_thread = thread_current();
34 /* Add dummy "start" action, just to create a first clock vector */
35 model->switch_to_master(new ModelAction(THREAD_START, memory_order_seq_cst, curr_thread));
37 /* Call the actual thread function */
38 curr_thread->start_routine(curr_thread->arg);
41 int Thread::create_context()
45 ret = getcontext(&context);
49 /* Initialize new managed context */
50 stack = stack_allocate(STACK_SIZE);
51 context.uc_stack.ss_sp = stack;
52 context.uc_stack.ss_size = STACK_SIZE;
53 context.uc_stack.ss_flags = 0;
54 context.uc_link = model->get_system_context();
55 makecontext(&context, thread_startup, 0);
60 int Thread::swap(Thread *t, ucontext_t *ctxt)
62 return swapcontext(&t->context, ctxt);
65 int Thread::swap(ucontext_t *ctxt, Thread *t)
67 return swapcontext(ctxt, &t->context);
70 void Thread::complete()
72 if (state != THREAD_COMPLETED) {
73 DEBUG("completed thread %d\n", get_id());
74 state = THREAD_COMPLETED;
80 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
84 state(THREAD_CREATED),
85 last_action_val(VALUE_NONE)
89 /* Initialize state */
90 ret = create_context();
92 printf("Error in create_context\n");
94 id = model->get_next_id();
96 parent = thread_current();
102 model->remove_thread(this);
105 thread_id_t Thread::get_id()