release method

void release()

Release a lock.

Release the lock that was previously acquired.

When the lock is released, locks waiting to be acquired can be acquired depending on the type of lock waiting and if other locks have been acquired.

A StateError is thrown if the mutex does not currently have a lock on it.

Implementation

void release() {
  if (_state == -1) {
    // Write lock released
    _state = 0;
  } else if (0 < _state) {
    // Read lock released
    _state--;
  } else if (_state == 0) {
    throw StateError('no lock to release');
  } else {
    assert(false, 'invalid state');
  }

  // Let all jobs that can now acquire a lock do so.

  while (_waiting.isNotEmpty) {
    final nextJob = _waiting.first;
    if (_jobAcquired(nextJob)) {
      _waiting.removeAt(0);
    } else {
      break; // no more can be acquired
    }
  }
}