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}) {
  _lock(timeout: timeout);
  try {
    return action();
  } finally {
    _unlock();
  }
}