tryAcquire method

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

Tries to acquire the lock and returns true if the lock was acquired before the timeout expires, otherwise the acquisition attempt is canceled and false is returned.

Parameters:

  • timeout: The period of time during which an attempt to acquire the lock will be performed.

Implementation

@override
@useResult
Future<bool> tryAcquire(Duration timeout) async {
  if (_owner == null) {
    _owner = Zone.current;
    _count++;
    return true;
  }

  if (_owner == Zone.current) {
    _count++;
    return true;
  }

  final isSuccess = await _entranceQueue.enqueue(timeout);
  if (isSuccess) {
    _owner = Zone.current;
    _count++;
  }

  return isSuccess;
}