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 /* TODO -- we should make this event always immediately follow the
35 CREATE event, so we don't get redundant traces... */
37 /* Add dummy "start" action, just to create a first clock vector */
38 model->switch_to_master(new ModelAction(THREAD_START, memory_order_seq_cst, curr_thread));
40 /* Call the actual thread function */
41 curr_thread->start_routine(curr_thread->arg);
44 int Thread::create_context()
48 ret = getcontext(&context);
52 /* Initialize new managed context */
53 stack = stack_allocate(STACK_SIZE);
54 context.uc_stack.ss_sp = stack;
55 context.uc_stack.ss_size = STACK_SIZE;
56 context.uc_stack.ss_flags = 0;
57 context.uc_link = model->get_system_context();
58 makecontext(&context, thread_startup, 0);
63 int Thread::swap(Thread *t, ucontext_t *ctxt)
65 return swapcontext(&t->context, ctxt);
68 int Thread::swap(ucontext_t *ctxt, Thread *t)
70 return swapcontext(ctxt, &t->context);
73 void Thread::complete()
75 if (state != THREAD_COMPLETED) {
76 DEBUG("completed thread %d\n", get_id());
77 state = THREAD_COMPLETED;
83 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
87 state(THREAD_CREATED),
88 last_action_val(VALUE_NONE)
92 /* Initialize state */
93 ret = create_context();
95 printf("Error in create_context\n");
97 id = model->get_next_id();
99 parent = thread_current();
105 model->remove_thread(this);
108 thread_id_t Thread::get_id()