objectSynchronized<T> function
Execute computation
when lock is available. Only one block can run while
the lock is retained. Any object can be a lock, locking is based on identity.
Implementation
Future<T> objectSynchronized<T>(
Object monitor, FutureOr<T> Function() computation,
{Duration? timeout}) async {
// Make any object a lock object
var lockImpl = objectMakeLock(monitor);
try {
return await lockImpl.synchronized(() async {
var result = computation();
if (result is Future) {
result = await result;
}
return result;
}, timeout: timeout);
} finally {
// Clean up if unlocked
if (!lockImpl.locked) {
cleanUpLock(monitor);
}
}
}