X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=threads.cc;h=a8f282a8b32efdb96d1d248570fc8b7574af92f7;hb=3e520de48b5be0b0d4fa4a5f033e207b17c4496d;hp=87de32fb2452f0384016353306b0b0b43c3d4cea;hpb=a330eb33b1b62dfa71f3f0cb055367a30090c180;p=model-checker.git diff --git a/threads.cc b/threads.cc index 87de32f..a8f282a 100644 --- a/threads.cc +++ b/threads.cc @@ -23,7 +23,13 @@ static void stack_free(void *stack) snapshot_free(stack); } -/** Return the currently executing thread. */ +/** + * @brief Get the current Thread + * + * Must be called from a user context + * + * @return The currently executing thread + */ Thread * thread_current(void) { ASSERT(model); @@ -84,6 +90,7 @@ int Thread::create_context() */ int Thread::swap(Thread *t, ucontext_t *ctxt) { + t->set_state(THREAD_READY); return swapcontext(&t->context, ctxt); } @@ -97,6 +104,7 @@ int Thread::swap(Thread *t, ucontext_t *ctxt) */ int Thread::swap(ucontext_t *ctxt, Thread *t) { + t->set_state(THREAD_RUNNING); return swapcontext(ctxt, &t->context); } @@ -104,12 +112,11 @@ int Thread::swap(ucontext_t *ctxt, Thread *t) /** Terminate a thread and free its stack. */ void Thread::complete() { - if (!is_complete()) { - DEBUG("completed thread %d\n", id_to_int(get_id())); - state = THREAD_COMPLETED; - if (stack) - stack_free(stack); - } + ASSERT(!is_complete()); + DEBUG("completed thread %d\n", id_to_int(get_id())); + state = THREAD_COMPLETED; + if (stack) + stack_free(stack); } /** @@ -162,14 +169,15 @@ Thread::Thread(thrd_t *t, void (*func)(void *), void *a) : model_print("Error in create_context\n"); id = model->get_next_id(); - *user_thread = id; + user_thread->priv = this; parent = thread_current(); } /** Destructor */ Thread::~Thread() { - complete(); + if (!is_complete()) + complete(); model->remove_thread(this); }