Class LeafLock

java.lang.Object
com.vaadin.flow.signals.impl.LeafLock
All Implemented Interfaces:
Serializable

public class LeafLock extends Object implements Serializable
A component-level ("leaf") lock used to protect small pieces of in-memory state in signal-reactivity building blocks such as effects, cached signals and usage trackers.

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:
  • Constructor Details

    • LeafLock

      public LeafLock(String name)
      Creates a new leaf lock with a descriptive name used in assertion messages.
      Parameters:
      name - a human-readable name for diagnostics, not null
  • Method Details

    • lock

      public LeafLock.Handle 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:
      true if the current thread holds this lock
    • isAnyHeldByCurrentThread

      public static boolean isAnyHeldByCurrentThread()
      Checks whether the current thread holds any leaf lock.
      Returns:
      true if at least one leaf lock is held
    • describeHeld

      public static String describeHeld()
      Describes the leaf locks currently held by the thread, for diagnostics.
      Returns:
      a comma-separated list of held leaf-lock names