tryAcquire method

  1. @override
  2. @useResult
Future<bool> tryAcquire(
  1. Duration timeout
)
override

Tries to acquire a permit from this semaphore and waits until the specified timeout expires.
If the semaphore is locked and timeout is zero (or not specified), false is returned.
If the timeout expires before the semaphore is unlocked, then returns false.
If permit was acquired, returns true.

Implementation

@override
@useResult
Future<bool> tryAcquire(Duration timeout) {
  if (timeout.isNegative) {
    throw ArgumentError.value(
        timeout, 'timeout', 'Timeout must not be negative');
  }

  if (!_isLocked) {
    _isLocked = true;
    return _true;
  }

  return _waitQueue.enqueue(timeout);
}