Top Description Inners Fields Constructors Methods
EDU.oswego.cs.dl.util.concurrent

public Class WriterPreferenceReadWriteLock

extends Object
implements ReadWriteLock
Class Inheritance
All Implemented Interfaces
EDU.oswego.cs.dl.util.concurrent.ReadWriteLock
Known Direct Subclasses
EDU.oswego.cs.dl.util.concurrent.ReaderPreferenceReadWriteLock, EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock

A ReadWriteLock that prefers waiting writers over waiting readers when there is contention. This class is adapted from the versions described in CPJ, improving on the ones there a bit by segregating reader and writer wait queues, which is typically more efficient.

The locks are NOT reentrant. In particular, even though it may appear to usually work OK, a thread holding a read lock should not attempt to re-acquire it. Doing so risks lockouts when there are also waiting writers.

See Also
Introduction to this package.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
protected abstract class
WriterPreferenceReadWriteLock.Signaller

Reader and Writer requests are maintained in two different wait sets, by two different objects.

protected class

Field Summary

Modifier and TypeField and Description
protected long
protected Thread
protected final WriterPreferenceReadWriteLock.ReaderLock
protected long
protected long
protected final WriterPreferenceReadWriteLock.WriterLock

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected boolean
allowReader()

Override this method to change to reader preference

protected synchronized void
protected synchronized void
protected synchronized WriterPreferenceReadWriteLock.Signaller
endRead()

Called upon termination of a read.

protected synchronized WriterPreferenceReadWriteLock.Signaller
endWrite()

Called upon termination of a write.

public Sync
protected synchronized boolean
protected synchronized boolean
protected synchronized boolean
protected synchronized boolean
protected synchronized boolean
protected synchronized boolean
public Sync
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

activeReaders_back to summary
protected long activeReaders_
activeWriter_back to summary
protected Thread activeWriter_
readerLock_back to summary
protected final WriterPreferenceReadWriteLock.ReaderLock readerLock_
waitingReaders_back to summary
protected long waitingReaders_
waitingWriters_back to summary
protected long waitingWriters_
writerLock_back to summary
protected final WriterPreferenceReadWriteLock.WriterLock writerLock_

Constructor Detail

WriterPreferenceReadWriteLockback to summary
public WriterPreferenceReadWriteLock()

Method Detail

allowReaderback to summary
protected boolean allowReader()

Override this method to change to reader preference

cancelledWaitingReaderback to summary
protected synchronized void cancelledWaitingReader()
cancelledWaitingWriterback to summary
protected synchronized void cancelledWaitingWriter()
endReadback to summary
protected synchronized WriterPreferenceReadWriteLock.Signaller endRead()

Called upon termination of a read. Returns the object to signal to wake up a waiter, or null if no such

endWriteback to summary
protected synchronized WriterPreferenceReadWriteLock.Signaller endWrite()

Called upon termination of a write. Returns the object to signal to wake up a waiter, or null if no such

readLockback to summary
public Sync readLock()

Implements EDU.oswego.cs.dl.util.concurrent.ReadWriteLock.readLock.

Doc from EDU.oswego.cs.dl.util.concurrent.ReadWriteLock.readLock.

get the readLock

startReadback to summary
protected synchronized boolean startRead()
startReadFromNewReaderback to summary
protected synchronized boolean startReadFromNewReader()
startReadFromWaitingReaderback to summary
protected synchronized boolean startReadFromWaitingReader()
startWriteback to summary
protected synchronized boolean startWrite()
startWriteFromNewWriterback to summary
protected synchronized boolean startWriteFromNewWriter()
startWriteFromWaitingWriterback to summary
protected synchronized boolean startWriteFromWaitingWriter()
writeLockback to summary
public Sync writeLock()

Implements EDU.oswego.cs.dl.util.concurrent.ReadWriteLock.writeLock.

Doc from EDU.oswego.cs.dl.util.concurrent.ReadWriteLock.writeLock.

get the writeLock

EDU.oswego.cs.dl.util.concurrent back to summary

protected Class WriterPreferenceReadWriteLock.ReaderLock

extends Signaller
implements Sync
Class Inheritance
All Implemented Interfaces
EDU.oswego.cs.dl.util.concurrent.Sync

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
acquire()

Implements EDU.oswego.cs.dl.util.concurrent.Sync.acquire.

Wait (possibly forever) until successful passage.
public boolean
attempt(long
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.
msecs
)

Implements EDU.oswego.cs.dl.util.concurrent.Sync.attempt.

Wait at most msecs to pass; report whether passed.
public void
release()

Implements EDU.oswego.cs.dl.util.concurrent.Sync.release.

Potentially enable others to pass.
pack-priv synchronized void

Constructor Detail

ReaderLockback to summary
protected ReaderLock()

Method Detail

acquireback to summary
public void acquire() throws InterruptedException

Implements EDU.oswego.cs.dl.util.concurrent.Sync.acquire.

Doc from EDU.oswego.cs.dl.util.concurrent.Sync.acquire.

