6 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
8 // Copyright (c) 2009 Jérémie "Garuma" Laval
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 namespace System.Threading
33 internal interface ISnziNode
43 readonly ISnziNode root;
44 readonly ISnziNode[] nodes;
46 readonly int count = Environment.ProcessorCount;
50 nodes = new ISnziNode[count];
51 root = new RootNode ();
52 for (int i = 0; i < count; i++)
53 nodes[i] = new LeafNode (root);
56 public void Increment ()
58 ISnziNode node = nodes[GetRandomIndex ()];
62 public void Decrement ()
64 ISnziNode node = nodes[GetRandomIndex ()];
70 ISnziNode node = nodes[GetRandomIndex ()];
82 return (Thread.CurrentThread.ManagedThreadId) % count;
85 class UnsafeLeafNode : ISnziNode
90 public UnsafeLeafNode (ISnziNode parent)
95 #region ISnziNode implementation
104 public void Depart ()
113 throw new System.NotImplementedException();
125 class LeafNode : ISnziNode
130 public LeafNode (ISnziNode parent)
132 this.parent = parent;
136 #region ISnziNode implementation
137 public void Arrive ()
144 Decode (x, out c, out v);
147 if (Interlocked.CompareExchange (ref var, Encode ((short)(c + 1), v), x) == x)
151 int temp = Encode (-1, (short)(v + 1));
152 if (Interlocked.CompareExchange (ref var, temp, x) == x) {
162 if (Interlocked.CompareExchange (ref var, Encode (1, v), x) != x)
166 for (int i = 0; i < undoArr; i++)
170 public void Depart ()
175 Decode (x, out c, out v);
177 if (Interlocked.CompareExchange (ref var, Encode ((short)(c - 1), v), x) == x) {
187 throw new System.NotImplementedException();
197 int Encode (short c, short v)
201 temp |= ((uint)v) << 16;
206 void Decode (int value, out short c, out short v)
208 uint temp = (uint)value;
209 c = (short)(temp & 0xFFFF);
210 v = (short)(temp >> 16);
214 class RootNode : ISnziNode
219 #region ISnziNode implementation
220 public void Arrive ()
229 Decode (x, out c, out a, out v);
232 temp = Encode (1, true, (short)(v + 1));
234 temp = Encode ((short)(c + 1), a, v);
235 } while (Interlocked.CompareExchange (ref var, temp, x) != x);
237 Decode (temp, out c, out a, out v);
242 int newI = (i & 0x7FFFFFFF) + 1;
243 newI = (int)(((uint)newI) | 0x80000000);
244 if (Interlocked.CompareExchange (ref state, newI, i) == i) {
248 Interlocked.CompareExchange (ref var, Encode (c, false, v), temp);
252 public void Depart ()
258 Decode (x, out c, out a, out v);
260 if (Interlocked.CompareExchange (ref var, Encode ((short)(c - 1), false, v), x) == x) {
265 if (((short)(var >> 16)) != v)
267 int newI = (i & 0x7FFFFFFF) + 1;
268 if (Interlocked.CompareExchange (ref state, newI, i) == i) {
278 throw new System.NotImplementedException();
283 return (state & 0x80000000) > 0;
288 int Encode (short c, bool a, short v)
292 temp |= (uint)(a ? 0x8000 : 0);
293 temp |= ((uint)v) << 16;
298 void Decode (int value, out short c, out bool a, out short v)
300 uint temp = (uint)value;
301 c = (short)(temp & 0x7FFF);
302 a = (temp & 0x8000) > 0;
303 v = (short)(temp >> 16);