Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock Class Reference

List of all members.

Public Member Functions

 ReadPrioritaryFIFOWriteLock ()
void acquireRead () throws InterruptedException
synchronized void releaseRead ()
void acquireWrite () throws InterruptedException
synchronized void releaseWrite ()
final synchronized boolean isReadLocked ()
final synchronized boolean isWriteLocked ()

Detailed Description

Reader/Writer lock with write priority.

If a reader holds the lock, all incoming readers are allowed to acquire the lock until a write arrives. A writer must wait for all current readers to release the lock before acquiring it.
When a writer has the lock, everybody else is blocked.
When a writer release the lock, there is a writer priority so if another writer is waiting it will have the lock even if it arrived later than readers. Writers are prioritary against readers but all writers get the lock in a FIFO order.

Author:
Emmanuel Cecchet
Version:
1.0

Definition at line 44 of file ReadPrioritaryFIFOWriteLock.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.ReadPrioritaryFIFOWriteLock  ) 
 

Creates a new ReadPrioritaryFIFOWriteLock instance.

Definition at line 64 of file ReadPrioritaryFIFOWriteLock.java.

00065   {
00066     activeReaders = 0;
00067     activeWriter = false;
00068     waitingReaders = 0;
00069     waitingWriters = 0;
00070     readSync = new Object();
00071     writeWaitingQueue = new ArrayList();
00072   }


Member Function Documentation

void org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.acquireRead  )  throws InterruptedException
 

Acquires the lock for a read.

Exceptions:
InterruptedException if wait failed (the lock should remain in a correct state)

Definition at line 80 of file ReadPrioritaryFIFOWriteLock.java.

00081   {
00082     synchronized (this)
00083     {
00084       if ((waitingWriters == 0) && !activeWriter)
00085       { // No active or pending writer
00086         activeReaders++;
00087         return;
00088       }
00089     }
00090 
00091     // Beyond this point, we have to wait
00092     synchronized (readSync)
00093     {
00094       // It becomes a little bit tricky here but a writer could have
00095       // released the lock between the first test at the beginning of
00096       // this function and this point. Therefore we have to recheck if
00097       // the lock is not completely idle else we'll never wake up !
00098       synchronized (this)
00099       {
00100         if (!activeWriter)
00101         { // No active or pending writer
00102           activeReaders++;
00103           return;
00104         }
00105       }
00106 
00107       waitingReaders++;
00108       try
00109       {
00110         readSync.wait();
00111       }
00112       catch (InterruptedException ie)
00113       {
00114         waitingReaders--; // roll back state
00115         throw ie;
00116       }
00117       waitingReaders--;
00118     }
00119     synchronized (this)
00120     {
00121       activeReaders++;
00122     }
00123   }

void org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.acquireWrite  )  throws InterruptedException
 

Acquires the lock for a write.

Exceptions:
InterruptedException if wait failed (the lock should remain in a correct state)

Definition at line 149 of file ReadPrioritaryFIFOWriteLock.java.

Referenced by org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.enableBackend(), and org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.enableBackend().

00150   {
00151     synchronized (Thread.currentThread())
00152     {
00153       synchronized (this)
00154       {
00155         if ((activeReaders == 0) && !activeWriter)
00156         {
00157           activeWriter = true;
00158           return;
00159         }
00160         else
00161         {
00162           waitingWriters++;
00163           writeWaitingQueue.add(Thread.currentThread());
00164         }
00165       }
00166       try
00167       {
00168         Thread.currentThread().wait();
00169       }
00170       catch (InterruptedException ie)
00171       {
00172         releaseWrite();
00173         throw ie;
00174       }
00175     }
00176   }

final synchronized boolean org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.isReadLocked  ) 
 

Tests if the lock is currently held by at least one reader.

Returns:
true if the lock is held by a reader

Definition at line 211 of file ReadPrioritaryFIFOWriteLock.java.

00212   {
00213     return activeReaders > 0;
00214   }

final synchronized boolean org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.isWriteLocked  ) 
 

Tests if the lock is currently held by a writer.

Returns:
true if the lock is held by a writer

Definition at line 221 of file ReadPrioritaryFIFOWriteLock.java.

00222   {
00223     return activeWriter;
00224   }

synchronized void org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.releaseRead  ) 
 

Releases a lock previously acquired for reading.

Definition at line 128 of file ReadPrioritaryFIFOWriteLock.java.

00129   {
00130     activeReaders--;
00131     if ((activeReaders == 0) && (waitingWriters > 0))
00132     { // Wake up first waiting write if any
00133       Object thread = writeWaitingQueue.remove(0);
00134       synchronized (thread)
00135       {
00136         thread.notify();
00137         activeWriter = true;
00138         waitingWriters--;
00139       }
00140     }
00141   }

synchronized void org.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock.releaseWrite  ) 
 

Releases a lock previously acquired for writing.

Definition at line 181 of file ReadPrioritaryFIFOWriteLock.java.

Referenced by org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.disableBackend(), org.objectweb.cjdbc.controller.loadbalancer.raidb2.RAIDb2ec.enableBackend(), and org.objectweb.cjdbc.controller.loadbalancer.raidb1.RAIDb1ec.enableBackend().

00182   {
00183     activeWriter = false;
00184 
00185     // Writer priority
00186     if (waitingWriters > 0)
00187     { // Wake up first waiting write if any
00188       Object thread = writeWaitingQueue.remove(0);
00189       synchronized (thread)
00190       {
00191         thread.notify();
00192         activeWriter = true;
00193         waitingWriters--;
00194       }
00195     }
00196 
00197     // Wake up readers
00198     else
00199       synchronized (readSync)
00200       {
00201         if (waitingReaders > 0)
00202           readSync.notifyAll();
00203       }
00204   }


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:02:14 2005 for C-JDBC by  doxygen 1.3.9.1