forked from Inumedia/SlackAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockFreeQueue.cs
More file actions
210 lines (182 loc) · 5.78 KB
/
Copy pathLockFreeQueue.cs
File metadata and controls
210 lines (182 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace SlackAPI.Utilities
{
public class LockFreeQueue<T> : ILockFree<T>
where T : class
{
private SingleLinkNode mHead;
private SingleLinkNode mTail;
public int Count;
public T Latest
{
get
{
return mHead.Next == null ? default(T) : mTail.Item;
}
}
public LockFreeQueue()
{
mHead = new SingleLinkNode();
mTail = mHead;
}
private static bool CompareAndExchange(ref SingleLinkNode pLocation, SingleLinkNode pComparand, SingleLinkNode pNewValue)
{
return
pComparand ==
Interlocked.CompareExchange(ref pLocation, pNewValue, pComparand);
}
public T Next { get { return mHead.Next == null ? default(T) : mHead.Next.Item; } }
public void Unshift(T pItem)
{
SingleLinkNode oldHead = null;
SingleLinkNode newNode = new SingleLinkNode();
newNode.Item = pItem;
bool newNodeWasAdded = false;
while (!newNodeWasAdded)
{
oldHead = mHead.Next;
newNode.Next = oldHead;
if (mHead.Next == oldHead)
newNodeWasAdded = CompareAndExchange(ref mHead.Next, oldHead, newNode);
}
CompareAndExchange(ref mHead, oldHead, newNode);
}
public override void Push(T pItem)
{
SingleLinkNode oldTail = null;
SingleLinkNode oldTailNext;
SingleLinkNode newNode = new SingleLinkNode();
newNode.Item = pItem;
bool newNodeWasAdded = false;
while (!newNodeWasAdded)
{
oldTail = mTail;
oldTailNext = oldTail.Next;
if (mTail == oldTail)
if (oldTailNext == null)
newNodeWasAdded = CompareAndExchange(ref mTail.Next, null, newNode);
else
CompareAndExchange(ref mTail, oldTail, oldTailNext);
}
CompareAndExchange(ref mTail, oldTail, newNode);
Interlocked.Increment(ref Count);
}
public override bool Pop(out T pItem)
{
pItem = default(T);
SingleLinkNode oldHead = null;
bool haveAdvancedHead = false;
while (!haveAdvancedHead)
{
oldHead = mHead;
SingleLinkNode oldTail = mTail;
SingleLinkNode oldHeadNext = oldHead.Next;
if (oldHead == mHead)
{
if (oldHead == oldTail)
{
if (oldHeadNext == null)
return false;
CompareAndExchange(ref mTail, oldTail, oldHeadNext);
}
else
{
pItem = oldHeadNext.Item;
haveAdvancedHead =
CompareAndExchange(ref mHead, oldHead, oldHeadNext);
}
}
}
Interlocked.Decrement(ref Count);
return true;
}
public T Shift()
{
T result;
Shift(out result);
return result;
}
public bool Shift(out T pItem)
{
pItem = default(T);
if (mHead == null)
return false;
SingleLinkNode oldHead = null;
bool haveAdvancedHead = false;
while (!haveAdvancedHead)
{
oldHead = mHead;
if (oldHead != null)
{
SingleLinkNode oldHeadNext = oldHead.Next;
if (CompareAndExchange(ref mHead, oldHead, oldHeadNext))
{
pItem = oldHead.Item;
return true;
}
}
}
return false;
}
public override T Pop()
{
T result;
Pop(out result);
return result;
}
public override string ToString()
{
return String.Format("Item count: {0}", Count);
}
public IEnumerator<T> GetEnumerator()
{
return new LockFreeEnumerator(this);
}
/// <summary>
/// Does *not* provide any kind of stateful guarantee. Should only be used in cases where we know that the queue is not volatile.
/// </summary>
internal class LockFreeEnumerator : IEnumerator<T>
{
LockFreeQueue<T> parent;
SingleLinkNode currentNode;
T IEnumerator<T>.Current
{
get
{
return currentNode.Item;
}
}
object IEnumerator.Current
{
get
{
return currentNode.Item;
}
}
public bool MoveNext()
{
if (currentNode == null)
currentNode = parent.mHead.Next;
else
currentNode = currentNode.Next;
return currentNode != null;
}
public LockFreeEnumerator(LockFreeQueue<T> list)
{
parent = list;
}
public void Dispose()
{
parent = null;
currentNode = null;
}
public void Reset()
{
currentNode = parent.mHead.Next;
}
}
}
}