internal void Add(WaitNode node) { if (last is null) { first = last = node; } else { last.Append(node); last = node; } }
public bool Reset() { ThrowIfDisposed(); if (node is null) { node = new WaitNode(); return(true); } else { return(false); } }
private async Task <bool> WaitAsync(WaitNode node, CancellationToken token) { if (ReferenceEquals(node.Task, await Task.WhenAny(node.Task, Task.Delay(InfiniteTimeSpan, token)).ConfigureAwait(false))) { return(true); } if (RemoveNode(node)) { token.ThrowIfCancellationRequested(); return(false); } return(await node.Task.ConfigureAwait(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); }
internal async Task <TValue> WaitAsync(WaitNode node, CancellationToken token) { Debug.Assert(token.CanBeCanceled); using (var source = new CancelableCompletionSource <TValue>(TaskCreationOptions.RunContinuationsAsynchronously, token)) { if (ReferenceEquals(node.Task, await Task.WhenAny(node.Task, source.Task).ConfigureAwait(false))) { goto exit; } } if (Remove(node)) { token.ThrowIfCancellationRequested(); } exit: return(await node.Task.ConfigureAwait(false)); }
internal async Task <TValue> WaitAsync(WaitNode node, TimeSpan timeout, CancellationToken token) { using (var source = token.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(token) : new CancellationTokenSource()) { if (ReferenceEquals(node.Task, await Task.WhenAny(node.Task, Task.Delay(timeout, source.Token)).ConfigureAwait(false))) { source.Cancel(); // ensure that timer is cancelled goto exit; } } if (Remove(node)) { token.ThrowIfCancellationRequested(); throw new TimeoutException(); } exit: return(await node.Task.ConfigureAwait(false)); }
private async Task <bool> WaitAsync(WaitNode node, TimeSpan timeout, CancellationToken token) { // cannot use Task.WaitAsync here because this method contains side effect in the form of RemoveNode method using (var tokenSource = token.CanBeCanceled ? CancellationTokenSource.CreateLinkedTokenSource(token) : new CancellationTokenSource()) { if (ReferenceEquals(node.Task, await Task.WhenAny(node.Task, Task.Delay(timeout, tokenSource.Token)).ConfigureAwait(false))) { tokenSource.Cancel(); // ensure that Delay task is cancelled return(true); } } if (RemoveNode(node)) { token.ThrowIfCancellationRequested(); return(false); } return(await node.Task.ConfigureAwait(false)); }
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 ConditionalNode(WaitNode parent) : base(parent) { }
private protected ReadLockNode(WaitNode previous, bool upgradeable) : base(previous) => Upgradeable = upgradeable;
internal WriteLockNode(WaitNode previous) : base(previous) { }
internal StrongLockNode(WaitNode previous) : base(previous) { }
internal DisposeAsyncNode(WaitNode previous) : base(previous) { }
internal WaitNode(WaitNode previous) { previous.next = this; this.previous = previous; }
private WriteLockNode(WaitNode previous) : base(previous) { }
ReadLockNode ILockManager <ReadLockNode> .CreateNode(WaitNode node) => node is null ? new ReadLockNode(false) : new ReadLockNode(node, false);
WriteLockNode ILockManager <WriteLockNode> .CreateNode(WaitNode node) => node is null ? new WriteLockNode() : new WriteLockNode(node);
internal WaitNode(Predicate <TState> condition, WaitNode tail) : base(tail) => predicate = condition;
internal bool Remove(WaitNode node) => RemoveCore(node);
internal void Append(WaitNode node) { next = node; node.previous = this; }
WaitNode ILockManager <WaitNode> .CreateNode(WaitNode tail) => tail is null ? new WaitNode() : new WaitNode(tail);
internal ConditionalNode(Predicate <TState> condition, WaitNode tail) : base(tail) => Condition = condition;