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();
26 /* This method just gets around makecontext not being 64-bit clean */
28 void thread_startup() {
29 Thread * curr_thread = thread_current();
30 curr_thread->start_routine(curr_thread->arg);
33 int Thread::create_context()
37 ret = getcontext(&context);
41 /* Initialize new managed context */
42 stack = stack_allocate(STACK_SIZE);
43 context.uc_stack.ss_sp = stack;
44 context.uc_stack.ss_size = STACK_SIZE;
45 context.uc_stack.ss_flags = 0;
46 context.uc_link = model->get_system_context();
47 makecontext(&context, thread_startup, 0);
52 int Thread::swap(Thread *t, ucontext_t *ctxt)
54 return swapcontext(&t->context, ctxt);
57 int Thread::swap(ucontext_t *ctxt, Thread *t)
59 return swapcontext(ctxt, &t->context);
62 void Thread::complete()
64 if (state != THREAD_COMPLETED) {
65 DEBUG("completed thread %d\n", get_id());
66 state = THREAD_COMPLETED;
72 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
73 last_action_val(VALUE_NONE)
81 /* Initialize state */
82 ret = create_context();
84 printf("Error in create_context\n");
86 state = THREAD_CREATED;
87 id = model->get_next_id();
89 parent = thread_current();
95 model->remove_thread(this);
98 thread_id_t Thread::get_id()