tryWait method

  1. @useResult
Future<bool> tryWait(
  1. Duration timeout
)

Releases the lock and waits for a notification.
Upon receiving the notification, the lock will be reacquired. Accordingly, upon exiting the method, the locked code will be entered.

Returns true if the timeout has not expired; otherwise, returns false.

Implementation

@useResult
Future<bool> tryWait(Duration timeout) async {
  final started = _stopwatch.elapsedMicroseconds;
  if (timeout.isNegative) {
    throw ArgumentError.value(timeout, 'timeout', 'Must not be negative');
  }

  await lock.release();
  await _queue.enqueue();
  await lock.reacquire();
  return _stopwatch.elapsedMicroseconds - started <= timeout.inMicroseconds;
}