示例#1
0
            /// <summary>
            /// 多线程并发添加节点
            /// </summary>
            /// <param name="node"></param>
            internal void EnqueueNotNull(poolNode node)
            {
                ++node.UsedCount;
                poolNode end;

                do
                {
                    while ((end = this.end).TryUse() != 0)
                    {
                        Thread.Yield();
                        if ((end = this.end).TryUse() == 0)
                        {
                            break;
                        }
                        Thread.Sleep(0);
                    }
                    if (end == this.end)
                    {
                        if (Interlocked.CompareExchange(ref end.QueueNextNode, node, null) == null)
                        {
                            this.end = node;
                            end.tryPush();
                            return;
                        }
                        while (end == this.end)
                        {
                            Thread.Sleep(0);
                        }
                    }
                    end.tryPush();
                }while (true);
            }
示例#2
0
            /// <summary>
            /// 多线程并发弹出节点
            /// </summary>
            /// <returns></returns>
            public poolNode Dequeue()
            {
                poolNode head;

                do
                {
                    if ((head = this.head).QueueNextNode == null)
                    {
                        return(null);
                    }
                    if (head.TryUse() != 0)
                    {
                        Thread.Yield();
                        if ((head = this.head).QueueNextNode == null)
                        {
                            return(null);
                        }
                        if (head.TryUse() != 0)
                        {
                            Thread.Sleep(0);
                            continue;
                        }
                    }
                    if (head == this.head)
                    {
                        poolNode node = head.QueueNextNode;
                        if (Interlocked.CompareExchange(ref this.head, node, head) == head)
                        {
                            head.TryPush2();
                            return(node);
                        }
                    }
                    head.tryPush();
                }while (true);
            }
示例#3
0
 public void Enqueue(poolNode node)
 {
     if (node == null)
     {
         log.Error.Throw(log.exceptionType.Null);
     }
     EnqueueNotNull(node);
 }
示例#4
0
 internal void TryPush2()
 {
     if (Interlocked.Add(ref UsedCount, -2) == 0)
     {
         QueueNextNode = null;
         push();
     }
 }
示例#5
0
 protected internal void tryPush()
 {
     if (Interlocked.Decrement(ref UsedCount) == 0)
     {
         QueueNextNode = null;
         push();
     }
 }
示例#6
0
            public poolNode UnsafeDequeue()
            {
                if (this.head.QueueNextNode == null)
                {
                    return(null);
                }
                poolNode head = this.head;

                this.head = this.head.QueueNextNode;
                head.tryPush();
                return(this.head);
            }
示例#7
0
            /// <summary>
            /// 多线程并发清除节点数据
            /// </summary>
            public void Clear()
            {
                poolNode head;

                do
                {
                    if ((head = this.head) == this.end)
                    {
                        return;
                    }
                    if (head.TryUse() != 0)
                    {
                        Thread.Yield();
                        if ((head = this.head) == this.end)
                        {
                            return;
                        }
                        if (head.TryUse() != 0)
                        {
                            Thread.Sleep(0);
                            continue;
                        }
                    }
                    if (head == this.head)
                    {
                        poolNode end = this.end;
                        if (Interlocked.CompareExchange(ref this.head, end, head) == head)
                        {
                            poolNode node = head.QueueNextNode;
                            head.TryPush2();
                            while (node != end)
                            {
                                head = node.QueueNextNode;
                                node.tryPush();
                                if (head == end)
                                {
                                    return;
                                }
                                node = head.QueueNextNode;
                                head.tryPush();
                            }
                            return;
                        }
                    }
                    head.tryPush();
                }while (true);
            }
示例#8
0
            /// <summary>
            /// 单线程清除节点数据
            /// </summary>
            public void UnsafeClear()
            {
                poolNode node;

                while (head != end)
                {
                    node = head.QueueNextNode;
                    head.tryPush();
                    if (node == end)
                    {
                        head = node;
                        return;
                    }
                    head = node.QueueNextNode;
                    node.tryPush();
                }
            }
示例#9
0
 public void UnsafeEnqueue(poolNode node)
 {
     ++node.UsedCount;
     end.QueueNextNode = node;
     end = node;
 }
示例#10
0
 /// <summary>
 /// 节点队列
 /// </summary>
 /// <param name="node">默认节点,不能为null</param>
 public concurrentPool(poolNode node)
 {
     node.UsedCount += 2;
     head            = end = node;
 }