org.objectweb.cjdbc.common.util
Class ReadPrioritaryFIFOWriteLock

java.lang.Object
  extended byorg.objectweb.cjdbc.common.util.ReadPrioritaryFIFOWriteLock

public class ReadPrioritaryFIFOWriteLock
extends java.lang.Object

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.

Version:
1.0
Author:
Emmanuel Cecchet

Field Summary
private  int activeReaders
          Threads executing read.
private  boolean activeWriter
          Is there an active writer?
private  java.lang.Object readSync
           
private  int waitingReaders
          Threads not yet in read.
private  int waitingWriters
          Threads not yet in write.
private  java.util.ArrayList writeWaitingQueue
           
 
Constructor Summary
ReadPrioritaryFIFOWriteLock()
          Creates a new ReadPrioritaryFIFOWriteLock instance.
 
Method Summary
 void acquireRead()
          Acquires the lock for a read.
 void acquireWrite()
          Acquires the lock for a write.
 boolean isReadLocked()
          Tests if the lock is currently held by at least one reader.
 boolean isWriteLocked()
          Tests if the lock is currently held by a writer.
 void releaseRead()
          Releases a lock previously acquired for reading.
 void releaseWrite()
          Releases a lock previously acquired for writing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activeReaders

private int activeReaders
Threads executing read.


activeWriter

private boolean activeWriter
Is there an active writer?


waitingReaders

private int waitingReaders
Threads not yet in read.


waitingWriters

private int waitingWriters
Threads not yet in write.


readSync

private java.lang.Object readSync

writeWaitingQueue

private java.util.ArrayList writeWaitingQueue
Constructor Detail

ReadPrioritaryFIFOWriteLock

public ReadPrioritaryFIFOWriteLock()
Creates a new ReadPrioritaryFIFOWriteLock instance.

Method Detail

acquireRead

public void acquireRead()
                 throws java.lang.InterruptedException
Acquires the lock for a read.

Throws:
java.lang.InterruptedException - if wait failed (the lock should remain in a correct state)

releaseRead

public void releaseRead()
Releases a lock previously acquired for reading.


acquireWrite

public void acquireWrite()
                  throws java.lang.InterruptedException
Acquires the lock for a write.

Throws:
java.lang.InterruptedException - if wait failed (the lock should remain in a correct state)

releaseWrite

public void releaseWrite()
Releases a lock previously acquired for writing.


isReadLocked

public final boolean isReadLocked()
Tests if the lock is currently held by at least one reader.

Returns:
true if the lock is held by a reader

isWriteLocked

public final boolean isWriteLocked()
Tests if the lock is currently held by a writer.

Returns:
true if the lock is held by a writer


Copyright © 2002, 2005 - ObjectWeb Consortium - All Rights Reserved.