synchronized<T> method

  1. @override
Future<T> synchronized<T>(
  1. FutureOr<T> computation(), {
  2. Duration? timeout,
})
override

Executes computation when lock is available.

Only one asynchronous block can run while the lock is retained.

If timeout is specified, it will try to grab the lock and will not call the computation callback and throw a TimeoutExpection if the lock cannot be grabbed in the given duration.

Implementation

@override
Future<T> synchronized<T>(
  FutureOr<T> Function() computation, {
  Duration? timeout,
}) async {
  FutureOr<T> runWithLocks(Iterator<Lock> iterator) {
    if (!iterator.moveNext()) {
      return computation();
    } else {
      final currentLock = iterator.current;
      return currentLock.synchronized(
        () => runWithLocks(iterator),
        timeout: timeout,
      );
    }
  }

  return runWithLocks(_locks.iterator);
}