C# Class Roslyn.Utilities.NonReentrantLock

A lightweight mutual exclusion object which supports waiting with cancellation and prevents recursion (i.e. you may not call Wait if you already hold the lock)

The NonReentrantLock provides a lightweight mutual exclusion class that doesn't use Windows kernel synchronization primitives.

The implementation is distilled from the workings of T:System.Threading.SemaphoreSlim The basic idea is that we use a regular sync object (Monitor.Enter/Exit) to guard the setting of an 'owning thread' field. If, during the Wait, we find the lock is held by someone else then we register a cancellation callback and enter a "Monitor.Wait" loop. If the cancellation callback fires, then it "pulses" all the waiters to wake them up and check for cancellation. Waiters are also "pulsed" when leaving the lock.

All public members of NonReentrantLock are thread-safe and may be used concurrently from multiple threads.

Datei anzeigen Open project: Runt-Editor/Runt Class Usage Examples

Public Properties

Property Type Description
Factory Func

Private Properties

Property Type Description
CancellationTokenCanceledEventHandler void
ReleaseOwnership void
TakeOwnership void

Public Methods

Method Description
AssertHasLock ( ) : void

Throw an exception if the lock is not held by the calling thread.

LockHeldByMe ( ) : bool

Determine if the lock is currently held by the calling thread.

NonReentrantLock ( bool useThisInstanceForSynchronization = false ) : System

Constructor.

Release ( ) : void

Exit the mutual exclusion.

The calling thread must currently hold the lock.

Wait ( CancellationToken cancellationToken = default(CancellationToken) ) : void

Blocks the current thread until it can enter the NonReentrantLock, while observing a T:System.Threading.CancellationToken.

Recursive locking is not supported. i.e. A thread may not call Wait successfully twice without an intervening Release.

Private Methods

Method Description
CancellationTokenCanceledEventHandler ( object obj ) : void

Callback executed when a cancellation token is canceled during a Wait.

ReleaseOwnership ( ) : void

Release ownership of the lock. The lock must already be held by the calling thread.

TakeOwnership ( ) : void

Take ownership of the lock (by the calling thread). The lock may not already be held by any other code.

Method Details

AssertHasLock() public method

Throw an exception if the lock is not held by the calling thread.
The lock is not currently held by the calling thread.
public AssertHasLock ( ) : void
return void

LockHeldByMe() public method

Determine if the lock is currently held by the calling thread.
public LockHeldByMe ( ) : bool
return bool

NonReentrantLock() public method

Constructor.
public NonReentrantLock ( bool useThisInstanceForSynchronization = false ) : System
useThisInstanceForSynchronization bool If false (the default), then the class /// allocates an internal object to be used as a sync lock. /// If true, then the sync lock object will be the NonReentrantLock instance itself. This /// saves an allocation but a client may not safely further use this instance in a call to /// Monitor.Enter/Exit or in a "lock" statement. ///
return System

Release() public method

Exit the mutual exclusion.
The calling thread must currently hold the lock.
The lock is not currently held by the calling thread.
public Release ( ) : void
return void

Wait() public method

Blocks the current thread until it can enter the NonReentrantLock, while observing a T:System.Threading.CancellationToken.
Recursive locking is not supported. i.e. A thread may not call Wait successfully twice without an intervening Release.
was /// canceled. The caller already holds the lock
public Wait ( CancellationToken cancellationToken = default(CancellationToken) ) : void
cancellationToken System.Threading.CancellationToken The token to /// observe.
return void

Property Details

Factory public_oe static_oe property

Shared factory for use in lazy initialization.
public static Func Factory
return Func