tac0S
Template Affectional Command Operating System
|
The following class defines a "semaphore" whose value is a non-negative integer. The semaphore has only two operations P() and V(): More...
#include <synch.h>
Public Member Functions | |
Semaphore (const char *debugName, int initialValue) | |
Semaphore::Semaphore Initialize a semaphore, so that it can be used for synchronization. More... | |
~Semaphore () | |
Semaphore::Semaphore De-allocate semaphore, when no longer needed. Assume no one is still waiting on the semaphore! | |
const char * | getName () |
void | P () |
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. More... | |
void | V () |
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 threads are disabled when it is called. | |
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.
Semaphore::Semaphore | ( | const char * | debugName, |
int | initialValue | ||
) |
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. |
void Semaphore::P | ( | ) |
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.