runUnderDirectoryLock<T> function

Future<T> runUnderDirectoryLock<T>(
  1. Directory directory,
  2. Future<T> callback(), {
  3. Duration? timeout,
  4. Logger? logger,
})

Run callback with this Dart process having exclusive access to directory.

Note multiple isolates and isolate groups in the same Dart process share locks, so these will be able to enter the exclusive section simultanously.

If provided, the timeout parameter determines how long to retry before giving up. If the timeout is exceeded a TimeoutException is thrown.

If provided, the logger streams information on the the locking status, and also streams error messages.

Implementation

Future<T> runUnderDirectoryLock<T>(
  Directory directory,
  Future<T> Function() callback, {
  Duration? timeout,
  Logger? logger,
}) async {
  const lockFileName = '.lock';
  final lockFile = _fileInDir(directory, lockFileName);
  return _runUnderFileLock(
    lockFile,
    callback,
    timeout: timeout,
    logger: logger,
  );
}