Class LeafLock
A non-reentrant lock for short critical sections that never nest and are never held across other work.
Namespace: Singulink.Threading
Assembly: Singulink.Threading.dll
Syntax
public sealed class LeafLock
Remarks
The lock is entered with EnterGuard(), which returns a guard that leaves the lock when it is disposed. The guard is a struct, so entering and leaving the lock in a using statement allocates nothing.
The lock is a leaf: code that holds it takes no other lock and does no other work that can block, such as I/O or waiting on another thread. It is not reentrant: entering it on a thread that already holds it throws a LockRecursionException instead of deadlocking or entering it a second time. Code that requires the lock can call EnsureHeld(), and code that must not run under it, such as a call that may take another lock or a wait, can call EnsureNotHeld(). These checks are always on, in release builds as well as debug builds; each costs about as much as reading IsHeldByCurrentThread.
The lock has thread affinity: a guard must be disposed on the thread that entered the lock, so it must not be held across an await. The lock is built on Monitor over a private object, so lock statements in other code cannot enter it.
Properties
| Name | Description |
|---|---|
| IsHeldByCurrentThread | Gets a value indicating whether the current thread holds the lock. |
Methods
| Name | Description |
|---|---|
| EnsureHeld() | Throws an exception if the current thread does not hold the lock. |
| EnsureNotHeld() | Throws an exception if the current thread holds the lock. |
| EnterGuard() | Enters the lock and returns a guard that leaves it when it is disposed. |