X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=threads.cc;h=e0cb277563bb057c989be29379866ac8f090094b;hb=8c8e990efa97568549542133e13d9f4df375dd15;hp=4c7ac41f1849d2a18fa6ad4510df118f2d81f87d;hpb=a9aac0d43e0e24354af4163cd3161036586f029a;p=model-checker.git diff --git a/threads.cc b/threads.cc index 4c7ac41..e0cb277 100644 --- a/threads.cc +++ b/threads.cc @@ -12,12 +12,12 @@ static void * stack_allocate(size_t size) { - return malloc(size); + return userMalloc(size); } static void stack_free(void *stack) { - free(stack); + userFree(stack); } Thread * thread_current(void) @@ -33,31 +33,43 @@ int Thread::create_context() if (ret) return ret; - /* start_routine == NULL means this is our initial context */ - if (!start_routine) - return 0; - /* Initialize new managed context */ stack = stack_allocate(STACK_SIZE); context.uc_stack.ss_sp = stack; context.uc_stack.ss_size = STACK_SIZE; context.uc_stack.ss_flags = 0; - context.uc_link = &model->system_thread->context; + context.uc_link = model->get_system_context(); makecontext(&context, start_routine, 1, arg); return 0; } -int Thread::swap(Thread *t) +int Thread::swap(Thread *t, ucontext_t *ctxt) { - return swapcontext(&this->context, &t->context); + return swapcontext(&t->context, ctxt); } -void Thread::dispose() +int Thread::swap(ucontext_t *ctxt, Thread *t) { - DEBUG("completed thread %d\n", thread_current()->get_id()); - state = THREAD_COMPLETED; - stack_free(stack); + return swapcontext(ctxt, &t->context); +} + +void Thread::complete() +{ + if (state != THREAD_COMPLETED) { + DEBUG("completed thread %d\n", get_id()); + state = THREAD_COMPLETED; + if (stack) + stack_free(stack); + } +} + +void * Thread::operator new(size_t size) { + return userMalloc(size); +} + +void Thread::operator delete(void *ptr) { + userFree(ptr); } Thread::Thread(thrd_t *t, void (*func)(), void *a) { @@ -75,20 +87,12 @@ Thread::Thread(thrd_t *t, void (*func)(), void *a) { state = THREAD_CREATED; id = model->get_next_id(); *user_thread = id; - model->scheduler->add_thread(this); } -Thread::Thread(thrd_t *t) { - /* system thread */ - user_thread = t; - start_routine = NULL; - arg = NULL; - - state = THREAD_CREATED; - id = model->get_next_id(); - *user_thread = id; - create_context(); - model->add_system_thread(this); +Thread::~Thread() +{ + complete(); + model->remove_thread(this); } thread_id_t Thread::get_id() @@ -104,13 +108,13 @@ static int thread_system_next(void) Thread *curr, *next; curr = thread_current(); - model->check_current_action(); if (curr) { - if (curr->get_state() == THREAD_READY) + if (curr->get_state() == THREAD_READY) { + model->check_current_action(); model->scheduler->add_thread(curr); - else if (curr->get_state() == THREAD_RUNNING) + } else if (curr->get_state() == THREAD_RUNNING) /* Stopped while running; i.e., completed */ - curr->dispose(); + curr->complete(); else DEBUG("ERROR: current thread in unexpected state??\n"); } @@ -120,7 +124,7 @@ static int thread_system_next(void) DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1); if (!next) return 1; - return model->system_thread->swap(next); + return Thread::swap(model->get_system_context(), next); } static void thread_wait_finish(void) @@ -136,24 +140,24 @@ static void thread_wait_finish(void) */ int main() { - thrd_t user_thread, main_thread; - Thread *th; + thrd_t user_thread; + ucontext_t main_context; model = new ModelChecker(); - th = new Thread(&main_thread); + if (getcontext(&main_context)) + return 1; + + model->set_system_context(&main_context); do { /* Start user program */ - thrd_create(&user_thread, &user_main, NULL); + model->add_thread(new Thread(&user_thread, &user_main, NULL)); /* Wait for all threads to complete */ thread_wait_finish(); - - model->print_trace(); } while (model->next_execution()); - delete th; delete model; DEBUG("Exiting\n");