threads, model, schedule: refactor thread joining
authorBrian Norris <banorris@uci.edu>
Wed, 12 Sep 2012 02:48:38 +0000 (19:48 -0700)
committerBrian Norris <banorris@uci.edu>
Wed, 12 Sep 2012 02:52:34 +0000 (19:52 -0700)
The Thread can hold a list of ModelAction (instead of Threads) that are waiting
for its completion. This will give the exiting Thread a better ability to
handle the event.

This also deletes Scheduler::wait, since it is no superceded by Scheduler::sleep.

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

index cb7e43f06964a564c05aba4650baf99f792d2c10..4f29e21bbc9871d56ce8676327064634554d6784 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -360,17 +360,20 @@ Thread * ModelChecker::check_current_action(ModelAction *curr)
                break;
        }
        case THREAD_JOIN: {
-               Thread *wait, *join;
-               wait = get_thread(curr);
-               join = (Thread *)curr->get_location();
-               if (!join->is_complete())
-                       scheduler->wait(wait, join);
+               Thread *waiting, *blocking;
+               waiting = get_thread(curr);
+               blocking = (Thread *)curr->get_location();
+               if (!blocking->is_complete()) {
+                       blocking->push_wait_list(curr);
+                       scheduler->sleep(waiting);
+               }
                break;
        }
        case THREAD_FINISH: {
                Thread *th = get_thread(curr);
                while (!th->wait_list_empty()) {
-                       Thread *wake = th->pop_wait_list();
+                       ModelAction *act = th->pop_wait_list();
+                       Thread *wake = get_thread(act);
                        scheduler->wake(wake);
                }
                th->complete();
index 9063fdb4680243c6d3a78e686ef29f217ecbf0cd..cbb4957a3e4a3865730679e8e816dfb51f636324 100644 (file)
@@ -31,20 +31,6 @@ void Scheduler::remove_thread(Thread *t)
                readyList.remove(t);
 }
 
-/**
- * Force one Thread to wait on another Thread. The "join" Thread should
- * eventually wake up the waiting Thread via Scheduler::wake.
- * @param wait The Thread that should wait
- * @param join The Thread on which we are waiting.
- */
-void Scheduler::wait(Thread *wait, Thread *join)
-{
-       ASSERT(!join->is_complete());
-       remove_thread(wait);
-       join->push_wait_list(wait);
-       wait->set_state(THREAD_BLOCKED);
-}
-
 /**
  * Prevent a Thread from being scheduled. The sleeping Thread should be
  * re-awoken via Scheduler::wake.
index 7875e0b6e24147c5e526b58ace7a580bc53c79c3..a7483e02340942cf773ea7a69c890d72b384f69a 100644 (file)
@@ -18,7 +18,6 @@ public:
        Scheduler();
        void add_thread(Thread *t);
        void remove_thread(Thread *t);
-       void wait(Thread *wait, Thread *join);
        void sleep(Thread *t);
        void wake(Thread *t);
        Thread * next_thread(Thread *t);
index 248d948fe98c87863741d4ed94343ee22d9cef5e..87a21ef2633f46f14de70240f052bc50af2adcce 100644 (file)
--- a/threads.h
+++ b/threads.h
@@ -79,17 +79,17 @@ public:
        bool wait_list_empty() { return wait_list.empty(); }
 
        /**
-        * Add a thread to the waiting list for this thread.
-        * @param t The Thread to add
+        * Add a ModelAction to the waiting list for this thread.
+        * @param t The ModelAction to add. Must be a JOIN.
         */
-       void push_wait_list(Thread *t) { wait_list.push_back(t); }
+       void push_wait_list(ModelAction *act) { wait_list.push_back(act); }
 
        /**
-        * Remove one Thread from the waiting list
-        * @return The Thread that was removed from the waiting list
+        * Remove one ModelAction from the waiting list
+        * @return The ModelAction that was removed from the waiting list
         */
-       Thread * pop_wait_list() {
-               Thread *ret = wait_list.front();
+       ModelAction * pop_wait_list() {
+               ModelAction *ret = wait_list.front();
                wait_list.pop_back();
                return ret;
        }
@@ -111,11 +111,11 @@ private:
        thread_state state;
 
        /**
-        * A list of Threads waiting on this Thread. Particularly, this list is
-        * used for thread joins, where another Thread waits for this Thread to
-        * complete
+        * A list of ModelActions waiting on this Thread. Particularly, this
+        * list is used for thread joins, where another Thread waits for this
+        * Thread to complete
         */
-       std::vector<Thread *> wait_list;
+       std::vector<ModelAction *> wait_list;
 
        /**
         * The value returned by the last action in this thread