nachos.threads
Class Semaphore
java.lang.Object
|
+--nachos.threads.Semaphore
- public class Semaphore
- extends Object
A Semaphore is a synchronization primitive with an unsigned value.
A semaphore has only two operations:
- P(): waits until the semaphore's value is greater than zero,
then decrements it.
- V(): increments the semaphore's value, and wakes up one thread
waiting in P() if possible.
Note that this API 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, a context switch might have occurred,
and some other thread might have called P() or V(), so the
true value might now be different.
|
Constructor Summary |
Semaphore(int initialValue)
Allocate a new semaphore. |
|
Method Summary |
void |
P()
Atomically wait for this semaphore to become non-zero and decrement it. |
static void |
selfTest()
Test if this module is working. |
void |
V()
Atomically increment this semaphore and wake up at most one other thread
sleeping on this semaphore. |
| Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Semaphore
public Semaphore(int initialValue)
- Allocate a new semaphore.
- Parameters:
initialValue - the initial value of this semaphore.
P
public void P()
- Atomically wait for this semaphore to become non-zero and decrement it.
V
public void V()
- Atomically increment this semaphore and wake up at most one other thread
sleeping on this semaphore.
selfTest
public static void selfTest()
- Test if this module is working.