SplitOrderedList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // SplitOrderedList.cs
  2. //
  3. // Copyright (c) 2010 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using System.Threading;
  26. using System.Collections;
  27. using System.Collections.Generic;
  28. using System.Runtime.Serialization;
  29. namespace System.Collections.Concurrent
  30. {
  31. internal class SplitOrderedList<TKey, T>
  32. {
  33. class Node
  34. {
  35. public bool Marked;
  36. public ulong Key;
  37. public TKey SubKey;
  38. public T Data;
  39. public Node Next;
  40. public Node Init (ulong key, TKey subKey, T data)
  41. {
  42. this.Key = key;
  43. this.SubKey = subKey;
  44. this.Data = data;
  45. this.Marked = false;
  46. this.Next = null;
  47. return this;
  48. }
  49. // Used to create dummy node
  50. public Node Init (ulong key)
  51. {
  52. this.Key = key;
  53. this.Data = default (T);
  54. this.Next = null;
  55. this.Marked = false;
  56. this.SubKey = default (TKey);
  57. return this;
  58. }
  59. // Used to create marked node
  60. public Node Init (Node wrapped)
  61. {
  62. this.Marked = true;
  63. this.Next = wrapped;
  64. this.Key = 0;
  65. this.Data = default (T);
  66. this.SubKey = default (TKey);
  67. return this;
  68. }
  69. }
  70. const int MaxLoad = 5;
  71. const uint BucketSize = 512;
  72. Node head;
  73. Node tail;
  74. Node[] buckets = new Node [BucketSize];
  75. int count;
  76. int size = 2;
  77. SimpleRwLock slim = new SimpleRwLock ();
  78. readonly IEqualityComparer<TKey> comparer;
  79. public SplitOrderedList (IEqualityComparer<TKey> comparer)
  80. {
  81. this.comparer = comparer;
  82. head = new Node ().Init (0);
  83. tail = new Node ().Init (ulong.MaxValue);
  84. head.Next = tail;
  85. SetBucket (0, head);
  86. }
  87. public int Count {
  88. get {
  89. return count;
  90. }
  91. }
  92. public T InsertOrUpdate (uint key, TKey subKey, Func<T> addGetter, Func<T, T> updateGetter)
  93. {
  94. Node current;
  95. bool result = InsertInternal (key, subKey, default (T), addGetter, out current);
  96. if (result)
  97. return current.Data;
  98. // FIXME: this should have a CAS-like behavior
  99. return current.Data = updateGetter (current.Data);
  100. }
  101. public T InsertOrUpdate (uint key, TKey subKey, T addValue, T updateValue)
  102. {
  103. Node current;
  104. if (InsertInternal (key, subKey, addValue, null, out current))
  105. return current.Data;
  106. // FIXME: this should have a CAS-like behavior
  107. return current.Data = updateValue;
  108. }
  109. public bool Insert (uint key, TKey subKey, T data)
  110. {
  111. Node current;
  112. return InsertInternal (key, subKey, data, null, out current);
  113. }
  114. public T InsertOrGet (uint key, TKey subKey, T data, Func<T> dataCreator)
  115. {
  116. Node current;
  117. InsertInternal (key, subKey, data, dataCreator, out current);
  118. return current.Data;
  119. }
  120. bool InsertInternal (uint key, TKey subKey, T data, Func<T> dataCreator, out Node current)
  121. {
  122. Node node = new Node ().Init (ComputeRegularKey (key), subKey, data);
  123. uint b = key % (uint)size;
  124. Node bucket;
  125. if ((bucket = GetBucket (b)) == null)
  126. bucket = InitializeBucket (b);
  127. if (!ListInsert (node, bucket, out current, dataCreator))
  128. return false;
  129. int csize = size;
  130. if (Interlocked.Increment (ref count) / csize > MaxLoad && (csize & 0x40000000) == 0)
  131. Interlocked.CompareExchange (ref size, 2 * csize, csize);
  132. current = node;
  133. return true;
  134. }
  135. public bool Find (uint key, TKey subKey, out T data)
  136. {
  137. Node node;
  138. uint b = key % (uint)size;
  139. data = default (T);
  140. Node bucket;
  141. if ((bucket = GetBucket (b)) == null)
  142. bucket = InitializeBucket (b);
  143. if (!ListFind (ComputeRegularKey (key), subKey, bucket, out node))
  144. return false;
  145. data = node.Data;
  146. return !node.Marked;
  147. }
  148. public bool CompareExchange (uint key, TKey subKey, T data, Func<T, bool> check)
  149. {
  150. Node node;
  151. uint b = key % (uint)size;
  152. Node bucket;
  153. if ((bucket = GetBucket (b)) == null)
  154. bucket = InitializeBucket (b);
  155. if (!ListFind (ComputeRegularKey (key), subKey, bucket, out node))
  156. return false;
  157. if (!check (node.Data))
  158. return false;
  159. node.Data = data;
  160. return true;
  161. }
  162. public bool Delete (uint key, TKey subKey, out T data)
  163. {
  164. uint b = key % (uint)size;
  165. Node bucket;
  166. if ((bucket = GetBucket (b)) == null)
  167. bucket = InitializeBucket (b);
  168. if (!ListDelete (bucket, ComputeRegularKey (key), subKey, out data))
  169. return false;
  170. Interlocked.Decrement (ref count);
  171. return true;
  172. }
  173. public IEnumerator<T> GetEnumerator ()
  174. {
  175. Node node = head.Next;
  176. while (node != tail) {
  177. while (node.Marked || (node.Key & 1) == 0) {
  178. node = node.Next;
  179. if (node == tail)
  180. yield break;
  181. }
  182. yield return node.Data;
  183. node = node.Next;
  184. }
  185. }
  186. Node InitializeBucket (uint b)
  187. {
  188. Node current;
  189. uint parent = GetParent (b);
  190. Node bucket;
  191. if ((bucket = GetBucket (parent)) == null)
  192. bucket = InitializeBucket (parent);
  193. Node dummy = new Node ().Init (ComputeDummyKey (b));
  194. if (!ListInsert (dummy, bucket, out current, null))
  195. return current;
  196. return SetBucket (b, dummy);
  197. }
  198. // Turn v's MSB off
  199. static uint GetParent (uint v)
  200. {
  201. uint t, tt;
  202. // Find MSB position in v
  203. var pos = (tt = v >> 16) > 0 ?
  204. (t = tt >> 8) > 0 ? 24 + logTable[t] : 16 + logTable[tt] :
  205. (t = v >> 8) > 0 ? 8 + logTable[t] : logTable[v];
  206. return (uint)(v & ~(1 << pos));
  207. }
  208. // Reverse integer bits and make sure LSB is set
  209. static ulong ComputeRegularKey (uint key)
  210. {
  211. return ComputeDummyKey (key) | 1;
  212. }
  213. // Reverse integer bits
  214. static ulong ComputeDummyKey (uint key)
  215. {
  216. return ((ulong)(((uint)reverseTable[key & 0xff] << 24) |
  217. ((uint)reverseTable[(key >> 8) & 0xff] << 16) |
  218. ((uint)reverseTable[(key >> 16) & 0xff] << 8) |
  219. ((uint)reverseTable[(key >> 24) & 0xff]))) << 1;
  220. }
  221. // Bucket storage is abstracted in a simple two-layer tree to avoid too much memory resize
  222. Node GetBucket (uint index)
  223. {
  224. if (index >= buckets.Length)
  225. return null;
  226. return buckets[index];
  227. }
  228. Node SetBucket (uint index, Node node)
  229. {
  230. try {
  231. slim.EnterReadLock ();
  232. CheckSegment (index, true);
  233. Interlocked.CompareExchange (ref buckets[index], node, null);
  234. return buckets[index];
  235. } finally {
  236. slim.ExitReadLock ();
  237. }
  238. }
  239. // When we run out of space for bucket storage, we use a lock-based array resize
  240. void CheckSegment (uint segment, bool readLockTaken)
  241. {
  242. if (segment < buckets.Length)
  243. return;
  244. if (readLockTaken)
  245. slim.ExitReadLock ();
  246. try {
  247. slim.EnterWriteLock ();
  248. while (segment >= buckets.Length)
  249. Array.Resize (ref buckets, buckets.Length * 2);
  250. } finally {
  251. slim.ExitWriteLock ();
  252. }
  253. if (readLockTaken)
  254. slim.EnterReadLock ();
  255. }
  256. Node ListSearch (ulong key, TKey subKey, ref Node left, Node h)
  257. {
  258. Node leftNodeNext = null, rightNode = null;
  259. do {
  260. Node t = h;
  261. Node tNext = t.Next;
  262. do {
  263. if (!tNext.Marked) {
  264. left = t;
  265. leftNodeNext = tNext;
  266. }
  267. t = tNext.Marked ? tNext.Next : tNext;
  268. if (t == tail)
  269. break;
  270. tNext = t.Next;
  271. } while (tNext.Marked || t.Key < key || (tNext.Key == key && !comparer.Equals (subKey, t.SubKey)));
  272. rightNode = t;
  273. if (leftNodeNext == rightNode) {
  274. if (rightNode != tail && rightNode.Next.Marked)
  275. continue;
  276. else
  277. return rightNode;
  278. }
  279. if (Interlocked.CompareExchange (ref left.Next, rightNode, leftNodeNext) == leftNodeNext) {
  280. if (rightNode != tail && rightNode.Next.Marked)
  281. continue;
  282. else
  283. return rightNode;
  284. }
  285. } while (true);
  286. }
  287. bool ListDelete (Node startPoint, ulong key, TKey subKey, out T data)
  288. {
  289. Node rightNode = null, rightNodeNext = null, leftNode = null;
  290. data = default (T);
  291. Node markedNode = null;
  292. do {
  293. rightNode = ListSearch (key, subKey, ref leftNode, startPoint);
  294. if (rightNode == tail || rightNode.Key != key || !comparer.Equals (subKey, rightNode.SubKey))
  295. return false;
  296. data = rightNode.Data;
  297. rightNodeNext = rightNode.Next;
  298. if (!rightNodeNext.Marked) {
  299. if (markedNode == null)
  300. markedNode = new Node ();
  301. markedNode.Init (rightNodeNext);
  302. if (Interlocked.CompareExchange (ref rightNode.Next, markedNode, rightNodeNext) == rightNodeNext)
  303. break;
  304. }
  305. } while (true);
  306. if (Interlocked.CompareExchange (ref leftNode.Next, rightNodeNext, rightNode) != rightNode)
  307. ListSearch (rightNode.Key, subKey, ref leftNode, startPoint);
  308. return true;
  309. }
  310. bool ListInsert (Node newNode, Node startPoint, out Node current, Func<T> dataCreator)
  311. {
  312. ulong key = newNode.Key;
  313. Node rightNode = null, leftNode = null;
  314. do {
  315. rightNode = current = ListSearch (key, newNode.SubKey, ref leftNode, startPoint);
  316. if (rightNode != tail && rightNode.Key == key && comparer.Equals (newNode.SubKey, rightNode.SubKey))
  317. return false;
  318. newNode.Next = rightNode;
  319. if (dataCreator != null)
  320. newNode.Data = dataCreator ();
  321. if (Interlocked.CompareExchange (ref leftNode.Next, newNode, rightNode) == rightNode)
  322. return true;
  323. } while (true);
  324. }
  325. bool ListFind (ulong key, TKey subKey, Node startPoint, out Node data)
  326. {
  327. Node rightNode = null, leftNode = null;
  328. data = null;
  329. rightNode = ListSearch (key, subKey, ref leftNode, startPoint);
  330. data = rightNode;
  331. return rightNode != tail && rightNode.Key == key && comparer.Equals (subKey, rightNode.SubKey);
  332. }
  333. static readonly byte[] reverseTable = {
  334. 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240, 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248, 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244, 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252, 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242, 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250, 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246, 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254, 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241, 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249, 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245, 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253, 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243, 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251, 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247, 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  335. };
  336. static readonly byte[] logTable = {
  337. 0xFF, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
  338. };
  339. struct SimpleRwLock
  340. {
  341. const int RwWait = 1;
  342. const int RwWrite = 2;
  343. const int RwRead = 4;
  344. int rwlock;
  345. public void EnterReadLock ()
  346. {
  347. SpinWait sw = new SpinWait ();
  348. do {
  349. while ((rwlock & (RwWrite | RwWait)) > 0)
  350. sw.SpinOnce ();
  351. if ((Interlocked.Add (ref rwlock, RwRead) & (RwWait | RwWait)) == 0)
  352. return;
  353. Interlocked.Add (ref rwlock, -RwRead);
  354. } while (true);
  355. }
  356. public void ExitReadLock ()
  357. {
  358. Interlocked.Add (ref rwlock, -RwRead);
  359. }
  360. public void EnterWriteLock ()
  361. {
  362. SpinWait sw = new SpinWait ();
  363. do {
  364. int state = rwlock;
  365. if (state < RwWrite) {
  366. if (Interlocked.CompareExchange (ref rwlock, RwWrite, state) == state)
  367. return;
  368. state = rwlock;
  369. }
  370. // We register our interest in taking the Write lock (if upgradeable it's already done)
  371. while ((state & RwWait) == 0 && Interlocked.CompareExchange (ref rwlock, state | RwWait, state) != state)
  372. state = rwlock;
  373. // Before falling to sleep
  374. while (rwlock > RwWait)
  375. sw.SpinOnce ();
  376. } while (true);
  377. }
  378. public void ExitWriteLock ()
  379. {
  380. Interlocked.Add (ref rwlock, -RwWrite);
  381. }
  382. }
  383. }
  384. }