From c6f371fe4fd795dd5bed5a275c324bbb6cf111cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Laval?= Date: Sat, 15 Oct 2011 14:51:38 +0200 Subject: [PATCH] Add a pop front operation to ConcurrentOrderedList --- .../ConcurrentOrderedList.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mcs/class/corlib/System.Collections.Concurrent/ConcurrentOrderedList.cs b/mcs/class/corlib/System.Collections.Concurrent/ConcurrentOrderedList.cs index dec8ef5df10..14d49f2b8f5 100644 --- a/mcs/class/corlib/System.Collections.Concurrent/ConcurrentOrderedList.cs +++ b/mcs/class/corlib/System.Collections.Concurrent/ConcurrentOrderedList.cs @@ -116,6 +116,11 @@ namespace System.Collections.Concurrent return false; } + public bool TryPop (out T data) + { + return ListPop (out data); + } + public bool Contains (T data) { return ContainsHash (comparer.GetHashCode (data)); @@ -238,6 +243,30 @@ namespace System.Collections.Concurrent return true; } + + bool ListPop (out T data) + { + Node rightNode = null, rightNodeNext = null, leftNode = null; + data = default (T); + + do { + rightNode = head.Next; + if (rightNode == tail) + return false; + + data = rightNode.Data; + + rightNodeNext = rightNode.Next; + if (!rightNodeNext.Marked) + if (Interlocked.CompareExchange (ref rightNode.Next, new Node (rightNodeNext), rightNodeNext) == rightNodeNext) + break; + } while (true); + + if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext) + ListSearch (rightNode.Key, ref leftNode); + + return true; + } bool ListInsert (Node newNode) { -- 2.25.1