Class FSHLog.SafePointZigZagLatch
- Enclosing class:
- FSHLog
Thread A signals Thread B to hold when it gets to a 'safe point'. Thread A wait until Thread B gets there. When the 'safe point' has been attained, Thread B signals Thread A. Thread B then holds at the 'safe point'. Thread A on notification that Thread B is paused, goes ahead and does the work it needs to do while Thread B is holding. When Thread A is done, it flags B and then Thread A and Thread B continue along on their merry way. Pause and signalling 'zigzags' between the two participating threads. We use two latches -- one the inverse of the other -- pausing and signaling when states are achieved.
To start up the drama, Thread A creates an instance of this class each time it would do this
zigzag dance and passes it to Thread B (these classes use Latches so it is one shot only).
Thread B notices the new instance (via reading a volatile reference or how ever) and it starts
to work toward the 'safe point'. Thread A calls waitSafePoint(SyncFuture)
when it
cannot proceed until the Thread B 'safe point' is attained. Thread A will be held inside in
waitSafePoint(SyncFuture)
until Thread B reaches the 'safe point'. Once there, Thread
B frees Thread A by calling safePointAttained()
. Thread A now knows Thread B is at the
'safe point' and that it is holding there (When Thread B calls safePointAttained()
it
blocks here until Thread A calls releaseSafePoint()
). Thread A proceeds to do what it
needs to do while Thread B is paused. When finished, it lets Thread B lose by calling
releaseSafePoint()
and away go both Threads again.
-
Field Summary
Modifier and TypeFieldDescriptionprivate CountDownLatch
Count down this latch when safe point attained.private CountDownLatch
Latch to wait on. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprivate void
checkIfSyncFailed
(SyncFuture syncFuture) (package private) boolean
isCocked()
Returns True is this is a 'cocked', fresh instance, and not one that has already fired.(package private) boolean
Returns if the safepoint has been attained.(package private) void
Called by Thread A when it is done with the work it needs to do while Thread B is halted.(package private) void
Called by Thread B when it attains the 'safe point'.(package private) SyncFuture
waitSafePoint
(SyncFuture syncFuture) For Thread A to call when it is ready to wait on the 'safe point' to be attained.
-
Field Details
-
safePointAttainedLatch
Count down this latch when safe point attained. -
safePointReleasedLatch
Latch to wait on. Will be released when we can proceed.
-
-
Constructor Details
-
SafePointZigZagLatch
-
-
Method Details
-
checkIfSyncFailed
-
waitSafePoint
SyncFuture waitSafePoint(SyncFuture syncFuture) throws InterruptedException, FailedSyncBeforeLogCloseException For Thread A to call when it is ready to wait on the 'safe point' to be attained. Thread A will be held in here until Thread B callssafePointAttained()
- Parameters:
syncFuture
- We need this as barometer on outstanding syncs. If it comes home with an exception, then something is up w/ our syncing.- Returns:
- The passed
syncFuture
- Throws:
InterruptedException
FailedSyncBeforeLogCloseException
-
isSafePointAttained
Returns if the safepoint has been attained. -
safePointAttained
Called by Thread B when it attains the 'safe point'. In this method, Thread B signals Thread A it can proceed. Thread B will be held in here untilreleaseSafePoint()
is called by Thread A.- Throws:
InterruptedException
-
releaseSafePoint
void releaseSafePoint()Called by Thread A when it is done with the work it needs to do while Thread B is halted. This will release the Thread B held in a call tosafePointAttained()
-
isCocked
boolean isCocked()Returns True is this is a 'cocked', fresh instance, and not one that has already fired.
-