com.newisys.dv
Class Semaphore

java.lang.Object
  extended by com.newisys.dv.Semaphore

public final class Semaphore
extends Object

Implements a counting semaphore using a SimulationManager.


Constructor Summary
Semaphore(SimulationManager simManager, int permits)
          Constructs a new semaphore with a generated name and the given initial permit count using the given simulation manager.
Semaphore(SimulationManager simManager, String name, int permits)
          Constructs a new semaphore with the given name and initial permit count using the given simulation manager.
 
Method Summary
 void acquire()
          Acquire one permit from this semaphore, waiting until one is available if necessary.
 void acquire(int permits)
          Acquires the given number of permits from this semaphore, waiting until they are available if necessary.
 boolean attempt()
          Attempts to acquire one permit from this semaphore.
 boolean attempt(int permits)
          Attempts to acquire the given number of permits from this semaphore.
 void release()
          Releases one permit for this semaphore, allowing it to be acquired by other threads.
 void release(int permits)
          Releases the given number of permits for this semaphore, allowing them to be acquired by other threads.
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Semaphore

public Semaphore(SimulationManager simManager,
                 String name,
                 int permits)
Constructs a new semaphore with the given name and initial permit count using the given simulation manager.

Parameters:
simManager - the simulation manager coordinating this semaphore
name - the name of this semaphore (for debugging purposes)
permits - the number of times acquire() will succeed before release() is called

Semaphore

public Semaphore(SimulationManager simManager,
                 int permits)
Constructs a new semaphore with a generated name and the given initial permit count using the given simulation manager.

Parameters:
simManager - the simulation manager coordinating this semaphore
permits - the number of times acquire() will succeed before release() is called
Method Detail

acquire

public void acquire()
Acquire one permit from this semaphore, waiting until one is available if necessary.


acquire

public void acquire(int permits)
Acquires the given number of permits from this semaphore, waiting until they are available if necessary. This method is non-greedy, meaning that it will not acquire any permits until the full number of requested permits is available at once. This algorithm prevents deadlock (caused by multiple waiters holding permits while waiting for more) but allows livelock (a thread waiting for multiple permits could be starved by threads waiting for fewer permits), so it should be used with care.

Parameters:
permits - the number of permits to acquire

attempt

public boolean attempt()
Attempts to acquire one permit from this semaphore. If a permit is available now, this method will acquire it and return true. Otherwise, no permit is acquired and this method returns false.

Returns:
a boolean indicating whether a permit was acquired

attempt

public boolean attempt(int permits)
Attempts to acquire the given number of permits from this semaphore. If all permits are available now, this method will acquire them and return true. Otherwise, no permits are acquired and this method returns false.

Parameters:
permits - the number of permits to acquire
Returns:
a boolean indicating whether the permits were acquired

release

public void release()
Releases one permit for this semaphore, allowing it to be acquired by other threads. Note that no checking is done to ensure that the current thread ever acquired the released permit.


release

public void release(int permits)
Releases the given number of permits for this semaphore, allowing them to be acquired by other threads. Note that no checking is done to ensure that the current thread ever acquired the released permits.

Parameters:
permits - the number of permits to release
Throws:
IllegalArgumentException - if permits is < 0

toString

public String toString()
Overrides:
toString in class Object