org.objectweb.dream.synchro
Class SemaphoreImpl

java.lang.Object
  extended by org.objectweb.dream.AbstractComponent
      extended by org.objectweb.dream.synchro.SemaphoreImpl
All Implemented Interfaces:
ContextualBindingController, NeedAsyncStartController, PrepareStopLifeCycleController, Loggable, Semaphore, SemaphoreAttributeController, AttributeController, BindingController, LifeCycleController
Direct Known Subclasses:
AbstractSemaphoreQueuedImpl

public class SemaphoreImpl
extends AbstractComponent
implements Semaphore, SemaphoreAttributeController

Basic implementation of the Semaphoreinterface.

Note: Inspired by Doug Lea's implementation.


Field Summary
protected  long permits
          current number of available permits *
 
Fields inherited from class org.objectweb.dream.AbstractComponent
bindingLogger, fcNeedAsyncStart, fcState, lifeCycleLogger, logger, weaveableC
 
Fields inherited from interface org.objectweb.dream.synchro.Semaphore
ITF_NAME
 
Fields inherited from interface org.objectweb.fractal.api.control.LifeCycleController
STARTED, STOPPED
 
Constructor Summary
SemaphoreImpl()
           
 
Method Summary
 void acquire()
          Waits until a permit is available, and take one
 boolean attempt(long msecs)
          Waits at most msecs millisconds for a permit.
 long getInitialPermits()
          Returns the initial number of permits of the semaphore.
 String[] listFc()
           
 long permits()
          Returns the current number of available permits.
 void release()
          Release a permit
 void release(long n)
          Releases N permits.
 void setInitialPermits(long initialPermits)
          Sets the initial number of permits of the semaphore.
 
Methods inherited from class org.objectweb.dream.AbstractComponent
beforeFirstStart, bindFc, bindFc, getFcNeedAsyncStart, getFcState, initComponent, lookupFc, prepareStopFc, setLogger, startFc, stopFc, unbindFc
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

permits

protected long permits
current number of available permits *

Constructor Detail

SemaphoreImpl

public SemaphoreImpl()
Method Detail

acquire

public void acquire()
             throws InterruptedException
Description copied from interface: Semaphore
Waits until a permit is available, and take one

Specified by:
acquire in interface Semaphore
Throws:
InterruptedException - if the thread is interrupted while trying to acquire the permit.
See Also:
Semaphore.acquire()

attempt

public boolean attempt(long msecs)
                throws InterruptedException
Description copied from interface: Semaphore
Waits at most msecs millisconds for a permit.

Specified by:
attempt in interface Semaphore
Parameters:
msecs - the number of milleseconds to wait. An argument less than or equal to zero means not to wait at all. However, this may still require access to a synchronization lock, which can impose unbounded delay if there is a lot of contention among threads.
Returns:
true if acquired.
Throws:
InterruptedException - if the thread is interrupted while waiting for the permit.
See Also:
Semaphore.attempt(long)

release

public void release()
Description copied from interface: Semaphore
Release a permit

Specified by:
release in interface Semaphore
See Also:
Semaphore.release()

release

public void release(long n)
Description copied from interface: Semaphore
Releases N permits.

Specified by:
release in interface Semaphore
Parameters:
n - the number of permits to be released.
See Also:
Semaphore.release(long)

permits

public long permits()
Description copied from interface: Semaphore
Returns the current number of available permits.

Specified by:
permits in interface Semaphore
Returns:
an accurate, but possibly unstable value, that may change immediately after returning.
See Also:
Semaphore.permits()

setInitialPermits

public void setInitialPermits(long initialPermits)
Description copied from interface: SemaphoreAttributeController
Sets the initial number of permits of the semaphore. Setting initialPermits to one makes the semaphore act as a mutual exclusion lock. Negative seeds are also allowed, in which case no acquires will proceed until the number of releases has pushed the number of permits past 0.

Specified by:
setInitialPermits in interface SemaphoreAttributeController
Parameters:
initialPermits - the initial number of permits to set.
See Also:
SemaphoreAttributeController.setInitialPermits(long)

getInitialPermits

public long getInitialPermits()
Description copied from interface: SemaphoreAttributeController
Returns the initial number of permits of the semaphore.

Specified by:
getInitialPermits in interface SemaphoreAttributeController
Returns:
the initial number of permits of the semaphore.
See Also:
SemaphoreAttributeController.getInitialPermits()

listFc

public String[] listFc()
Specified by:
listFc in interface BindingController
See Also:
BindingController.listFc()


Copyright © 2003, 2005 - INRIA Rhone-Alpes - All Rights Reserved.