/** * Try to cancel an async acquire request */ private void AcquireCancellationHandler(object _acquireNode, bool canceling) { LinkedListNode <AsyncAcquire> acquireNode = (LinkedListNode <AsyncAcquire>)_acquireNode; AsyncAcquire acquire = acquireNode.Value; bool complete = false; List <AsyncAcquire> satisfied = null; // To access shared mutable state we must acquire the lock lock (theLock) { /** * Here, the async request can be already satisfied or cancelled. */ if (!acquire.done) { // Remove the async acquire request from queue and mark it as done. asyncAcquires.Remove(acquireNode); complete = acquire.done = true; // If after removing the async acquire is possible to satisfy any // pending async acquire(s) do it if (asyncAcquires.Count > 0 && permits >= asyncAcquires.First.Value.acquires) { satisfied = SatisfyPendingAsyncAcquires(); } } } // If we cancelled the async acquire, release the resources associated with it, // and complete the underlying task. if (complete) { // Complete any satisfied async acquires if (satisfied != null) { CompleteSatisfiedAsyncAcquires(satisfied); } // Dispose the resources associated with the cancelled async acquire acquire.Dispose(canceling); // Complete the TaskCompletionSource to RanToCompletion with false (timeout) // or Canceled final state (cancellation). if (canceling) { acquire.SetCanceled(); // cancelled } else { acquire.SetResult(false); // timeout } } }
/** * Try to cancel an async acquire request */ private void AcquireCancellationHandler(object _acquireNode, bool canceling) { LinkedListNode <AsyncAcquire> acquireNode = (LinkedListNode <AsyncAcquire>)_acquireNode; AsyncAcquire acquire = acquireNode.Value; if (acquire.TryLock()) { List <AsyncAcquire> satisfied = null; // To access shared mutable state we must acquire the lock lock (theLock) { if (acquireNode.List != null) { asyncAcquires.Remove(acquireNode); } if (asyncAcquires.Count > 0 && permits >= asyncAcquires.First.Value.acquires) { satisfied = SatisfyPendingAsyncAcquires(); } } // Complete the satisfied async acquires CompleteSatisfiedAsyncAcquires(satisfied); // Release the resources associated with the async acquire. acquire.Dispose(canceling); // Complete the TaskCompletionSource to RanToCompletion (timeout) // or Canceled final state. if (canceling) { acquire.SetCanceled(); } else { acquire.SetResult(false); } } }