Class LeafLock
- All Implemented Interfaces:
Serializable
These building blocks are notified from within a SignalTree while the
tree lock is held (through notifyObservers), and they also call back
into signal trees while updating their own state. If such a component held
its own lock while calling into a tree, two threads acquiring the component
lock and the tree lock in opposite orders would form an ABBA deadlock.
The invariant that avoids this is: a leaf lock is a leaf -- never
acquire a signal-tree lock while holding one. This class makes that invariant
enforceable: every held leaf lock is recorded in a thread-local, and
SignalTree asserts that none is held (ignoring reentrant tree
re-locks) before acquiring its lock. Because the tree cannot enumerate
arbitrary intrinsic monitors held by a caller, the leaf locks make themselves
known instead.
The check only runs under -ea (assertions), so it costs nothing in
production but turns a latent, timing-dependent ABBA into a deterministic
failure on any thread that violates the ordering.
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringDescribes the leaf locks currently held by the thread, for diagnostics.static booleanChecks whether the current thread holds any leaf lock.booleanChecks whether the current thread holds this specific leaf lock.lock()Acquires this lock, recording it as held by the current thread.
-
Constructor Details
-
LeafLock
Creates a new leaf lock with a descriptive name used in assertion messages.- Parameters:
name- a human-readable name for diagnostics, notnull
-
-
Method Details
-
lock
Acquires this lock, recording it as held by the current thread. Close the returned handle to release the lock, typically using try-with-resources.- Returns:
- a handle that releases the lock when closed, not
null
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()Checks whether the current thread holds this specific leaf lock.- Returns:
trueif the current thread holds this lock
-
isAnyHeldByCurrentThread
public static boolean isAnyHeldByCurrentThread()Checks whether the current thread holds any leaf lock.- Returns:
trueif at least one leaf lock is held
-
describeHeld
Describes the leaf locks currently held by the thread, for diagnostics.- Returns:
- a comma-separated list of held leaf-lock names
-