internal WaitNode?CleanupAndGotoNext() { var next = this.next; this.next = previous = null; return(next); }
internal void DetachNode() { if (!(previous is null)) { previous.next = next; } if (!(next is null)) { next.previous = previous; } next = previous = null; }
internal void Add(WaitNode node) { if (last is null) { first = last = node; } else { last.Append(node); last = node; } }
public void CancelSuspendedCallers(CancellationToken token) { if (!token.IsCancellationRequested) { throw new ArgumentOutOfRangeException(nameof(token)); } for (WaitNode?current = head, next; current is not null; current = next) { next = current.CleanupAndGotoNext(); current.TrySetCanceled(token); } head = tail = null; }
internal bool Remove(TKey expected, TValue value, IEqualityComparer <TKey> comparer) { for (WaitNode?current = first, next; current is not null; current = next) { next = current.Next; if (comparer.Equals(expected, current.Id)) { // the node will be removed automatically by consumer return(current.TrySetResult(value)); } } return(false); }
private protected bool RemoveNode(WaitNode node) { var inList = ReferenceEquals(head, node) || !node.IsRoot; if (ReferenceEquals(head, node)) { head = node.Next; } if (ReferenceEquals(tail, node)) { tail = node.Previous; } node.DetachNode(); return(inList); }
private Task DisposeAsync() { DisposeAsyncNode disposeNode; if (tail is null) { head = tail = disposeNode = new DisposeAsyncNode(); } else { tail = disposeNode = new DisposeAsyncNode(tail); } return(disposeNode.Task); }
private void NotifyObjectDisposed() { var e = new ObjectDisposedException(GetType().Name); for (WaitNode?current = head, next; current is not null; current = next) { next = current.CleanupAndGotoNext(); if (current is DisposeAsyncNode disposeNode) { disposeNode.SetResult(); } else { current.TrySetException(e); } } head = tail = null; }
private bool RemoveCore(WaitNode node) { Debug.Assert(Monitor.IsEntered(this)); var inList = false; if (ReferenceEquals(first, node)) { first = node.Next; inList = true; } if (ReferenceEquals(last, node)) { last = node.Previous; inList = true; } inList |= node.IsNotRoot; node.Detach(); return(inList); }
private protected Task <bool> WaitAsync <TNode, TManager>(ref TManager manager, TimeSpan timeout, CancellationToken token) where TNode : WaitNode where TManager : struct, ILockManager <TNode> { if (IsDisposed) { return(GetDisposedTask <bool>()); } if (timeout < TimeSpan.Zero && timeout != InfiniteTimeSpan) { throw new ArgumentOutOfRangeException(nameof(timeout)); } if (token.IsCancellationRequested) { return(Task.FromCanceled <bool>(token)); } if (manager.TryAcquire()) { return(CompletedTask <bool, BooleanConst.True> .Task); } if (timeout == TimeSpan.Zero) { return(CompletedTask <bool, BooleanConst.False> .Task); // if timeout is zero fail fast } if (tail is null) { head = tail = manager.CreateNode(null); } else { tail = manager.CreateNode(tail); } return(timeout == InfiniteTimeSpan? token.CanBeCanceled?WaitAsync(tail, token) : tail.Task : WaitAsync(tail, timeout, token)); }
// write lock management readonly WriteLockNode ILockManager <WriteLockNode> .CreateNode(WaitNode?node) => node is null ? new WriteLockNode() : new WriteLockNode(node);
// upgradeable read lock management readonly UpgradeableReadLockNode ILockManager <UpgradeableReadLockNode> .CreateNode(WaitNode?node) => node is null ? new UpgradeableReadLockNode() : new UpgradeableReadLockNode(node);
// read lock management readonly ReadLockNode ILockManager <ReadLockNode> .CreateNode(WaitNode?node) => node is null ? new ReadLockNode() : new ReadLockNode(node);
readonly StrongLockNode ILockManager <StrongLockNode> .CreateNode(WaitNode?tail) => tail is null ? new StrongLockNode() : new StrongLockNode(tail);
internal WaitNode(WaitNode previous) { previous.next = this; this.previous = previous; }
WaitNode ILockManager <WaitNode> .CreateNode(WaitNode?tail) => tail is null ? new WaitNode() : new WaitNode(tail);
private protected static bool IsTerminalNode(WaitNode?node) => node is DisposeAsyncNode;
WaitNode <TState> ILockManager <WaitNode <TState> > .CreateNode(WaitNode?tail) => tail is null ? new WaitNode <TState>(condition) : new WaitNode <TState>(condition, tail);
ReadLockNode ILockManager <ReadLockNode> .CreateNode(WaitNode?node) => node is null ? new ReadLockNode(false) : new ReadLockNode(node, false);