withLockAsync method
creates a lock file and then calls fn
once fn returns the lock is released.
If waiting is passed it will be used to write
a log message to the console.
@Throwing(ArgumentError) @Throwing(FormatException) @Throwing(LockException) @Throwing(TouchException)
Implementation
Future<void> withLockAsync(
Future<void> Function() fn, {
String? waiting,
}) async {
var lockHeld = false;
try {
verbose(() => 'lockcount = $_lockCountForName');
_createLockPath();
if (_lockCountForName > 0 || (await _takeLock(waiting))) {
lockHeld = true;
incLockCount;
await fn();
}
} finally {
if (lockHeld) {
await _releaseLock();
}
// just in case an async exception can be thrown
// I'm uncertain if this is a reality.
lockHeld = false;
}
}