private void NotifyIfNeeded() { if (q.Count > 0 && units >= q.First.Value) { SyncUtils.Notify(mutex, q.First); } }
public bool Acquire(int requestedUnits, int timeout) { lock (mutex) { if (units >= requestedUnits && q.Count == 0) { units -= requestedUnits; return(true); } var node = q.AddLast(requestedUnits); TimeoutInstant limit = new TimeoutInstant(timeout); do { try { SyncUtils.Wait(mutex, node, limit.Remaining); } catch (ThreadInterruptedException) { q.Remove(node); NotifyIfNeeded(); throw; } if (node == q.First && units >= requestedUnits) { q.RemoveFirst(); units -= requestedUnits; NotifyIfNeeded(); return(true); } if (limit.IsTimeout) { q.Remove(node); NotifyIfNeeded(); return(false); } } while (true); } }