ReadWriteMutex class
Mutual exclusion that supports read and write locks.
Multiple read locks can be simultaneously acquired, but at most only one write lock can be acquired at any one time.
Protecting critical code
The protectWrite and protectRead are convenience methods for acquiring locks and releasing them. Using them will ensure the locks are always released after use.
Create the mutex:
m = ReadWriteMutex();
Code protected by a write lock:
await m.protectWrite(() {
// critical write section
});
Other code can be protected by a read lock:
await m.protectRead(() {
// critical read section
});
Explicitly managing locks
Alternatively, the locks can be explicitly acquired and managed. In this situation, the program is responsible for releasing the locks after they have been used. Failure to release the lock will prevent other code for ever acquiring a lock.
Create the mutex:
m = ReadWriteMutex();
Some code can acquire a write lock:
await m.acquireWrite();
try {
// critical write section
assert(m.isWriteLocked);
} finally {
m.release();
}
Other code can acquire a read lock.
await m.acquireRead();
try {
// critical read section
assert(m.isReadLocked);
} finally {
m.release();
}
The current implementation lets locks be acquired in first-in-first-out order. This ensures there will not be any lock starvation, which can happen if some locks are prioritised over others. Submit a feature request issue, if there is a need for another scheduling algorithm.
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isLocked → bool
-
Indicates if a lock (read or write) has been acquired and not released.
no setter
- isReadLocked → bool
-
Indicates if one or more read locks has been acquired and not released.
no setter
- isWriteLocked → bool
-
Indicates if a write lock has been acquired and not released.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
acquireRead(
) → Future - Acquire a read lock
-
acquireWrite(
) → Future - Acquire a write lock
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
protectRead<
T> (Future< T> criticalSection()) → Future<T> - Convenience method for protecting a function with a read lock.
-
protectWrite<
T> (Future< T> criticalSection()) → Future<T> - Convenience method for protecting a function with a write lock.
-
release(
) → void - Release a lock.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited