/** * 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 } } }
/** * Synchronous interface implemented using the asynchronous TAP interface. */ /** * Try to cancel an asynchronous acquire request identified by its task. * * Note: This method is needed to implement the synchronous interface. */ private bool TryCancelAcquireAsyncByTask(Task <bool> acquireTask) { AsyncAcquire acquire = null; List <AsyncAcquire> satisfied = null; // To access the shared mutable state we must acquire the lock lock (theLock) { foreach (AsyncAcquire _acquire in asyncAcquires) { if (_acquire.Task == acquireTask) { acquire = _acquire; asyncAcquires.Remove(_acquire); acquire.done = true; if (asyncAcquires.Count > 0 && permits >= asyncAcquires.First.Value.acquires) { satisfied = SatisfyPendingAsyncAcquires(); } break; } } } // If we canceled the async acquire, process the cancellation if (acquire != null) { // After release the lock, complete any satisfied acquires if (satisfied != null) { CompleteSatisfiedAsyncAcquires(satisfied); } // Dispose the resources associated with this async acquire and complete // its task to the Canceled state. acquire.Dispose(); acquire.SetCanceled(); return(true); } return(false); }
/** * 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); } } }
/** * Synchronous interface implemented using the asynchronous TAP interface. */ /** * Try to cancel an asynchronous request identified by its task. * * Note: This is used to implement the synchronous interface. */ private bool CancelAcquireByTask(Task <bool> acquireTask) { AsyncAcquire acquire = null; List <AsyncAcquire> satisfied = null; // To access the shared mutable state we must acquire the lock lock (theLock) { foreach (AsyncAcquire _acquire in asyncAcquires) { if (_acquire.Task == acquireTask) { if (_acquire.TryLock()) { acquire = _acquire; asyncAcquires.Remove(_acquire); } break; } } // If the new state od semaphore allows waiting acquires, satisfy them if (asyncAcquires.Count > 0 && permits >= asyncAcquires.First.Value.acquires) { satisfied = SatisfyPendingAsyncAcquires(); } } CompleteSatisfiedAsyncAcquires(satisfied); if (acquire != null) { // Dispose the resources associated with this async acquire and complete // its task to the Canceled state. acquire.Dispose(); acquire.SetCanceled(); } return(acquire != null); }