edu.wpi.first.wpilibj.communication
Class Semaphore

java.lang.Object
  extended by edu.wpi.first.wpilibj.communication.Semaphore

public class Semaphore
extends java.lang.Object

Emulates various VXWorks semaphores using Java native methods. Note: The options passed in currently are completely ignored.

Author:
wolf
Todo:
do something with the options?

Nested Class Summary
static class Semaphore.Options
          Options to create a semaphore with.
 
Field Summary
static int WAIT_FOREVER
           
 
Constructor Summary
Semaphore(Semaphore.Options options)
          Create a new mutex semaphore.
Semaphore(Semaphore.Options options, boolean initialState)
          Create a boolean semaphore with the given initial state.
Semaphore(Semaphore.Options options, int count)
          Create a counting semaphore with the given number of permits.
 
Method Summary
 void close()
           
protected  void doWaitForever()
          Attempt to wait forever on the semaphore.
protected  void doWaitMillis(int timeout)
          Attempt to wait for a certain number of milliseconds on a semaphore.
 void flush()
          Unblock every task that is blocked by the semaphore.
 void free()
          Free all of the resources associated with this semaphore.
 Pointer getPointer()
           
 void give()
          Release the semaphore.
 void takeForever()
          Take the semaphore.
 void takeMillis(int timeout)
          Take the semaphore.
 boolean tryTake()
          Attempt to take the semaphore, but don't block.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WAIT_FOREVER

public static final int WAIT_FOREVER
See Also:
Constant Field Values
Constructor Detail

Semaphore

public Semaphore(Semaphore.Options options)
Create a new mutex semaphore.

Parameters:
options - The options to create the semaphore with.

Semaphore

public Semaphore(Semaphore.Options options,
                 boolean initialState)
Create a boolean semaphore with the given initial state.

Parameters:
options - The options to create the semaphore with.
initialState - The initial state for the semaphore to have.

Semaphore

public Semaphore(Semaphore.Options options,
                 int count)
Create a counting semaphore with the given number of permits.

Parameters:
options - The options to create the semaphore with.
count - The initial count for the semaphore to hold.
Method Detail

flush

public void flush()
           throws SemaphoreException
Unblock every task that is blocked by the semaphore.

Throws:
SemaphoreException

give

public void give()
          throws SemaphoreException
Release the semaphore.

Throws:
SemaphoreException

tryTake

public boolean tryTake()
                throws SemaphoreException
Attempt to take the semaphore, but don't block.

Returns:
True if the semaphore was successfully taken, false otherwise
Throws:
SemaphoreException

doWaitForever

protected void doWaitForever()
                      throws SemaphoreException,
                             java.lang.InterruptedException
Attempt to wait forever on the semaphore. Called in a loop by takeMillis() to catch InterruptedExceptions.

Throws:
SemaphoreException
java.lang.InterruptedException

doWaitMillis

protected void doWaitMillis(int timeout)
                     throws SemaphoreException,
                            java.lang.InterruptedException
Attempt to wait for a certain number of milliseconds on a semaphore. Called in a loop by takeMillis() to catch InterruptedExceptions.

Parameters:
timeout - The number of milliseconds to wait, at most.
Throws:
SemaphoreException
java.lang.InterruptedException

getPointer

public final Pointer getPointer()

takeMillis

public final void takeMillis(int timeout)
                      throws SemaphoreException
Take the semaphore. Block for timeout milliseconds.

Parameters:
timeout - The number of milliseconds to block.
Throws:
SemaphoreException - if the lock can't be take in timeout seconds

takeForever

public final void takeForever()
                       throws SemaphoreException
Take the semaphore. Block forever until semaphore is available.

Throws:
SemaphoreException

close

public final void close()
                 throws SemaphoreException
Throws:
SemaphoreException

free

public final void free()
                throws SemaphoreException
Free all of the resources associated with this semaphore. Actually does nothing; this is for VxWorks semaphores.

Throws:
SemaphoreException