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

Semaphore Class Reference

The following class defines a "semaphore" whose value is a non-negative integer. The semaphore has only two operations P() and V(): P() -- waits until value > 0, then decrement V() -- increment, waking up a thread waiting in P() if necessary Note that the interface does *not* allow a thread to read the value of the semaphore directly -- even if you did read the value, the only thing you would know is what the value used to be. You don't know what the value is now, because by the time you get the value into a register, a context switch might have occurred, and some other thread might have called P or V, so the true value might now be different. More...

#include <synch.h>

List of all members.

Public Methods

 Semaphore (char *debugName, int initialValue)
 set initial value. More...

 ~Semaphore ()
 de-allocate semaphore. More...

char* getName ()
 debugging assist. More...

void P ()
 these are the only operations on a semaphore. More...

void V ()
 they are both *atomic*. More...

void SelfTest ()
 test routine for semaphore implementation. More...


Private Attributes

char* name
 useful for debugging. More...

int value
 semaphore value, always >= 0. More...

List<Thread *>* queue
 threads waiting in P() for the value to be > 0. More...


Detailed Description

The following class defines a "semaphore" whose value is a non-negative integer. The semaphore has only two operations P() and V(): P() -- waits until value > 0, then decrement V() -- increment, waking up a thread waiting in P() if necessary Note that the interface does *not* allow a thread to read the value of the semaphore directly -- even if you did read the value, the only thing you would know is what the value used to be. You don't know what the value is now, because by the time you get the value into a register, a context switch might have occurred, and some other thread might have called P or V, so the true value might now be different.

Definition at line 40 of file synch.h.


Constructor & Destructor Documentation

Semaphore::Semaphore ( char * debugName,
int initialValue )
 

set initial value.

Semaphore::Semaphore Initialize a semaphore, so that it can be used for synchronization.

"debugName" is an arbitrary name, useful for debugging. "initialValue" is the initial value of the semaphore.

Definition at line 47 of file synch.cc.

Referenced by SelfTest().

Semaphore::~Semaphore ( )
 

de-allocate semaphore.

Semaphore::Semaphore De-allocate semaphore, when no longer needed. Assume no one is still waiting on the semaphore!

Definition at line 60 of file synch.cc.


Member Function Documentation

void Semaphore::P ( )
 

these are the only operations on a semaphore.

Semaphore::P Wait until semaphore value > 0, then decrement. Checking the value and decrementing must be done atomically, so we need to disable interrupts before checking the value.

Note that Thread::Sleep assumes that interrupts are disabled when it is called.

Definition at line 76 of file synch.cc.

Referenced by Lock::Acquire(), SynchConsoleInput::GetChar(), PostOfficeInput::PostalDelivery(), SynchConsoleOutput::PutChar(), SynchDisk::ReadSector(), SelfTest(), SelfTestHelper(), PostOfficeOutput::Send(), Condition::Wait(), ElevatorInspector::WaitForNextControllerEvent(), ElevatorInspector::WaitForNextRiderEvent(), and SynchDisk::WriteSector().

void Semaphore::SelfTest ( )
 

test routine for semaphore implementation.

Definition at line 136 of file synch.cc.

Referenced by ThreadedKernel::SelfTest().

void Semaphore::V ( )
 

they are both *atomic*.

Semaphore::V Increment semaphore value, waking up a waiter if necessary. As with P(), this operation must be atomic, so we need to disable interrupts. Scheduler::ReadyToRun() assumes that interrupts are disabled when it is called.

Definition at line 103 of file synch.cc.

Referenced by SynchDisk::CallBack(), SynchConsoleOutput::CallBack(), SynchConsoleInput::CallBack(), PostOfficeOutput::CallBack(), PostOfficeInput::CallBack(), ElevatorInspector::CallBack(), Lock::Release(), SelfTest(), SelfTestHelper(), and Condition::Signal().

char * Semaphore::getName ( ) [inline]
 

debugging assist.

Definition at line 44 of file synch.h.


Member Data Documentation

char * Semaphore::name [private]
 

useful for debugging.

Definition at line 51 of file synch.h.

List< Thread *> * Semaphore::queue<Thread *> [private]
 

threads waiting in P() for the value to be > 0.

Definition at line 54 of file synch.h.

int Semaphore::value [private]
 

semaphore value, always >= 0.

Definition at line 52 of file synch.h.


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