next up previous
Next: Devices as Objects Up: Object Oriented Design Previous: Memory Heaps

Synchronization Objects

These classes were obvious and successful. uCR includes several synchronization classes, most derived from the base class ISync. The ISync class is a concrete class that allows threads to wait for a general condition to become true, and allows other threads to notify of a possible change in state. This base class is the most primitive and general synchronization that allows threads to interact with other threads and interrupt service routines.gif

typedef bool (*sfun)(volatile void*);
class ISync {
      void sleep(sfun fun,
                 volatile void*);
      void wakeup();

The ISema class is a counting semaphore implemented with the ISync class. The only operations supported are increment and decrement (and initialize with a specific value). The methods are easily implemented with the sleep and wakeup operations of the ISync class.

The ILock class is a binary semaphore. In principle, it could be implemented with the ISema class, but it works out more efficiently derived directly from ISync. Operations for ILock are get and put, and do the obvious things.

ISema, ILock and other classes are implemented by deriving from the ISync class. They all have similar fundamental behavior that can be easily factored into the base ISync class. The Mutex class implements monitor style synchronization and does not fit well in the ISync class hierarchy, so it is implemented separately.

The Mutex class has ``enter()'' and ``leave()'' methods to enter critical sections of code. Only one thread may be active in a critical section, hence the synchronization. The Condition class is a way for a thread to sleep within a critical section. A thread sleeping on a condition is not considered active in the critical section so other threads can enter. However, the implementations of Mutex and Condition assure that at all times there is no more then one thread executing in the critical section.

Stephen Williams
Sun May 4 15:28:26 PDT 1997