Enhanced C#
Language of your choice: library documentation
Public static fields | Public Member Functions | List of all members
Loyc.Threading.TinyReaderWriterLock Struct Reference

A fast, tiny 4-byte lock to support multiple readers or a single writer. Designed for low-contention, high-performance scenarios where reading is common and writing is rare. More...


Source file:

Remarks

A fast, tiny 4-byte lock to support multiple readers or a single writer. Designed for low-contention, high-performance scenarios where reading is common and writing is rare.

Do not use the default constructor! Use TinyReaderWriterLock.New as the initial value of the lock.

Recursive locking is not supported: the same lock cannot be acquired twice for writing on the same thread, nor can a reader lock be acquired after the writer lock was acquired on the same thread. If you make either of these mistakes, the lock will throw an NotSupportedException.

You also cannot acquire a read lock followed recursively by a write lock, either. Attempting to do so will self-deadlock the thread, bacause TinyReaderWriterLock does not track the identity of each reader and is not aware that it is waiting for the current thread to finish reading.

However, multiple reader locks can be acquired on the same thread, just as multiple reader locks can be acquired by different threads.

Make sure you call ExitRead() or ExitWrite() in a finally block! When compiled in debug mode, TinyReaderWriterLock will make sure you don't mix up ExitRead() and ExitWrite().

The range of Thread.CurrentThread.ManagedThreadId is undocumented. I have assumed they don't use IDs close to int.MinValue, so I use values near int.MinValue to indicate the number of readers holding the lock.

Public static fields

static readonly TinyReaderWriterLock New = new TinyReaderWriterLock { _user = NoUser }
 

Public Member Functions

void EnterReadLock ()
 Acquires the lock to protect read access to a shared resource. More...
 
void ExitReadLock ()
 Releases a read lock that was acquired with EnterRead(). More...
 
void EnterWriteLock ()
 Acquires the lock to protect write access to a shared resource. More...
 
void EnterWriteLock (int threadID)
 Acquires the lock to protect write access to a shared resource. More...
 
void ExitWriteLock ()
 Releases a write lock that was acquired with EnterWrite(). More...
 

Member Function Documentation

◆ EnterReadLock()

void Loyc.Threading.TinyReaderWriterLock.EnterReadLock ( )
inline

Acquires the lock to protect read access to a shared resource.

◆ EnterWriteLock() [1/2]

void Loyc.Threading.TinyReaderWriterLock.EnterWriteLock ( )
inline

Acquires the lock to protect write access to a shared resource.

◆ EnterWriteLock() [2/2]

void Loyc.Threading.TinyReaderWriterLock.EnterWriteLock ( int  threadID)
inline

Acquires the lock to protect write access to a shared resource.

Parameters
threadIDReports the value of Thread.CurrentThread.ManagedThreadId

◆ ExitReadLock()

void Loyc.Threading.TinyReaderWriterLock.ExitReadLock ( )
inline

Releases a read lock that was acquired with EnterRead().

◆ ExitWriteLock()

void Loyc.Threading.TinyReaderWriterLock.ExitWriteLock ( )
inline

Releases a write lock that was acquired with EnterWrite().