Wait (possibly forever) until successful passage. Fail only upon interuption. Interruptions always result in `clean' failures. On failure, you can be sure that it has not been acquired, and that no corresponding release should be performed. Conversely, a normal return guarantees that the acquire was successful.

attemptback to summary
public boolean attempt(long msecs) throws InterruptedException

Implements EDU.oswego.cs.dl.util.concurrent.Sync.attempt.

Doc from EDU.oswego.cs.dl.util.concurrent.Sync.attempt.

Wait at most msecs to pass; report whether passed.

The method has best-effort semantics: The msecs bound cannot be guaranteed to be a precise upper bound on wait time in Java. Implementations generally can only attempt to return as soon as possible after the specified bound. Also, timers in Java do not stop during garbage collection, so timeouts can occur just because a GC intervened. So, msecs arguments should be used in a coarse-grained manner. Further, implementations cannot always guarantee that this method will return at all without blocking indefinitely when used in unintended ways. For example, deadlocks may be encountered when called in an unintended context.

Parameters
msecs:long

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:boolean

true if acquired

releaseback to summary
public void release()

Implements EDU.oswego.cs.dl.util.concurrent.Sync.release.

Doc from EDU.oswego.cs.dl.util.concurrent.Sync.release.

Potentially enable others to pass.

Because release does not raise exceptions, it can be used in `finally' clauses without requiring extra embedded try/catch blocks. But keep in mind that as with any java method, implementations may still throw unchecked exceptions such as Error or NullPointerException when faced with uncontinuable errors. However, these should normally only be caught by higher-level error handlers.

signalWaitersback to summary
pack-priv synchronized void signalWaiters()

Implements abstract EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock.Signaller.signalWaiters.

EDU.oswego.cs.dl.util.concurrent back to summary

protected abstract Class WriterPreferenceReadWriteLock.Signaller

extends Object
Class Inheritance
  • java.lang.Object
  • EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock.Signaller
Known Direct Subclasses
EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock.ReaderLock, EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock.WriterLock

Reader and Writer requests are maintained in two different wait sets, by two different objects. These objects do not know whether the wait sets need notification since they don't know preference rules. So, each supports a method that can be selected by main controlling object to perform the notifications. This base class simplifies mechanics.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Signallerback to summary
protected Signaller()

Method Detail

signalWaitersback to summary
pack-priv abstract void signalWaiters()
EDU.oswego.cs.dl.util.concurrent back to summary

protected Class WriterPreferenceReadWriteLock.WriterLock

extends Signaller
implements Sync
Class Inheritance
All Implemented Interfaces
EDU.oswego.cs.dl.util.concurrent.Sync

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public void
acquire()

Implements EDU.oswego.cs.dl.util.concurrent.Sync.acquire.

Wait (possibly forever) until successful passage.
public boolean
attempt(long
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.
msecs
)

Implements EDU.oswego.cs.dl.util.concurrent.Sync.attempt.

Wait at most msecs to pass; report whether passed.
public void
release()

Implements EDU.oswego.cs.dl.util.concurrent.Sync.release.

Potentially enable others to pass.
pack-priv synchronized void

Constructor Detail

WriterLockback to summary
protected WriterLock()

Method Detail

acquireback to summary
public void acquire() throws InterruptedException

Implements EDU.oswego.cs.dl.util.concurrent.Sync.acquire.

Doc from EDU.oswego.cs.dl.util.concurrent.Sync.acquire.

Wait (possibly forever) until successful passage. Fail only upon interuption. Interruptions always result in `clean' failures. On failure, you can be sure that it has not been acquired, and that no corresponding release should be performed. Conversely, a normal return guarantees that the acquire was successful.

attemptback to summary
public boolean attempt(long msecs) throws InterruptedException

Implements EDU.oswego.cs.dl.util.concurrent.Sync.attempt.

Doc from EDU.oswego.cs.dl.util.concurrent.Sync.attempt.

Wait at most msecs to pass; report whether passed.

The method has best-effort semantics: The msecs bound cannot be guaranteed to be a precise upper bound on wait time in Java. Implementations generally can only attempt to return as soon as possible after the specified bound. Also, timers in Java do not stop during garbage collection, so timeouts can occur just because a GC intervened. So, msecs arguments should be used in a coarse-grained manner. Further, implementations cannot always guarantee that this method will return at all without blocking indefinitely when used in unintended ways. For example, deadlocks may be encountered when called in an unintended context.

Parameters
msecs:long

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:boolean

true if acquired

releaseback to summary
public void release()

Implements EDU.oswego.cs.dl.util.concurrent.Sync.release.

Doc from EDU.oswego.cs.dl.util.concurrent.Sync.release.

Potentially enable others to pass.

Because release does not raise exceptions, it can be used in `finally' clauses without requiring extra embedded try/catch blocks. But keep in mind that as with any java method, implementations may still throw unchecked exceptions such as Error or NullPointerException when faced with uncontinuable errors. However, these should normally only be caught by higher-level error handlers.

signalWaitersback to summary
pack-priv synchronized void signalWaiters()

Implements abstract EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock.Signaller.signalWaiters.