private void CheckWaitingReads() { if (!readLocked.HasValue && waitingReads.Count > 0) { WaitingRead wr = waitingReads.Peek(); if (items.Count >= wr.desiredItems || (eofSignaled && !writeLocked.HasValue && waitingWrites.Count == 0)) { waitingReads.Dequeue(); readLocked = Math.Min(wr.desiredItems, items.Count); wr.k.PostResult(new AcquireReadSucceeded <T>(readPtr, readLocked.Value == 0 ? ImmutableList <T> .Empty : items.GetRange(0, readLocked.Value))); if (wr.ctr.HasValue) { wr.ctr.Value.PostDispose(); } if (readLocked.Value == 0) { while (waitingReads.Count > 0) { WaitingRead wr2 = waitingReads.Dequeue(); wr2.k.PostResult(new AcquireReadSucceeded <T>(readPtr, ImmutableList <T> .Empty)); if (wr2.ctr.HasValue) { wr2.ctr.Value.PostDispose(); } } } } } }
private void CompleteWork(int workerId) { lock (syncRoot) { busyWorkers = busyWorkers.Remove(workerId); idleWorkers = idleWorkers.Add(workerId); if (waitingWorkItems.Count > 0) { WaitingWorkItem wwi = waitingWorkItems.Dequeue(); if (wwi.work is Func <int, Task> ) { wwi.k.PostResult(StartWorker((Func <int, Task>)wwi.work)); } else { wwi.k.PostResult(StartWorker((Func <int, CancellationToken, Task>)wwi.work, wwi.ctoken)); } if (wwi.ctr.HasValue) { wwi.ctr.Value.Dispose(); } } } }
public void Leave() { lock (syncRoot) { --referenceCount; if (referenceCount == 0) { while (waiters.Count > 0) { Waiter waiter = waiters.Dequeue(); waiter.k.PostResult(true); if (waiter.ctr.HasValue) { waiter.ctr.Value.Dispose(); } } } } }
private void CheckWaitingWrites() { if (!writeLocked.HasValue && waitingWrites.Count > 0) { WaitingWrite ww = waitingWrites.Peek(); System.Diagnostics.Debug.Assert(capacity.HasValue, "If there is no capacity limit, there should never be any waiting writes"); // ReSharper disable once PossibleInvalidOperationException if (capacity.Value - items.Count >= ww.desiredSpace) { waitingWrites.Dequeue(); writeLocked = ww.desiredSpace; ww.k.PostResult(new AcquireWriteSucceeded(readPtr + items.Count, ww.desiredSpace)); if (ww.ctr.HasValue) { ww.ctr.Value.PostDispose(); } } } }