Sync::RWLock
Inherits Sync::Lockable / Reference / Object
A multiple readers and exclusive writer lock to protect critical sections.
Multiple fibers can acquire the shared lock (read) to allow some critical sections to run concurrently. However a single fiber can acquire the exclusive lock at a time to protect a single critical section to ever run in parallel. When the lock has been acquired in exclusive mode, no other fiber can lock it, be it in shared or exclusive mode.
For example, the shared mode can allow to read one or many resources, albeit the resources must be safe to be accessed in such manner, while the exclusive mode allows to safely replace or mutate the resources with the guarantee that nothing else is accessing said resources.
The implementation doesn't favor readers or writers in particular.
NOTE: Consider Shared(T) to protect a value T with a RWLock.
Constructors
Instance methods
Acquires the shared (read) lock.
Multiple fibers can acquire the shared (read) lock at the same time. Blocks the calling fiber if the exclusive (write) lock is held.
WARNING: the shared lock is technically reentrant but any attempt to relock read can result in a deadlock if another fiber is trying to lock write!
Acquires the exclusive (write) lock. Blocks the calling fiber while the shared or exclusive (write) lock is held.
Acquires the shared (read) lock for the duration of the block.
Multiple fibers can acquire the shared (read) lock at the same time. The block will never run concurrently to an exclusive (write) lock.
WARNING: the shared lock is technically reentrant but any attempt to relock read can result in a deadlock if another fiber is trying to lock write!
Tries to acquire the shared (read) lock without blocking. Returns true when acquired, otherwise returns false immediately.
Tries to acquire the exclusive (write) lock without blocking. Returns true when acquired, otherwise returns false immediately.
Releases the shared (read) lock.
Every fiber that locked must unlock to actually release the reader lock (so a writer can lock). If a fiber locked multiple times (reentrant behavior) then it must unlock that many times.
Acquires the exclusive (write) lock for the duration of the block.
Only one fiber can acquire the exclusive (write) lock at the same time. The block will never run concurrently to a shared (read) lock or another exclusive (write) lock.