RwLock 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 withWriteLock and withReadLock are convenience methods for acquiring locks and releasing them. Using them will ensure the locks are always released after use.

Create the mutex:

m = RwLock();

Code protected by a write lock:

await m.withWriteLock(() {
   // critical write section
});

Other code can be protected by a read lock:

await m.withReadLock(() {
    // 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 = RwLock();

Some code can acquire a write lock:

await m.write();
try {
  // critical write section
  assert(m.isWriteLocked);
} finally {
  m.release();
}

Other code can acquire a read lock.

await m.read();
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 prioritized over others.

Constructors

RwLock()

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

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
read() Future
Acquire a read lock
release() → void
Release a lock.
toString() String
A string representation of this object.
inherited
withReadLock<T>(Future<T> criticalSection()) Future<T>
Convenience method for protecting a function with a read lock.
withWriteLock<T>(Future<T> criticalSection()) Future<T>
Convenience method for protecting a function with a write lock.
write() Future
Acquire a write lock

Operators

operator ==(Object other) bool
The equality operator.
inherited