cz.cuni.jagrlib
Class Semaphore

java.lang.Object
  extended by cz.cuni.jagrlib.Semaphore

public class Semaphore
extends java.lang.Object

General semaphore for inter-thread synchronization.

Since:
0.13
See Also:
Semaphore.java

Field Summary
protected  int counter
          Number of waiting clients (<0) or number of free signals (>0).
 
Constructor Summary
Semaphore(int limit)
          Initializing constructor.
 
Method Summary
 int getSignalCount()
          Returns number of signals available.
 void semSignal(int number)
          Signals the number of times.
 void semSignalAll(int limit)
          Resume all waiting threads (at most limit ones).
 boolean semWait()
          Waits for one signal.
 boolean semWait(long millis)
          Waits for one signal for the restricted amount of time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

counter

protected int counter
Number of waiting clients (<0) or number of free signals (>0).

Constructor Detail

Semaphore

public Semaphore(int limit)
Initializing constructor.

Parameters:
limit - Initial number of free signals (should be >= 0)
Method Detail

semWait

public boolean semWait()
Waits for one signal. Blocks the current thread for (potentially) infinite time.

Returns:
true if the current thread becomes a signal (false if it is interrupted by another thread).
See Also:
semWait(long)

semWait

public boolean semWait(long millis)
Waits for one signal for the restricted amount of time.

Parameters:
millis - Number of milliseconds to wait (0L => wait forever).
Returns:
true if the current thread becomes a signal or the given time expires (false if it is interrupted by another thread).
See Also:
semWait()

semSignal

public void semSignal(int number)
Signals the number of times. Resumes at most number of waiting threads.

Parameters:
number - Number of signals to be generated.
See Also:
semSignalAll(int)

semSignalAll

public void semSignalAll(int limit)
Resume all waiting threads (at most limit ones).

Parameters:
limit - Maximum number of waiting threads to be resumed (for <= 0 resumes exactly all currently waiting threads).
See Also:
semSignal(int)

getSignalCount

public int getSignalCount()
Returns number of signals available.

Returns:
Number of signals available in current time.