runLocked<R> method

R runLocked<R>(
  1. R action(), {
  2. Duration? timeout,
})

Run the given synchronous action under a mutex. The lock will return if the lock is gained. If a timeout occurs then a TimeoutException is thrown.

This function takes exclusive ownership of the mutex, executes action and then releases the mutex. It returns the value returned by action.

Warning: you can't combine runLocked with an asynchronous code.

Implementation

R runLocked<R>(R Function() action, {Duration? timeout}) {
   _logger.fine(() => 'Mutex::runLocked: $timeout');
  _lock(timeout: timeout);
  try {
  _logger.fine(() => 'Mutex: calling action');
    return action();
  } finally {
    _logger.fine(() => 'Mutex: calling _unlock');
    _unlock();
    _logger.fine(() => 'Mutex: unlocked');
  }
}