X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=threads.cc;h=a8f282a8b32efdb96d1d248570fc8b7574af92f7;hb=3e520de48b5be0b0d4fa4a5f033e207b17c4496d;hp=6b4e2c740ab50b7b55f624b7bda05a4c9ad72678;hpb=84d0cd2f078f4cb15c318a0fef2515feab570375;p=model-checker.git diff --git a/threads.cc b/threads.cc index 6b4e2c7..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); } @@ -178,3 +186,13 @@ thread_id_t Thread::get_id() const { return id; } + +/** + * Set a thread's THREAD_* state (@see thread_state) + * @param s The state to enter + */ +void Thread::set_state(thread_state s) +{ + ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED); + state = s; +}