www.digitalmars.com

D Programming Language 2.0

Last update Tue Jan 1 10:14:39 2013

core.sync.rwmutex

The read/write mutex module provides a primitive for maintaining shared read access and mutually exclusive write access.

License:
Boost License 1.0

Authors:
Sean Kelly

Source:
core/sync/rwmutex.d

class ReadWriteMutex;
This class represents a mutex that allows any number of readers to enter, but when a writer enters, all other readers and writers are blocked.

Please note that this mutex is not recursive and is intended to guard access to data only. Also, no deadlock checking is in place because doing so would require dynamic memory allocation, which would reduce performance by an unacceptable amount. As a result, any attempt to recursively acquire this mutex may well deadlock the caller, particularly if a write lock is acquired while holding a read lock, or vice-versa. In practice, this should not be an issue however, because it is uncommon to call deeply into unknown code while holding a lock that simply protects data.

enum Policy;
Defines the policy used by this mutex. Currently, two policies are defined.

The first will queue writers until no readers hold the mutex, then pass the writers through one at a time. If a reader acquires the mutex while there are still writers queued, the reader will take precedence.

The second will queue readers if there are any writers queued. Writers are passed through one at a time, and once there are no writers present, all queued readers will be alerted.

Future policies may offer a more even balance between reader and writer precedence.

PREFER_READERS
Readers get preference. This may starve writers.

PREFER_WRITERS
Writers get preference. This may starve readers.

this(Policy policy = Policy.PREFER_WRITERS);
Initializes a read/write mutex object with the supplied policy.

Parameters:
Policy policy The policy to use.

Throws:
SyncException on error.

@property Policy policy();
Gets the policy used by this mutex.

Returns:
The policy used by this mutex.

@property Reader reader();
Gets an object representing the reader lock for the associated mutex.

Returns:
A reader sub-mutex.

@property Writer writer();
Gets an object representing the writer lock for the associated mutex.

Returns:
A writer sub-mutex.

class Reader: object.Object.Monitor;
This class can be considered a mutex in its own right, and is used to negotiate a read lock for the enclosing mutex.

this();
Initializes a read/write mutex reader proxy object.

@trusted void lock();
Acquires a read lock on the enclosing mutex.

@trusted void unlock();
Releases a read lock on the enclosing mutex.

bool tryLock();
Attempts to acquire a read lock on the enclosing mutex. If one can be obtained without blocking, the lock is acquired and true is returned. If not, the lock is not acquired and false is returned.

Returns:
true if the lock was acquired and false if not.

class Writer: object.Object.Monitor;
This class can be considered a mutex in its own right, and is used to negotiate a write lock for the enclosing mutex.

this();
Initializes a read/write mutex writer proxy object.

@trusted void lock();
Acquires a write lock on the enclosing mutex.

@trusted void unlock();
Releases a write lock on the enclosing mutex.

bool tryLock();
Attempts to acquire a write lock on the enclosing mutex. If one can be obtained without blocking, the lock is acquired and true is returned. If not, the lock is not acquired and false is returned.

Returns:
true if the lock was acquired and false if not.