From: Brian Norris Date: Thu, 26 Apr 2012 23:52:20 +0000 (-0700) Subject: demote 'system_thread' to just 'system_context' X-Git-Tag: pldi2013~490 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=8a7dd35db7542ad66f87060cf637172249494e47;p=model-checker.git demote 'system_thread' to just 'system_context' I don't need the extra 'class Thread' metadata for the ModelChecker 'system thread,' and the dual use of class Thread for both system and user threads was causing problems. So I separate them: class Thread is used only for user threads. This changes a few interfaces throughout the code. --- diff --git a/model.cc b/model.cc index 19570ff..f1bc320 100644 --- a/model.cc +++ b/model.cc @@ -50,11 +50,6 @@ int ModelChecker::get_next_id() return ++used_thread_id; } -void ModelChecker::add_system_thread(Thread *t) -{ - this->system_thread = t; -} - Thread * ModelChecker::schedule_next_thread() { Thread *t; @@ -239,14 +234,13 @@ void ModelChecker::remove_thread(Thread *t) int ModelChecker::switch_to_master(ModelAction *act) { - Thread *old, *next; + Thread *old; DBG(); old = thread_current(); set_current_action(act); old->set_state(THREAD_READY); - next = system_thread; - return old->swap(next); + return Thread::swap(old, get_system_context()); } ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value) diff --git a/model.h b/model.h index aac968a..ad8760a 100644 --- a/model.h +++ b/model.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "schedule.h" #include "libthreads.h" @@ -69,9 +70,9 @@ public: ModelChecker(); ~ModelChecker(); class Scheduler *scheduler; - Thread *system_thread; - void add_system_thread(Thread *t); + void set_system_context(ucontext_t *ctxt) { system_context = ctxt; } + ucontext_t * get_system_context(void) { return system_context; } void set_current_action(ModelAction *act) { current_action = act; } void check_current_action(void); @@ -102,6 +103,7 @@ private: Backtrack *exploring; thread_id_t nextThread; + ucontext_t *system_context; action_list_t *action_trace; std::map thread_map; class TreeNode *rootNode, *currentNode; diff --git a/threads.cc b/threads.cc index 917090f..dc7202e 100644 --- a/threads.cc +++ b/threads.cc @@ -42,15 +42,20 @@ int Thread::create_context() 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); +} + +int Thread::swap(ucontext_t *ctxt, Thread *t) +{ + return swapcontext(ctxt, &t->context); } void Thread::complete() @@ -99,7 +104,7 @@ Thread::Thread(thrd_t *t) { state = THREAD_CREATED; id = model->get_next_id(); *user_thread = id; - model->add_system_thread(this); + model->set_system_context(&context); } Thread::~Thread() @@ -137,7 +142,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) @@ -153,12 +158,15 @@ 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 */ @@ -168,7 +176,6 @@ int main() thread_wait_finish(); } while (model->next_execution()); - delete th; delete model; DEBUG("Exiting\n"); diff --git a/threads.h b/threads.h index 44e1013..490081a 100644 --- a/threads.h +++ b/threads.h @@ -22,7 +22,9 @@ public: Thread(thrd_t *t); ~Thread(); void complete(); - int swap(Thread *t); + + static int swap(ucontext_t *ctxt, Thread *t); + static int swap(Thread *t, ucontext_t *ctxt); thread_state get_state() { return state; } void set_state(thread_state s) { state = s; }