C# Класс 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.

Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
Factory Func

Private Properties

Свойство Тип Описание
CancellationTokenCanceledEventHandler void
ReleaseOwnership void
TakeOwnership void

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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.

Описание методов

AssertHasLock() публичный Метод

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
Результат void

LockHeldByMe() публичный Метод

Determine if the lock is currently held by the calling thread.
public LockHeldByMe ( ) : bool
Результат bool

NonReentrantLock() публичный Метод

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. ///
Результат System

Release() публичный Метод

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
Результат void

Wait() публичный Метод

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.
Результат void

Описание свойств

Factory публичное статическое свойство

Shared factory for use in lazy initialization.
public static Func Factory
Результат Func