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)
22 return model->scheduler->get_current_thread();
25 /* This method just gets around makecontext not being 64-bit clean */
27 void thread_startup() {
28 Thread * curr_thread = thread_current();
29 curr_thread->start_routine(curr_thread->arg);
32 int Thread::create_context()
36 ret = getcontext(&context);
40 /* Initialize new managed context */
41 stack = stack_allocate(STACK_SIZE);
42 context.uc_stack.ss_sp = stack;
43 context.uc_stack.ss_size = STACK_SIZE;
44 context.uc_stack.ss_flags = 0;
45 context.uc_link = model->get_system_context();
46 makecontext(&context, thread_startup, 0);
51 int Thread::swap(Thread *t, ucontext_t *ctxt)
53 return swapcontext(&t->context, ctxt);
56 int Thread::swap(ucontext_t *ctxt, Thread *t)
58 return swapcontext(ctxt, &t->context);
61 void Thread::complete()
63 if (state != THREAD_COMPLETED) {
64 DEBUG("completed thread %d\n", get_id());
65 state = THREAD_COMPLETED;
71 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) {
78 /* Initialize state */
79 ret = create_context();
81 printf("Error in create_context\n");
83 state = THREAD_CREATED;
84 id = model->get_next_id();
86 parent = thread_current();
92 model->remove_thread(this);
95 thread_id_t Thread::get_id()