Class Clutch
java.lang.Object
org.cojen.tupl.util.Latch
org.cojen.tupl.util.Clutch
A clutch is a specialized latch which can support highly concurrent shared requests, under
the assumption that exclusive requests are infrequent. When too many shared requests are
denied due to high contention, the clutch switches to a special contended mode. Later, when
an exclusive clutch is acquired, the mode switches back to non-contended mode. This design
allows the clutch to be adaptive, by relying on the exclusive clutch as a signal that access
patterns have changed.
Note: Shared access should not be held by any thread indefinitely. If another thread attempts to switch to contended mode, it first needs to acquire exclusive access in order to make the switch. The thread will block even though shared access could have been granted if it just kept trying. This behavior holds true for downgrades as well. Another thread cannot switch to contended mode until after the downgraded latch is fully released.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSharable object for supporting contended clutches.Nested classes/interfaces inherited from class Latch
Latch.Condition -
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidAcquire the exclusive latch, barging ahead of any waiting threads if possible.final voidAcquire the exclusive latch, aborting if interrupted.final voidAcquire a shared latch, barging ahead of any waiting threads if possible.final voidAcquire a shared latch, aborting if interrupted.final booleanLike tryAcquireShared, except blocks if an exclusive latch is held.final intacquireSharedUncontendedNanos(long nanosTimeout) Like tryAcquireSharedNanos, except blocks if an exclusive latch is held.final voiddowngrade(boolean contended) Downgrade the held exclusive latch with the option to try switching to contended mode.protected abstract Clutch.PackgetPack()Returns the pack associated with this clutch, which should be shared to reduce the overall memory footprint.final booleanReturns true if clutch is operating in contended mode.static Clutchmake()Return a new Clutch instance, which might share a pack with other instances returned from this method.static Clutchmake(int initialState) Return a new Clutch instance, which might share a pack with other instances returned from this method.final voidreleaseExclusive(boolean contended) Release the held exclusive latch with the option to try switching to contended mode.final voidRelease a held shared latch.toString()final booleanTry to acquire the exclusive latch, barging ahead of any waiting threads if possible.final booleantryAcquireExclusiveNanos(long nanosTimeout) Attempt to acquire the exclusive latch, aborting if interrupted.final booleanTry to acquire a shared latch, barging ahead of any waiting threads if possible.final booleantryAcquireSharedNanos(long nanosTimeout) Attempt to acquire a shared latch, aborting if interrupted.final booleanAttempt to upgrade a held shared latch into an exclusive latch.final voiduponExclusive(Runnable cont) Invokes the given continuation upon the latch being acquired exclusively.Methods inherited from class Latch
downgrade, hasQueuedThreads, release, releaseEither, releaseExclusive
-
Constructor Details
-
Clutch
public Clutch() -
Clutch
public Clutch(int initialState) - Parameters:
initialState- UNLATCHED, EXCLUSIVE, or SHARED
-
-
Method Details
-
make
Return a new Clutch instance, which might share a pack with other instances returned from this method. -
make
Return a new Clutch instance, which might share a pack with other instances returned from this method.- Parameters:
initialState- UNLATCHED, EXCLUSIVE, or SHARED
-
isContended
public final boolean isContended()Returns true if clutch is operating in contended mode. -
tryAcquireExclusive
public final boolean tryAcquireExclusive()Description copied from class:LatchTry to acquire the exclusive latch, barging ahead of any waiting threads if possible.- Overrides:
tryAcquireExclusivein classLatch
-
tryAcquireExclusiveNanos
Description copied from class:LatchAttempt to acquire the exclusive latch, aborting if interrupted.- Overrides:
tryAcquireExclusiveNanosin classLatch- Parameters:
nanosTimeout- pass negative for infinite timeout- Throws:
InterruptedException
-
acquireExclusive
public final void acquireExclusive()Description copied from class:LatchAcquire the exclusive latch, barging ahead of any waiting threads if possible.- Overrides:
acquireExclusivein classLatch
-
acquireExclusiveInterruptibly
Description copied from class:LatchAcquire the exclusive latch, aborting if interrupted.- Overrides:
acquireExclusiveInterruptiblyin classLatch- Throws:
InterruptedException
-
uponExclusive
Description copied from class:LatchInvokes the given continuation upon the latch being acquired exclusively. When acquired, the continuation is run by the current thread, or it's enqueued to be run by a thread which releases the latch. The releasing thread actually retains the latch and runs the continuation, effectively transferring latch ownership. The continuation must not explicitly release the latch, although it can downgrade the latch. Any exception thrown by the continuation is passed to the uncaught exception handler of the running thread, and then the latch is released.- Overrides:
uponExclusivein classLatch- Parameters:
cont- called with latch held
-
downgrade
public final void downgrade(boolean contended) Downgrade the held exclusive latch with the option to try switching to contended mode. Caller must later call releaseShared instead of releaseExclusive.- Parameters:
contended- pass true to try switching to contended mode
-
releaseExclusive
public final void releaseExclusive(boolean contended) Release the held exclusive latch with the option to try switching to contended mode.- Parameters:
contended- pass true to try switching to contended mode
-
tryUpgrade
public final boolean tryUpgrade()Description copied from class:LatchAttempt to upgrade a held shared latch into an exclusive latch. Upgrade fails if shared latch is held by more than one thread. If successful, caller must later call releaseExclusive instead of releaseShared.- Overrides:
tryUpgradein classLatch
-
toString
-
getPack
Returns the pack associated with this clutch, which should be shared to reduce the overall memory footprint.
-