schedule: add wait and wake functions
authorBrian Norris <banorris@uci.edu>
Thu, 6 Sep 2012 20:37:35 +0000 (13:37 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 6 Sep 2012 20:48:56 +0000 (13:48 -0700)
These functions will be used to prevent execution of threads that should be
waiting for another event (e.g., a THREAD_JOIN should cause a thread to wait
for another thread's exit).

schedule.cc
schedule.h

index d96172e010d445a2b06eff5b392b89161126f12e..be4a92f739a1129bb2aa4bee50bb23ba0a3f8556 100644 (file)
@@ -31,6 +31,30 @@ 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);
+}
+
+/**
+ * Wake a Thread up that was previously waiting (see Scheduler::wait)
+ * @param t The Thread to wake up
+ */
+void Scheduler::wake(Thread *t)
+{
+       add_thread(t);
+       t->set_state(THREAD_READY);
+}
+
 /**
  * Remove one Thread from the scheduler. This implementation defaults to FIFO,
  * if a thread is not already provided.
index c3d029fb2ed4b77246889ad8f7a070b7893a080a..664b2d245bea0b05a3f072d9c0eb75146bdfaf0f 100644 (file)
@@ -18,6 +18,8 @@ public:
        Scheduler();
        void add_thread(Thread *t);
        void remove_thread(Thread *t);
+       void wait(Thread *wait, Thread *join);
+       void wake(Thread *t);
        Thread * next_thread(Thread *t);
        Thread * get_current_thread() const;
        void print() const;