5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2009 Jérémie "Garuma" Laval
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 namespace System.Threading
32 internal interface ICSnziNode
38 internal enum CSnziState
44 internal struct QueryReturn
46 internal readonly bool NonZero;
47 internal readonly bool Open;
49 internal QueryReturn (bool nonzero, bool open)
56 internal class CSnziLeafNode : ICSnziNode
59 readonly ICSnziNode parent;
61 public CSnziLeafNode (ICSnziNode parent)
66 #region ICSnziNode implementation
69 bool arrivedAtParent = false;
74 if (x == 0 && !arrivedAtParent) {
76 arrivedAtParent = true;
80 } while (Interlocked.CompareExchange (ref count, x + 1, x) != x);
82 if (arrivedAtParent && x != 0)
90 int x = Interlocked.Decrement (ref count);
92 return parent.Depart ();
100 internal class CSnziRootNode : ICSnziNode
106 return root & 0x7FFFFFFF;
110 public CSnziState State {
112 return (root >> 31) > 0 ? CSnziState.Open : CSnziState.Closed;
116 public CSnziRootNode () : this (0, CSnziState.Open)
121 public CSnziRootNode (int count, CSnziState state)
123 root = Encode (count, state);
126 #region ICSnziNode implementation
127 public bool Arrive ()
136 Decode (old, out c, out s);
138 if (c == 0 && s == CSnziState.Closed)
140 } while (Interlocked.CompareExchange (ref root, Encode (c + 1, s), old) != old);
145 public bool Depart ()
153 Decode (old, out c, out s);
154 } while (Interlocked.CompareExchange (ref root, Encode (c - 1, s), old) != old);
156 return c != 0 && s != CSnziState.Closed;
162 root = Encode (0, CSnziState.Open);
174 Decode (old, out c, out s);
175 if (s != CSnziState.Open)
178 newRoot = Encode (c, CSnziState.Closed);
179 } while (Interlocked.CompareExchange (ref root, newRoot, old) != old);
184 int Encode (int count, CSnziState state)
186 return (state == CSnziState.Open) ? (int)(((uint)count) | 0x80000000) : count & 0x7FFFFFFF;
189 void Decode (int code, out int count, out CSnziState state)
191 count = code & 0x7FFFFFFF;
192 state = (code >> 31) > 0 ? CSnziState.Open : CSnziState.Closed;
199 CSnziLeafNode[] leafs;
201 readonly int LeafCount = Environment.ProcessorCount * 2;
205 leafs = new CSnziLeafNode[LeafCount];
206 root = new CSnziRootNode ();
208 for (int i = 0; i < leafs.Length; i++) {
209 leafs[i] = new CSnziLeafNode (root);
213 public ICSnziNode Arrive ()
216 if (root.State != CSnziState.Open)
219 ICSnziNode leaf = leafs[GetLeafIndex ()];
228 public bool Depart (ICSnziNode node)
230 return node.Depart ();
235 return root.Close ();
243 public QueryReturn Query ()
245 CSnziRootNode copy = root;
247 return new QueryReturn (copy.Count > 0, copy.State == CSnziState.Open);
252 return (Thread.CurrentThread.ManagedThreadId - 1) % leafs.Length;