org.objectweb.dream.synchro
Class ReadWriteLockFIFOImpl

java.lang.Object
  extended by org.objectweb.dream.AbstractComponent
      extended by org.objectweb.dream.synchro.ReadWriteLockFIFOImpl
All Implemented Interfaces:
ContextualBindingController, NeedAsyncStartController, PrepareStopLifeCycleController, Loggable, ReadWriteLock, BindingController, LifeCycleController

public class ReadWriteLockFIFOImpl
extends AbstractComponent
implements ReadWriteLock

FIFO implementation of the ReadWriteLockinterface. Threads contend in a First-in/First-out manner for access. This lock is NOT reentrant. Current readers and writers should not try to re-obtain locks while holding them

Note: Inspired by Doug Lea's implementation.


Field Summary
protected  Semaphore entryLock
          Fair Semaphore serving as a kind of mutual exclusion lock.
protected  int exreaders
          Number of threads that have exited read lock.
protected  int readers
          Number of threads that have entered read lock.
 
Fields inherited from class org.objectweb.dream.AbstractComponent
bindingLogger, fcNeedAsyncStart, fcState, lifeCycleLogger, logger, weaveableC
 
Fields inherited from interface org.objectweb.dream.synchro.ReadWriteLock
ITF_NAME
 
Fields inherited from interface org.objectweb.fractal.api.control.LifeCycleController
STARTED, STOPPED
 
Constructor Summary
ReadWriteLockFIFOImpl()
           
 
Method Summary
 void acquireRead()
          Gets the read lock.
 void acquireWrite()
          Gets the write lock.
 boolean attemptRead(long msecs)
          Attempts to get the read lock.
 boolean attemptWrite(long msecs)
          Attempts to get the write lock.
 void bindFc(String clientItfName, Object serverItf)
           
 String[] listFc()
           
 void releaseRead()
          Releases the read lock.
 void releaseWrite()
          Releases the write lock.
 
Methods inherited from class org.objectweb.dream.AbstractComponent
beforeFirstStart, 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

entryLock

protected Semaphore entryLock
Fair Semaphore serving as a kind of mutual exclusion lock. Writers acquire on entry, and hold until rwlock exit. Readers acquire and release only during entry (but are blocked from doing so if there is an active writer).


readers

protected volatile int readers
Number of threads that have entered read lock. Note that this is never reset to zero. Incremented only during acquisition of read lock while the "entryLock" is held, but read elsewhere, so is declared volatile.


exreaders

protected int exreaders
Number of threads that have exited read lock. Note that this is never reset to zero. Accessed only in code protected by synchronized(this). When exreaders != readers, the rwlock is being used for reading. Else if the entry lock is held, it is being used for writing (or in transition). Else it is free. Note: To distinguish these states, we assume that fewer than 2^32 reader threads can simultaneously execute.

Constructor Detail

ReadWriteLockFIFOImpl

public ReadWriteLockFIFOImpl()
Method Detail

acquireRead

public void acquireRead()
                 throws InterruptedException
Description copied from interface: ReadWriteLock
Gets the read lock.

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

releaseRead

public void releaseRead()
Description copied from interface: ReadWriteLock
Releases the read lock.

Specified by:
releaseRead in interface ReadWriteLock
See Also:
ReadWriteLock.releaseRead()

acquireWrite

public void acquireWrite()
                  throws InterruptedException
Description copied from interface: ReadWriteLock
Gets the write lock.

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

releaseWrite

public void releaseWrite()
Description copied from interface: ReadWriteLock
Releases the write lock.

Specified by:
releaseWrite in interface ReadWriteLock
See Also:
ReadWriteLock.releaseWrite()

attemptRead

public boolean attemptRead(long msecs)
                    throws InterruptedException
Description copied from interface: ReadWriteLock
Attempts to get the read lock.

Specified by:
attemptRead in interface ReadWriteLock
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 lock.
See Also:
ReadWriteLock.attemptRead(long)

attemptWrite

public boolean attemptWrite(long msecs)
                     throws InterruptedException
Description copied from interface: ReadWriteLock
Attempts to get the write lock.

Specified by:
attemptWrite in interface ReadWriteLock
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 lock.
See Also:
ReadWriteLock.attemptWrite(long)

bindFc

public void bindFc(String clientItfName,
                   Object serverItf)
            throws NoSuchInterfaceException,
                   IllegalBindingException,
                   IllegalLifeCycleException
Specified by:
bindFc in interface BindingController
Overrides:
bindFc in class AbstractComponent
Throws:
NoSuchInterfaceException
IllegalBindingException
IllegalLifeCycleException
See Also:
BindingController.bindFc(java.lang.String, java.lang.Object)

listFc

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


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