edu.cmu.emulator.util
Class RWLock

java.lang.Object
  extended by edu.cmu.emulator.util.RWLock

public final class RWLock
extends Object

A reader/writer lock. This lock will allow multiple readers, but only a single writer. TO DO: - prevent starvation of writers (will only happen under severe circumstances, but it should be made impossible)

Author:
Glenn Judd

Field Summary
private  ArrayList readers
           
private  int writeLockDepth
           
private  Thread writer
           
 
Constructor Summary
RWLock()
           
 
Method Summary
 void readLock()
          Obtain a read lock.
 void release()
          Release the lock.
 void verifyReadLock()
          Verifies that the current thread holds a read lock.
 void verifyWriteLock()
          Verifies that the current thread holds a write lock.
 void writeLock()
          Obtain an exclusive write lock.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

readers

private ArrayList readers

writer

private Thread writer

writeLockDepth

private int writeLockDepth
Constructor Detail

RWLock

public RWLock()
Method Detail

readLock

public final void readLock()
Obtain a read lock. If a thread holding the write lock calls this method it will simply return still holding the write lock. That is write lock is also implicitly a read lock, and this method will not downgrade the write lock to a read lock without an explicit release.


writeLock

public final void writeLock()
Obtain an exclusive write lock. This method will wait for all readers to complete before obtaining a write lock. This method will not upgrade read locks to write locks.


release

public final void release()
Release the lock. This class tracks what sort of lock this thread holds, and will relase the appropriate lock.


verifyReadLock

public final void verifyReadLock()
Verifies that the current thread holds a read lock.


verifyWriteLock

public final void verifyWriteLock()
Verifies that the current thread holds a write lock.



Copyright © 2013. All Rights Reserved.