From 9b99c30c39f17b485eeca827a9e83806bf67ffac Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Thu, 6 Sep 2012 13:37:35 -0700 Subject: [PATCH] schedule: add wait and wake functions 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 | 24 ++++++++++++++++++++++++ schedule.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/schedule.cc b/schedule.cc index d96172e..be4a92f 100644 --- a/schedule.cc +++ b/schedule.cc @@ -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. diff --git a/schedule.h b/schedule.h index c3d029f..664b2d2 100644 --- a/schedule.h +++ b/schedule.h @@ -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; -- 2.34.1