se.lth.cs.realtime.semaphore
Class BinarySem

java.lang.Object
  |
  +--se.lth.cs.realtime.semaphore.BinarySem
All Implemented Interfaces:
Semaphore

public final class BinarySem
extends java.lang.Object
implements Semaphore

A Semaphore containing a boolean flag instead of a counter, to support signaling in cases when waiting threads should not catch up with successive missed calls of give. Another reason to use this class, mainly applicable to small compiled applications, is efficiency when this type of semaphore is the simplest and most optimized one for the particular type of OS/kernel. Instead of a boolean flag, an implementation is free to use equivalent solutions such as a counter with a maximum value of one, as natively defined in some operating systems such as Windows.

The order of the awakenings of the threads which called take depends on the underlying OS and/or VM, but for standard implementation in pure Java it will be the same as for the wait/notify mechanism.

See Also:
Semaphore, MutexSem, CountingSem, MultistepSem, SemInterrupted

Constructor Summary
BinarySem()
          Creates a new BinarySem with the internal (boolean) flag set to false.
BinarySem(boolean access)
          Creates a new BinarySem with the internal (boolean) flag set to the specified value.
BinarySem(int count)
          Creates a new BinarySem with the boolean initialized in a way compatible with the constructors of CountingSem.
 
Method Summary
 void give()
          Sets the internal flag to true and releases the first-in-queue blocked thread, if any.
 void take()
          Causes the calling thread to block until the boolean flag represented by this semaphore is set to true.
 boolean tryTake(long timeout)
          Try to take the semaphore but give up after a certain time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinarySem

public BinarySem()
Creates a new BinarySem with the internal (boolean) flag set to false.

BinarySem

public BinarySem(boolean access)
Creates a new BinarySem with the internal (boolean) flag set to the specified value.

BinarySem

public BinarySem(int count)
Creates a new BinarySem with the boolean initialized in a way compatible with the constructors of CountingSem.
Method Detail

give

public void give()
Sets the internal flag to true and releases the first-in-queue blocked thread, if any.
Specified by:
give in interface Semaphore

take

public void take()
Causes the calling thread to block until the boolean flag represented by this semaphore is set to true. On exiting, the flag is set to false, telling that the semaphore has been taken.
Specified by:
take in interface Semaphore
Throws:
SemInterrupted - (which is an error and not an exception) if the calling thread has been interrupted before or during blocking. The caller is not expected to catch it since it indicated severe violation in the handling of resources, concurrency, or time. Thus, you should not catch such an error, except for special cases like handling an emergency stop.

tryTake

public boolean tryTake(long timeout)
Try to take the semaphore but give up after a certain time. Be careful not to neglect the returned value, which could result in concurrency faults.
Specified by:
tryTake in interface Semaphore
Returns:
true if the semaphore was successfully taken, and false if the timeout occured which means that the semaphore was not taken.
Throws:
SemInterrupted - (which is an error and not an exception) if the calling thread has been interrupted before or during blocking. The caller is not expected to catch it since it indicated severe violation in the handling of resources, concurrency, or time. Thus, you should not catch such an error, except for special cases like handling an emergency stop.