/// <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); }
/// <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); }
public poolNode UnsafeDequeue() { if (this.head.QueueNextNode == null) { return(null); } poolNode head = this.head; this.head = this.head.QueueNextNode; head.tryPush(); return(this.head); }
/// <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); }