Beispiel #1
0
 internal void Add(WaitNode node)
 {
     if (last is null)
     {
         first = last = node;
     }
     else
     {
         last.Append(node);
         last = node;
     }
 }
Beispiel #2
0
 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));
        }
Beispiel #8
0
            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);
            }
Beispiel #9
0
 private protected ConditionalNode(WaitNode parent)
     : base(parent)
 {
 }
 private protected ReadLockNode(WaitNode previous, bool upgradeable)
     : base(previous) => Upgradeable = upgradeable;
 internal WriteLockNode(WaitNode previous)
     : base(previous)
 {
 }
Beispiel #12
0
 internal StrongLockNode(WaitNode previous) : base(previous)
 {
 }
Beispiel #13
0
 internal DisposeAsyncNode(WaitNode previous)
     : base(previous)
 {
 }
Beispiel #14
0
 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);
Beispiel #18
0
 internal WaitNode(Predicate <TState> condition, WaitNode tail)
     : base(tail) => predicate = condition;
Beispiel #19
0
 internal bool Remove(WaitNode node) => RemoveCore(node);
Beispiel #20
0
 internal void Append(WaitNode node)
 {
     next          = node;
     node.previous = this;
 }
Beispiel #21
0
 WaitNode ILockManager <WaitNode> .CreateNode(WaitNode tail)
 => tail is null ? new WaitNode() : new WaitNode(tail);
Beispiel #22
0
 internal ConditionalNode(Predicate <TState> condition, WaitNode tail)
     : base(tail) => Condition = condition;