Exemplo n.º 1
0
            internal WaitNode?CleanupAndGotoNext()
            {
                var next = this.next;

                this.next = previous = null;
                return(next);
            }
Exemplo n.º 2
0
 internal void DetachNode()
 {
     if (!(previous is null))
     {
         previous.next = next;
     }
     if (!(next is null))
     {
         next.previous = previous;
     }
     next = previous = null;
 }
Exemplo n.º 3
0
 internal void Add(WaitNode node)
 {
     if (last is null)
     {
         first = last = node;
     }
     else
     {
         last.Append(node);
         last = node;
     }
 }
Exemplo n.º 4
0
        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;
        }
Exemplo n.º 5
0
            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);
            }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        private Task DisposeAsync()
        {
            DisposeAsyncNode disposeNode;

            if (tail is null)
            {
                head = tail = disposeNode = new DisposeAsyncNode();
            }
            else
            {
                tail = disposeNode = new DisposeAsyncNode(tail);
            }

            return(disposeNode.Task);
        }
Exemplo n.º 8
0
        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;
        }
Exemplo n.º 9
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);
            }
Exemplo n.º 10
0
 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));
 }
Exemplo n.º 11
0
 // write lock management
 readonly WriteLockNode ILockManager <WriteLockNode> .CreateNode(WaitNode?node) => node is null ? new WriteLockNode() : new WriteLockNode(node);
Exemplo n.º 12
0
 // upgradeable read lock management
 readonly UpgradeableReadLockNode ILockManager <UpgradeableReadLockNode> .CreateNode(WaitNode?node) => node is null ? new UpgradeableReadLockNode() : new UpgradeableReadLockNode(node);
Exemplo n.º 13
0
 // read lock management
 readonly ReadLockNode ILockManager <ReadLockNode> .CreateNode(WaitNode?node) => node is null ? new ReadLockNode() : new ReadLockNode(node);
Exemplo n.º 14
0
 readonly StrongLockNode ILockManager <StrongLockNode> .CreateNode(WaitNode?tail) => tail is null ? new StrongLockNode() : new StrongLockNode(tail);
Exemplo n.º 15
0
 internal WaitNode(WaitNode previous)
 {
     previous.next = this;
     this.previous = previous;
 }
Exemplo n.º 16
0
 WaitNode ILockManager <WaitNode> .CreateNode(WaitNode?tail)
 => tail is null ? new WaitNode() : new WaitNode(tail);
Exemplo n.º 17
0
 private protected static bool IsTerminalNode(WaitNode?node)
 => node is DisposeAsyncNode;
Exemplo n.º 18
0
 WaitNode <TState> ILockManager <WaitNode <TState> > .CreateNode(WaitNode?tail)
 => tail is null ? new WaitNode <TState>(condition) : new WaitNode <TState>(condition, tail);
Exemplo n.º 19
0
 ReadLockNode ILockManager <ReadLockNode> .CreateNode(WaitNode?node) => node is null ? new ReadLockNode(false) : new ReadLockNode(node, false);