|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnachos.kernel.threads.Semaphore
public class Semaphore
This 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.
Field Summary | |
---|---|
java.lang.String |
name
Printable name useful for debugging. |
private List |
queue
Threads waiting in P() for the value to be > 0. |
private int |
value
The value of the semaphore, always >= 0. |
Constructor Summary | |
---|---|
Semaphore(java.lang.String debugName,
int initialValue)
Initialize a semaphore, so that it can be used for synchronization. |
Method Summary | |
---|---|
void |
P()
Wait until semaphore value > 0, then decrement. |
void |
V()
Increment semaphore value, waking up a waiter if necessary. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public final java.lang.String name
private int value
private List queue
Constructor Detail |
---|
public Semaphore(java.lang.String debugName, int initialValue)
debugName
- An arbitrary name, useful for debugging.initialValue
- The initial value of the semaphore.Method Detail |
---|
public void P()
public void V()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |