A synchronization lock that allows concurrent access to multiple readers, but limits access to a single writer. More...
#include <rw/sync/RWReadersWriterLock.h>
Public Types | |
typedef RWTLockGuard< RWReadersWriterLock > | LockGuard |
typedef RWTReadLockGuard< RWReadersWriterLock > | ReadLockGuard |
typedef RWTReadUnlockGuard< RWReadersWriterLock > | ReadUnlockGuard |
typedef RWTTryLockGuard< RWReadersWriterLock > | TryLockGuard |
typedef RWTTryReadLockGuard< RWReadersWriterLock > | TryReadLockGuard |
typedef RWTTryWriteLockGuard< RWReadersWriterLock > | TryWriteLockGuard |
typedef RWTUnlockGuard< RWReadersWriterLock > | UnlockGuard |
typedef RWTWriteLockGuard< RWReadersWriterLock > | WriteLockGuard |
typedef RWTWriteUnlockGuard< RWReadersWriterLock > | WriteUnlockGuard |
Public Member Functions | |
RWReadersWriterLock (RWCancellationState state=0) | |
~RWReadersWriterLock () | |
void | acquire () |
RWWaitStatus | acquire (unsigned long) |
void | acquireRead () |
RWWaitStatus | acquireRead (unsigned long) |
void | acquireWrite () |
RWWaitStatus | acquireWrite (unsigned long) |
void | release () |
bool | tryAcquire () |
bool | tryAcquireRead () |
bool | tryAcquireWrite () |
Public Member Functions inherited from RWSynchObject | |
void | disableCancellation () |
void | enableCancellation (RWCancellationState) |
bool | isCancellationEnabled () const |
void | setCancellation (RWCancellationState) |
Private Member Functions | |
RWReadersWriterLock (const RWReadersWriterLock &) | |
RWReadersWriterLock & | operator= (const RWReadersWriterLock &) |
Additional Inherited Members | |
Protected Member Functions inherited from RWSynchObject | |
RWSynchObject (RWCancellationState state=0) | |
RWSynchObject (RWStaticCtor) | |
RWSynchObject (const RWSynchObject &second) | |
RWSynchObject & | operator= (const RWSynchObject &second) |
void | testCancellation () |
Related Functions inherited from RWSynchObject | |
#define | RW_CANCELLATION_DISABLED |
#define | RW_CANCELLATION_ENABLED |
typedef void(* | RWCancellationState) () |
void | rwServiceCancellation (void) |
RWReadersWriterLock is a multiple-readers, single-writer synchronization lock. With this type of lock, multiple readers are allowed concurrent access to the protected critical section. However, exclusive access is granted to writers: a reader and a writer cannot both enter the critical section, and only one writer at a time is granted access. A writer that attempts to acquire the lock blocks waiting for readers to exit the critical section. Note that because this lock favors writers over readers, the writer gets priority over any readers that try to access the lock after the time that the writer attempts to acquire it. A reader attempting to access the lock acquires it only after all writers attempting access have acquired and released the lock.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
Predefined type for compatible guard.
RWReadersWriterLock::RWReadersWriterLock | ( | RWCancellationState | state = 0 | ) |
Creates and initializes an RWReadersWriterLock. The thread cancellation state of the object is initialized to state. Can throw RWTHRInternalError.
RWReadersWriterLock::~RWReadersWriterLock | ( | ) |
Recovers any system resources used to implement the RWReadersWriterLock. Can throw RWTHRInternalError.
|
private |
Copy construction prohibited.
|
inline |
Calls acquireWrite(). Provided for compatibility with simple mutex locks. Possible exceptions include RWCancellation and RWTHRInternalError.
|
inline |
Returns the result of calling acquireWrite(milliseconds). Provided for compatibility with simple mutex locks. Possible exceptions include RWCancellation and RWTHRInternalError.
void RWReadersWriterLock::acquireRead | ( | ) |
Acquires the lock as a reader. If there are writers waiting for the lock, the calling thread blocks until all readers currently in the protected code exit and until all waiting writers have acquired and left the protected code. At that time, the thread is given access to the protected code concurrently with other readers waiting on the lock.
This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.
This method returns immediately on success for single-threaded builds. A debug assertion occurs or an RWTHRInternalError is thrown on failure.
RWWaitStatus RWReadersWriterLock::acquireRead | ( | unsigned | long | ) |
Attempts to acquire the lock as a reader. If there are writers waiting for the lock, the calling thread blocks until all readers currently in the protected code exit and until all waiting writers have acquired and left the protected code. At that time, the thread is given access to the protected code concurrently with other readers waiting on the lock.
If the lock cannot be acquired within the time specified, the function returns RW_THR_TIMEOUT.
This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.
This method returns immediately in single-threaded builds.
void RWReadersWriterLock::acquireWrite | ( | ) |
Acquires the lock as a writer. The calling thread is given priority access to the lock over all readers which have not yet acquired the lock. If readers already have acquired the lock, but have not released it, or another writer owns the lock, the call blocks until the lock is available. This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.
This method returns immediately on success for single-threaded builds. A debug assertion occurs or an RWTHRInternalError is thrown on failure.
RWWaitStatus RWReadersWriterLock::acquireWrite | ( | unsigned | long | ) |
Attempts to acquire the lock as a writer. The calling thread isgiven priority access to the lock over all readers which have not yet acquired the lock. If readers already have acquired the lock, but have not released it, or another writer owns the lock, the call blocks until the lock is available. If the lock cannot be acquired within the time specified, the function returns RW_THR_TIMEOUT.
This function throws an RWCancellation object if the lock has cancellation detection enabled and a runnable containing the calling thread has been canceled. Other possible exceptions include RWTHRInternalError.
This method returns immediately in single-threaded builds.
|
private |
Assignment prohibited.
void RWReadersWriterLock::release | ( | ) |
Releases the lock to one writer waiting to access the protected code. If there are no writers blocked waiting for the lock, all readers blocked on the lock are given concurrent access to the protected code. Possible exceptions include RWTHRInternalError.
|
inline |
Calls tryAcquireWrite(). Provided for compatibility with the interface used by simple mutex locks. Possible exceptions include RWCancellation and RWTHRInternalError.
bool RWReadersWriterLock::tryAcquireRead | ( | ) |
Attempts to acquire the lock as a reader. If there are no writers either waiting for or owning the lock, the calling thread acquires the lock, and this function returns true
. Otherwise, this function returns false
without acquiring the lock. Possible exceptions include RWCancellation and RWTHRInternalError.
bool RWReadersWriterLock::tryAcquireWrite | ( | ) |
Attempts to acquire the lock as a writer. If no thread currently owns the lock, the calling thread is given ownership of the lock and this function returns true
. Otherwise, this function returns false
without acquiring the lock. Possible exceptions include RWCancellation and RWTHRInternalError.
Copyright © 2021 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved. |