nachos.kernel.threads
Class Semaphore

java.lang.Object
  extended by nachos.kernel.threads.Semaphore

public class Semaphore
extends java.lang.Object

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

name

public final java.lang.String name
Printable name useful for debugging.


value

private int value
The value of the semaphore, always >= 0.


queue

private List queue
Threads waiting in P() for the value to be > 0.

Constructor Detail

Semaphore

public Semaphore(java.lang.String debugName,
                 int initialValue)
Initialize a semaphore, so that it can be used for synchronization.

Parameters:
debugName - An arbitrary name, useful for debugging.
initialValue - The initial value of the semaphore.
Method Detail

P

public void P()
Wait until semaphore value > 0, then decrement.


V

public void V()
Increment semaphore value, waking up a waiter if necessary.