public class WriterReaderPhaser extends Object
WriterReaderPhaser
instances provide an asymmetric means for synchronizing the execution of
wait-free "writer" critical sections against a "reader phase flip" that needs to make sure no writer critical
sections that were active at the beginning of the flip are still active after the flip is done. Multiple writers
and multiple readers are supported.
While a WriterReaderPhaser
can be useful in multiple scenarios, a specific and common use case is
that of safely managing "double buffered" data stream access in which writers can proceed without being
blocked, while readers gain access to stable and unchanging buffer samples
NOTE: WriterReaderPhaser
writers are wait-free on architectures that support wait-free atomic
increment operations. They remain lock-free (but not wait-free) on architectures that do not support
wait-free atomic increment operations.
WriterReaderPhaser
"writers" are wait free, "readers" block for other "readers", and
"readers" are only blocked by "writers" whose critical was entered before the reader's
flipPhase()
attempt.
When used to protect an actively recording data structure, the assumptions on how readers and writers act are:
writerCriticalSectionEnter()
and writerCriticalSectionExit(long)
).WriterReaderPhaser
guarantees that the inactive data structures are not
being modified by any writers while being read while under readerLock() protection after a flipPhase()
operation.Constructor and Description |
---|
WriterReaderPhaser() |
Modifier and Type | Method and Description |
---|---|
void |
flipPhase()
Flip a phase in the
WriterReaderPhaser instance, flipPhase()
can only be called while holding the readerLock(). |
void |
flipPhase(long yieldTimeNsec)
Flip a phase in the
WriterReaderPhaser instance, flipPhase()
can only be called while holding the readerLock(). |
void |
readerLock()
Enter to a critical section containing a read operation (mutually excludes against other
readerLock calls). |
void |
readerUnlock()
Exit from a critical section containing a read operation (relinquishes mutual exclusion against other
readerLock calls). |
long |
writerCriticalSectionEnter()
Indicate entry to a critical section containing a write operation.
|
void |
writerCriticalSectionExit(long criticalValueAtEnter)
Indicate exit from a critical section containing a write operation.
|
public long writerCriticalSectionEnter()
This call is wait-free on architectures that support wait free atomic increment operations, and is lock-free on architectures that do not.
writerCriticalSectionEnter()
must be matched with a subsequent
writerCriticalSectionExit(long)
in order for CriticalSectionPhaser
synchronization to function properly.
writerCriticalSectionExit(long)
call.public void writerCriticalSectionExit(long criticalValueAtEnter)
This call is wait-free on architectures that support wait free atomic increment operations, and is lock-free on architectures that do not.
writerCriticalSectionExit(long)
must be matched with a preceding
writerCriticalSectionEnter()
call, and must be provided with the
matching writerCriticalSectionEnter()
call's return value, in
order for CriticalSectionPhaser synchronization to function properly.
criticalValueAtEnter
- the (opaque) value returned from the matching
writerCriticalSectionEnter()
call.public void readerLock()
readerLock
calls).
readerLock
DOES NOT provide synchronization
against writerCriticalSectionEnter()
calls. Use flipPhase()
to synchronize reads against writers.
public void readerUnlock()
readerLock
calls).public void flipPhase(long yieldTimeNsec)
WriterReaderPhaser
instance, flipPhase()
can only be called while holding the readerLock().
flipPhase()
will return only after all writer critical sections (protected by
writerCriticalSectionEnter()
()} and
writerCriticalSectionExit(long)
()}) that may have been in flight when the
flipPhase()
call were made had completed.
No actual writer critical section activity is required for flipPhase()
to
succeed.
However, flipPhase()
is lock-free with respect to calls to
writerCriticalSectionEnter()
and
writerCriticalSectionExit(long)
. It may spin-wait for for active
writer critical section code to complete.
yieldTimeNsec
- The amount of time (in nanoseconds) to sleep in each yield if yield loop is needed.public void flipPhase()
WriterReaderPhaser
instance, flipPhase()
can only be called while holding the readerLock().
flipPhase()
will return only after all writer critical sections (protected by
writerCriticalSectionEnter()
()} and
writerCriticalSectionExit(long)
()}) that may have been in flight when the
flipPhase()
call were made had completed.
No actual writer critical section activity is required for flipPhase()
to
succeed.
However, flipPhase()
is lock-free with respect to calls to
writerCriticalSectionEnter()
and
writerCriticalSectionExit(long)
. It may spin-wait for for active
writer critical section code to complete.
Copyright © 2016. All rights reserved.