CSnzi.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #if NET_4_0
  2. //
  3. // CSnzi.cs
  4. //
  5. // Author:
  6. // Jérémie "Garuma" Laval <[email protected]>
  7. //
  8. // Copyright (c) 2009 Jérémie "Garuma" Laval
  9. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be included in
  18. // all copies or substantial portions of the Software.
  19. //
  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
  26. // THE SOFTWARE.
  27. using System;
  28. namespace System.Threading
  29. {
  30. internal interface ICSnziNode
  31. {
  32. bool Arrive ();
  33. bool Depart ();
  34. }
  35. internal enum CSnziState
  36. {
  37. Open,
  38. Closed
  39. }
  40. internal struct QueryReturn
  41. {
  42. internal readonly bool NonZero;
  43. internal readonly bool Open;
  44. internal QueryReturn (bool nonzero, bool open)
  45. {
  46. NonZero = nonzero;
  47. Open = open;
  48. }
  49. }
  50. internal class CSnziLeafNode : ICSnziNode
  51. {
  52. int count;
  53. readonly ICSnziNode parent;
  54. public CSnziLeafNode (ICSnziNode parent)
  55. {
  56. this.parent = parent;
  57. }
  58. #region ICSnziNode implementation
  59. public bool Arrive ()
  60. {
  61. bool arrivedAtParent = false;
  62. int x;
  63. do {
  64. x = count;
  65. if (x == 0 && !arrivedAtParent) {
  66. if (parent.Arrive ())
  67. arrivedAtParent = true;
  68. else
  69. return false;
  70. }
  71. } while (Interlocked.CompareExchange (ref count, x + 1, x) != x);
  72. if (arrivedAtParent && x != 0)
  73. parent.Depart ();
  74. return true;
  75. }
  76. public bool Depart ()
  77. {
  78. int x = Interlocked.Decrement (ref count);
  79. if (x == 1)
  80. return parent.Depart ();
  81. else
  82. return true;
  83. }
  84. #endregion
  85. }
  86. internal class CSnziRootNode : ICSnziNode
  87. {
  88. int root;
  89. public int Count {
  90. get {
  91. return root & 0x7FFFFFFF;
  92. }
  93. }
  94. public CSnziState State {
  95. get {
  96. return (root >> 31) > 0 ? CSnziState.Open : CSnziState.Closed;
  97. }
  98. }
  99. public CSnziRootNode () : this (0, CSnziState.Open)
  100. {
  101. }
  102. public CSnziRootNode (int count, CSnziState state)
  103. {
  104. root = Encode (count, state);
  105. }
  106. #region ICSnziNode implementation
  107. public bool Arrive ()
  108. {
  109. int old;
  110. int c;
  111. CSnziState s;
  112. do {
  113. old = root;
  114. Decode (old, out c, out s);
  115. if (c == 0 && s == CSnziState.Closed)
  116. return false;
  117. } while (Interlocked.CompareExchange (ref root, Encode (c + 1, s), old) != old);
  118. return true;
  119. }
  120. public bool Depart ()
  121. {
  122. int old;
  123. int c;
  124. CSnziState s;
  125. do {
  126. old = root;
  127. Decode (old, out c, out s);
  128. } while (Interlocked.CompareExchange (ref root, Encode (c - 1, s), old) != old);
  129. return c != 0 && s != CSnziState.Closed;
  130. }
  131. #endregion
  132. public void Open ()
  133. {
  134. root = Encode (0, CSnziState.Open);
  135. }
  136. public bool Close ()
  137. {
  138. int old, newRoot;
  139. int c;
  140. CSnziState s;
  141. do {
  142. old = root;
  143. Decode (old, out c, out s);
  144. if (s != CSnziState.Open)
  145. return false;
  146. newRoot = Encode (c, CSnziState.Closed);
  147. } while (Interlocked.CompareExchange (ref root, newRoot, old) != old);
  148. return c == 0;
  149. }
  150. int Encode (int count, CSnziState state)
  151. {
  152. return (state == CSnziState.Open) ? (int)(((uint)count) | 0x80000000) : count & 0x7FFFFFFF;
  153. }
  154. void Decode (int code, out int count, out CSnziState state)
  155. {
  156. count = code & 0x7FFFFFFF;
  157. state = (code >> 31) > 0 ? CSnziState.Open : CSnziState.Closed;
  158. }
  159. }
  160. internal class CSnzi
  161. {
  162. CSnziRootNode root;
  163. CSnziLeafNode[] leafs;
  164. readonly int LeafCount = Environment.ProcessorCount * 2;
  165. public CSnzi ()
  166. {
  167. leafs = new CSnziLeafNode[LeafCount];
  168. root = new CSnziRootNode ();
  169. for (int i = 0; i < leafs.Length; i++) {
  170. leafs[i] = new CSnziLeafNode (root);
  171. }
  172. }
  173. public ICSnziNode Arrive ()
  174. {
  175. while (true) {
  176. if (root.State != CSnziState.Open)
  177. return null;
  178. ICSnziNode leaf = leafs[GetLeafIndex ()];
  179. if (leaf.Arrive ())
  180. return leaf;
  181. else {
  182. return null;
  183. }
  184. }
  185. }
  186. public bool Depart (ICSnziNode node)
  187. {
  188. return node.Depart ();
  189. }
  190. public bool Close ()
  191. {
  192. return root.Close ();
  193. }
  194. public void Open ()
  195. {
  196. root.Open ();
  197. }
  198. public QueryReturn Query ()
  199. {
  200. CSnziRootNode copy = root;
  201. return new QueryReturn (copy.Count > 0, copy.State == CSnziState.Open);
  202. }
  203. int GetLeafIndex ()
  204. {
  205. return (Thread.CurrentThread.ManagedThreadId - 1) % leafs.Length;
  206. }
  207. }
  208. }
  209. #endif