Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Condition Class Reference

The following class defines a "condition variable". A condition variable does not have a value, but threads may be queued, waiting on the variable. These are only operations on a condition variable: Wait() -- release the lock, relinquish the CPU until signaled, then re-acquire the lock Signal() -- wake up a thread, if there are any waiting on the condition Broadcast() -- wake up all threads waiting on the condition All operations on a condition variable must be made while the current thread has acquired a lock. Indeed, all accesses to a given condition variable must be protected by the same lock. In other words, mutual exclusion must be enforced among threads calling the condition variable operations. In Nachos, condition variables are assumed to obey *Mesa*-style semantics. When a Signal or Broadcast wakes up another thread, it simply puts the thread on the ready list, and it is the responsibility of the woken thread to re-acquire the lock (this re-acquire is taken care of within Wait()). By contrast, some define condition variables according to *Hoare*-style semantics -- where the signalling thread gives up control over the lock and the CPU to the woken thread, which runs immediately and gives back control over the lock to the signaller when the woken thread leaves the critical section. The consequence of using Mesa-style semantics is that some other thread can acquire the lock, and change data structures, before the woken thread gets a chance to run. The advantage to Mesa-style semantics is that it is a lot easier to implement than Hoare-style. More...

#include <synch.h>

List of all members.

Public Methods

 Condition (char *debugName)
 initialize condition to "no one waiting". More...

 ~Condition ()
 deallocate the condition. More...

char* getName ()
void Wait (Lock *conditionLock)
void Signal (Lock *conditionLock)
void Broadcast (Lock *conditionLock)

Private Attributes

char* name
List<Semaphore *>* waitQueue
 list of waiting threads. More...


Detailed Description

The following class defines a "condition variable". A condition variable does not have a value, but threads may be queued, waiting on the variable. These are only operations on a condition variable: Wait() -- release the lock, relinquish the CPU until signaled, then re-acquire the lock Signal() -- wake up a thread, if there are any waiting on the condition Broadcast() -- wake up all threads waiting on the condition All operations on a condition variable must be made while the current thread has acquired a lock. Indeed, all accesses to a given condition variable must be protected by the same lock. In other words, mutual exclusion must be enforced among threads calling the condition variable operations. In Nachos, condition variables are assumed to obey *Mesa*-style semantics. When a Signal or Broadcast wakes up another thread, it simply puts the thread on the ready list, and it is the responsibility of the woken thread to re-acquire the lock (this re-acquire is taken care of within Wait()). By contrast, some define condition variables according to *Hoare*-style semantics -- where the signalling thread gives up control over the lock and the CPU to the woken thread, which runs immediately and gives back control over the lock to the signaller when the woken thread leaves the critical section. The consequence of using Mesa-style semantics is that some other thread can acquire the lock, and change data structures, before the woken thread gets a chance to run. The advantage to Mesa-style semantics is that it is a lot easier to implement than Hoare-style.

Definition at line 124 of file synch.h.


Constructor & Destructor Documentation

Condition::Condition ( char * debugName )
 

initialize condition to "no one waiting".

Condition::Condition Initialize a condition variable, so that it can be used for synchronization. Initially, no one is waiting on the condition.

"debugName" is an arbitrary name, useful for debugging.

Definition at line 213 of file synch.cc.

Condition::~Condition ( )
 

deallocate the condition.

Condition::Condition Deallocate the data structures implementing a condition variable.

Definition at line 224 of file synch.cc.


Member Function Documentation

void Condition::Broadcast ( Lock * conditionLock )
 

Condition::Broadcast Wake up all threads waiting on this condition, if any.

"conditionLock" -- lock protecting the use of this condition

Definition at line 292 of file synch.cc.

void Condition::Signal ( Lock * conditionLock )
 

Condition::Signal Wake up a thread waiting on this condition, if any.

Note: we assume Mesa-style semantics, which means that the signaller doesn't give up control immediately to the thread being woken up (unlike Hoare-style).

Also note: we assume the caller holds the monitor lock (unlike what is described in Birrell's paper). This allows us to access waitQueue without disabling interrupts.

"conditionLock" -- lock protecting the use of this condition

Definition at line 273 of file synch.cc.

Referenced by SynchList::Append(), and Broadcast().

void Condition::Wait ( Lock * conditionLock )
 

Condition::Wait Atomically release monitor lock and go to sleep. Our implementation uses semaphores to implement this, by allocating a semaphore for each waiting thread. The signaller will V() this semaphore, so there is no chance the waiter will miss the signal, even though the lock is released before calling P().

Note: we assume Mesa-style semantics, which means that the waiter must re-acquire the monitor lock when waking up.

"conditionLock" -- lock protecting the use of this condition

Definition at line 244 of file synch.cc.

Referenced by SynchList::RemoveFront().

char * Condition::getName ( ) [inline]
 

Definition at line 129 of file synch.h.


Member Data Documentation

char * Condition::name [private]
 

Definition at line 142 of file synch.h.

List< Semaphore *> * Condition::waitQueue<Semaphore *> [private]
 

list of waiting threads.

Definition at line 143 of file synch.h.


The documentation for this class was generated from the following files:
Generated at Wed Jul 4 11:32:23 2001 for Nachos by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001