synchronized<T> method

Future<T> synchronized<T>(
  1. Future<T> action()
)

Executes action sequentially to guarantee concurrency-safe access to metadata and IFDs.

Implementation

Future<T> synchronized<T>(Future<T> Function() action) {
  if (_isClosed) {
    throw StateError('Cannot access a closed SvsFile');
  }

  // Re-entrant call from within an active synchronized block on this SvsFile:
  if (Zone.current[_syncZoneKey] == this) {
    return action();
  }

  final prev = _lastOp;
  final completer = Completer<void>();
  _lastOp = completer.future;

  return Future.sync(() async {
    if (prev != null) {
      try {
        await prev;
      } catch (_) {}
    }

    if (_isClosed) {
      throw StateError('Cannot access a closed SvsFile');
    }

    await _acquireSpecificHandle(raf);
    try {
      return await runZoned(
        () => action(),
        zoneValues: {_syncZoneKey: this},
      );
    } finally {
      _releaseHandle(raf);
    }
  }).whenComplete(() {
    completer.complete();
  });
}