demote 'system_thread' to just 'system_context'
authorBrian Norris <banorris@uci.edu>
Thu, 26 Apr 2012 23:52:20 +0000 (16:52 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 26 Apr 2012 23:52:20 +0000 (16:52 -0700)
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.

model.cc
model.h
threads.cc
threads.h

index 19570ffc2914fda9ccb003a73836a525fb96de83..f1bc320b417e7c7f0938a73d50eff20b2a6b7301 100644 (file)
--- 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 aac968a9f14077ca17953f934c82881266dc781b..ad8760a28576aafecc17d05d9cb01b92e7df259d 100644 (file)
--- a/model.h
+++ b/model.h
@@ -4,6 +4,7 @@
 #include <list>
 #include <map>
 #include <cstddef>
+#include <ucontext.h>
 
 #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_id_t, class Thread *> thread_map;
        class TreeNode *rootNode, *currentNode;
index 917090fb71500665009e1c5d4d0e50d3de729e06..dc7202e3aaa08e4e9dc00d8d6a40e6c04834180e 100644 (file)
@@ -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");
index 44e1013768e911c6aaec6aa8564fd2a629b94cc7..490081afe882c41b0bfec641740f43c4e99d90ed 100644 (file)
--- 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; }