Ion
|
The ReadWriteLock class defines a non-promotable lock that is very fast when only readers try to obtain the lock, but slower than a Mutex when there are writers. More...
#include "readwritelock.h"
Public Member Functions | |
ReadWriteLock () | |
The semaphore is initialized to 1 so that the first Wait() succeeds. More... | |
~ReadWriteLock () | |
void | LockForRead () |
Locks the ReadWriteLock for reading. More... | |
void | UnlockForRead () |
Unlocks the ReadWriteLock for reading, which will allow writers to obtain a lock once the last reader has exited. More... | |
void | LockForWrite () |
Locks the ReadWriteLock for writing. More... | |
void | UnlockForWrite () |
Unlocks the ReadWriteLock for writing. More... | |
int | GetReaderCount () const |
Returns the number of readers in this lock. More... | |
int | GetWriterCount () const |
Returns the number of writers in this lock. More... | |
The ReadWriteLock class defines a non-promotable lock that is very fast when only readers try to obtain the lock, but slower than a Mutex when there are writers.
A ReadWriteLock allows any number of readers to enter the lock as long as there are no writers, but each writer obtains exclusive access to the lock. At a high level, a ReadWriteLock behaves like an atomic integer under no or reader-only contention, and like a Mutex when there are any writers.
This implementation is based on sections 4.2.5 of The Little Book of Semaphores by Allen B. Downey. http://greenteapress.com/semaphores/downey08semaphores.pdf
This particular implementation has the following behaviors:
Definition at line 51 of file readwritelock.h.
ion::base::ReadWriteLock::ReadWriteLock | ( | ) |
The semaphore is initialized to 1 so that the first Wait() succeeds.
Definition at line 24 of file readwritelock.cc.
ion::base::ReadWriteLock::~ReadWriteLock | ( | ) |
Definition at line 27 of file readwritelock.cc.
|
inline |
Returns the number of readers in this lock.
Definition at line 71 of file readwritelock.h.
|
inline |
Returns the number of writers in this lock.
Definition at line 73 of file readwritelock.h.
void ion::base::ReadWriteLock::LockForRead | ( | ) |
Locks the ReadWriteLock for reading.
This will block other readers if there is a writer is in the lock, and will cause LockForWrite() to block until the last reader that has entered the lock calls UnlockForRead().
Definition at line 29 of file readwritelock.cc.
References ion::port::Mutex::Lock(), ion::port::Mutex::Unlock(), and ion::port::Semaphore::Wait().
void ion::base::ReadWriteLock::LockForWrite | ( | ) |
Locks the ReadWriteLock for writing.
This will cause any callers of LockFor*() to block until the caller calls UnlockForWrite().
Definition at line 48 of file readwritelock.cc.
References ion::port::Mutex::Lock(), and ion::port::Semaphore::Wait().
void ion::base::ReadWriteLock::UnlockForRead | ( | ) |
Unlocks the ReadWriteLock for reading, which will allow writers to obtain a lock once the last reader has exited.
Definition at line 42 of file readwritelock.cc.
References ion::port::Semaphore::Post().
void ion::base::ReadWriteLock::UnlockForWrite | ( | ) |
Unlocks the ReadWriteLock for writing.
This will allow other callers to obtain a read or write lock.
Definition at line 54 of file readwritelock.cc.
References ion::port::Semaphore::Post(), and ion::port::Mutex::Unlock().