ConcurrentDictionary.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #if NET_4_0
  2. // ConcurrentSkipList.cs
  3. //
  4. // Copyright (c) 2009 Jérémie "Garuma" Laval
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Threading;
  27. using System.Collections;
  28. using System.Collections.Generic;
  29. using System.Runtime.Serialization;
  30. namespace System.Collections.Concurrent
  31. {
  32. public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>,
  33. ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>,
  34. IDictionary, ICollection, IEnumerable
  35. {
  36. class Pair
  37. {
  38. public readonly TKey Key;
  39. public TValue Value;
  40. public Pair (TKey key, TValue value)
  41. {
  42. Key = key;
  43. Value = value;
  44. }
  45. public override bool Equals (object obj)
  46. {
  47. Pair rhs = obj as Pair;
  48. return rhs == null ? false : Key.Equals (rhs.Key) && Value.Equals (rhs.Value);
  49. }
  50. public override int GetHashCode ()
  51. {
  52. return Key.GetHashCode ();
  53. }
  54. }
  55. class Basket: List<Pair>
  56. {
  57. public SpinLock Lock = new SpinLock (true);
  58. }
  59. // Assumption: a List<T> is never empty
  60. ConcurrentSkipList<Basket> container
  61. = new ConcurrentSkipList<Basket> ((value) => value[0].GetHashCode ());
  62. int count;
  63. IEqualityComparer<TKey> comparer;
  64. public ConcurrentDictionary () : this (EqualityComparer<TKey>.Default)
  65. {
  66. }
  67. public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> values)
  68. : this (values, EqualityComparer<TKey>.Default)
  69. {
  70. foreach (KeyValuePair<TKey, TValue> pair in values)
  71. Add (pair.Key, pair.Value);
  72. }
  73. public ConcurrentDictionary (IEqualityComparer<TKey> comparer)
  74. {
  75. this.comparer = comparer;
  76. }
  77. public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> values, IEqualityComparer<TKey> comparer)
  78. : this (comparer)
  79. {
  80. foreach (KeyValuePair<TKey, TValue> pair in values)
  81. Add (pair.Key, pair.Value);
  82. }
  83. // Parameters unused
  84. public ConcurrentDictionary (int concurrencyLevel, int capacity)
  85. : this (EqualityComparer<TKey>.Default)
  86. {
  87. }
  88. public ConcurrentDictionary (int concurrencyLevel,
  89. IEnumerable<KeyValuePair<TKey, TValue>> values,
  90. IEqualityComparer<TKey> comparer)
  91. : this (values, comparer)
  92. {
  93. }
  94. // Parameters unused
  95. public ConcurrentDictionary (int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer)
  96. : this (comparer)
  97. {
  98. }
  99. void Add (TKey key, TValue value)
  100. {
  101. while (!TryAdd (key, value));
  102. }
  103. void IDictionary<TKey, TValue>.Add (TKey key, TValue value)
  104. {
  105. Add (key, value);
  106. }
  107. public bool TryAdd (TKey key, TValue value)
  108. {
  109. Basket basket;
  110. bool taken = false;
  111. // Add a value to an existing basket
  112. if (TryGetBasket (key, out basket)) {
  113. while (!taken) {
  114. try {
  115. basket.Lock.Enter (ref taken);
  116. if (!taken)
  117. continue;
  118. foreach (var p in basket) {
  119. if (comparer.Equals (p.Key, key))
  120. return false;
  121. }
  122. basket.Add (new Pair (key, value));
  123. } finally {
  124. if (taken)
  125. basket.Lock.Exit ();
  126. }
  127. }
  128. } else {
  129. // Add a new basket
  130. basket = new Basket ();
  131. basket.Add (new Pair (key, value));
  132. if (container.TryAdd (basket)) {
  133. Interlocked.Increment (ref count);
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. Interlocked.Increment (ref count);
  140. return true;
  141. }
  142. void ICollection<KeyValuePair<TKey,TValue>>.Add (KeyValuePair<TKey, TValue> pair)
  143. {
  144. Add (pair.Key, pair.Value);
  145. }
  146. public TValue AddOrUpdate (TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  147. {
  148. Basket basket;
  149. TValue temp = default (TValue);
  150. bool taken = false;
  151. if (!TryGetBasket (key, out basket)) {
  152. Add (key, (temp = addValueFactory (key)));
  153. } else {
  154. while (!taken) {
  155. try {
  156. basket.Lock.Enter (ref taken);
  157. if (!taken)
  158. continue;
  159. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  160. if (pair == null)
  161. throw new InvalidOperationException ("pair is null, shouldn't be");
  162. pair.Value = (temp = updateValueFactory (key, pair.Value));
  163. } finally {
  164. if (taken)
  165. basket.Lock.Exit ();
  166. }
  167. }
  168. }
  169. return temp;
  170. }
  171. public TValue AddOrUpdate (TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  172. {
  173. return AddOrUpdate (key, (_) => addValue, updateValueFactory);
  174. }
  175. TValue GetValue (TKey key)
  176. {
  177. TValue temp;
  178. if (!TryGetValue (key, out temp))
  179. // TODO: find a correct Exception
  180. throw new ArgumentException ("Not a valid key for this dictionary", "key");
  181. return temp;
  182. }
  183. public bool TryGetValue (TKey key, out TValue value)
  184. {
  185. Basket basket;
  186. value = default (TValue);
  187. bool taken = false;
  188. if (!TryGetBasket (key, out basket))
  189. return false;
  190. while (!taken) {
  191. try {
  192. basket.Lock.Enter (ref taken);
  193. if (!taken)
  194. continue;
  195. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  196. if (pair == null)
  197. return false;
  198. value = pair.Value;
  199. } finally {
  200. if (taken)
  201. basket.Lock.Exit ();
  202. }
  203. }
  204. return true;
  205. }
  206. public bool TryUpdate (TKey key, TValue newValue, TValue comparand)
  207. {
  208. Basket basket;
  209. bool taken = false;
  210. if (!TryGetBasket (key, out basket))
  211. return false;
  212. while (!taken) {
  213. try {
  214. basket.Lock.Enter (ref taken);
  215. if (!taken)
  216. continue;
  217. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  218. if (pair.Value.Equals (comparand)) {
  219. pair.Value = newValue;
  220. return true;
  221. }
  222. } finally {
  223. if (taken)
  224. basket.Lock.Exit ();
  225. }
  226. }
  227. return false;
  228. }
  229. public TValue this[TKey key] {
  230. get {
  231. return GetValue (key);
  232. }
  233. set {
  234. Basket basket;
  235. bool taken = false;
  236. if (!TryGetBasket (key, out basket)) {
  237. Add (key, value);
  238. return;
  239. }
  240. while (!taken) {
  241. try {
  242. basket.Lock.Enter (ref taken);
  243. if (!taken)
  244. continue;
  245. Pair pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  246. if (pair == null)
  247. throw new InvalidOperationException ("pair is null, shouldn't be");
  248. pair.Value = value;
  249. } finally {
  250. if (taken)
  251. basket.Lock.Exit ();
  252. }
  253. }
  254. }
  255. }
  256. public TValue GetOrAdd (TKey key, Func<TKey, TValue> valueFactory)
  257. {
  258. Basket basket;
  259. TValue temp = default (TValue);
  260. if (TryGetBasket (key, out basket)) {
  261. Pair pair = null;
  262. bool taken = false;
  263. while (!taken) {
  264. try {
  265. basket.Lock.Enter (ref taken);
  266. if (!taken)
  267. continue;
  268. pair = basket.Find ((p) => comparer.Equals (p.Key, key));
  269. if (pair != null)
  270. temp = pair.Value;
  271. } finally {
  272. if (taken)
  273. basket.Lock.Exit ();
  274. }
  275. }
  276. if (pair == null)
  277. Add (key, (temp = valueFactory (key)));
  278. } else {
  279. Add (key, (temp = valueFactory (key)));
  280. }
  281. return temp;
  282. }
  283. public TValue GetOrAdd (TKey key, TValue value)
  284. {
  285. return GetOrAdd (key, (_) => value);
  286. }
  287. public bool TryRemove(TKey key, out TValue value)
  288. {
  289. value = default (TValue);
  290. Basket b;
  291. bool taken = false;
  292. if (!TryGetBasket (key, out b))
  293. return false;
  294. while (!taken) {
  295. try {
  296. b.Lock.Enter (ref taken);
  297. if (!taken)
  298. continue;
  299. TValue temp = default (TValue);
  300. // Should always be == 1 but who know
  301. bool result = b.RemoveAll ((p) => {
  302. bool r = comparer.Equals (p.Key, key);
  303. if (r) temp = p.Value;
  304. return r;
  305. }) >= 1;
  306. value = temp;
  307. if (result)
  308. Interlocked.Decrement (ref count);
  309. return result;
  310. } finally {
  311. if (taken)
  312. b.Lock.Exit ();
  313. }
  314. }
  315. return false;
  316. }
  317. bool Remove (TKey key)
  318. {
  319. TValue dummy;
  320. return TryRemove (key, out dummy);
  321. }
  322. bool IDictionary<TKey, TValue>.Remove (TKey key)
  323. {
  324. return Remove (key);
  325. }
  326. bool ICollection<KeyValuePair<TKey,TValue>>.Remove (KeyValuePair<TKey,TValue> pair)
  327. {
  328. return Remove (pair.Key);
  329. }
  330. public bool ContainsKey (TKey key)
  331. {
  332. return container.ContainsFromHash (key.GetHashCode ());
  333. }
  334. bool IDictionary.Contains (object key)
  335. {
  336. if (!(key is TKey))
  337. return false;
  338. return ContainsKey ((TKey)key);
  339. }
  340. void IDictionary.Remove (object key)
  341. {
  342. if (!(key is TKey))
  343. return;
  344. Remove ((TKey)key);
  345. }
  346. object IDictionary.this [object key]
  347. {
  348. get {
  349. if (!(key is TKey))
  350. throw new ArgumentException ("key isn't of correct type", "key");
  351. return this[(TKey)key];
  352. }
  353. set {
  354. if (!(key is TKey) || !(value is TValue))
  355. throw new ArgumentException ("key or value aren't of correct type");
  356. this[(TKey)key] = (TValue)value;
  357. }
  358. }
  359. void IDictionary.Add (object key, object value)
  360. {
  361. if (!(key is TKey) || !(value is TValue))
  362. throw new ArgumentException ("key or value aren't of correct type");
  363. Add ((TKey)key, (TValue)value);
  364. }
  365. bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey, TValue> pair)
  366. {
  367. return ContainsKey (pair.Key);
  368. }
  369. public KeyValuePair<TKey,TValue>[] ToArray ()
  370. {
  371. // This is most certainly not optimum but there is
  372. // not a lot of possibilities
  373. return new List<KeyValuePair<TKey,TValue>> (this).ToArray ();
  374. }
  375. public void Clear()
  376. {
  377. // Pronk
  378. container = new ConcurrentSkipList<Basket> ((value) => value [0].GetHashCode ());
  379. }
  380. public int Count {
  381. get {
  382. return count;
  383. }
  384. }
  385. public bool IsEmpty {
  386. get {
  387. return count == 0;
  388. }
  389. }
  390. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
  391. get {
  392. return false;
  393. }
  394. }
  395. bool IDictionary.IsReadOnly {
  396. get {
  397. return false;
  398. }
  399. }
  400. public ICollection<TKey> Keys {
  401. get {
  402. return GetPart<TKey> ((kvp) => kvp.Key);
  403. }
  404. }
  405. public ICollection<TValue> Values {
  406. get {
  407. return GetPart<TValue> ((kvp) => kvp.Value);
  408. }
  409. }
  410. ICollection IDictionary.Keys {
  411. get {
  412. return (ICollection)Keys;
  413. }
  414. }
  415. ICollection IDictionary.Values {
  416. get {
  417. return (ICollection)Values;
  418. }
  419. }
  420. ICollection<T> GetPart<T> (Func<KeyValuePair<TKey, TValue>, T> extractor)
  421. {
  422. List<T> temp = new List<T> ();
  423. foreach (KeyValuePair<TKey, TValue> kvp in this)
  424. temp.Add (extractor (kvp));
  425. return temp.AsReadOnly ();
  426. }
  427. void ICollection.CopyTo (Array array, int startIndex)
  428. {
  429. KeyValuePair<TKey, TValue>[] arr = array as KeyValuePair<TKey, TValue>[];
  430. if (arr == null)
  431. return;
  432. CopyTo (arr, startIndex, count);
  433. }
  434. void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
  435. {
  436. CopyTo (array, startIndex, count);
  437. }
  438. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex)
  439. {
  440. CopyTo (array, startIndex);
  441. }
  442. void CopyTo (KeyValuePair<TKey, TValue>[] array, int startIndex, int num)
  443. {
  444. // TODO: This is quite unsafe as the count value will likely change during
  445. // the copying. Watchout for IndexOutOfRange thingies
  446. if (array.Length <= count + startIndex)
  447. throw new InvalidOperationException ("The array isn't big enough");
  448. int i = startIndex;
  449. foreach (Basket b in container) {
  450. bool taken = false;
  451. while (!taken) {
  452. try {
  453. b.Lock.Enter (ref taken);
  454. if (!taken)
  455. continue;
  456. foreach (Pair p in b) {
  457. if (i >= num)
  458. break;
  459. array[i++] = new KeyValuePair<TKey, TValue> (p.Key, p.Value);
  460. }
  461. } finally {
  462. if (taken)
  463. b.Lock.Exit ();
  464. }
  465. }
  466. }
  467. }
  468. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
  469. {
  470. return GetEnumeratorInternal ();
  471. }
  472. IEnumerator IEnumerable.GetEnumerator ()
  473. {
  474. return (IEnumerator)GetEnumeratorInternal ();
  475. }
  476. IEnumerator<KeyValuePair<TKey, TValue>> GetEnumeratorInternal ()
  477. {
  478. foreach (Basket b in container) {
  479. bool taken = false;
  480. while (!taken) {
  481. try {
  482. b.Lock.Enter (ref taken);
  483. if (!taken)
  484. continue;
  485. foreach (Pair p in b)
  486. yield return new KeyValuePair<TKey, TValue> (p.Key, p.Value);
  487. } finally {
  488. if (taken)
  489. b.Lock.Exit ();
  490. }
  491. }
  492. }
  493. }
  494. IDictionaryEnumerator IDictionary.GetEnumerator ()
  495. {
  496. return new ConcurrentDictionaryEnumerator (GetEnumeratorInternal ());
  497. }
  498. class ConcurrentDictionaryEnumerator : IDictionaryEnumerator
  499. {
  500. IEnumerator<KeyValuePair<TKey, TValue>> internalEnum;
  501. public ConcurrentDictionaryEnumerator (IEnumerator<KeyValuePair<TKey, TValue>> internalEnum)
  502. {
  503. this.internalEnum = internalEnum;
  504. }
  505. public bool MoveNext ()
  506. {
  507. return internalEnum.MoveNext ();
  508. }
  509. public void Reset ()
  510. {
  511. internalEnum.Reset ();
  512. }
  513. public object Current {
  514. get {
  515. return Entry;
  516. }
  517. }
  518. public DictionaryEntry Entry {
  519. get {
  520. KeyValuePair<TKey, TValue> current = internalEnum.Current;
  521. return new DictionaryEntry (current.Key, current.Value);
  522. }
  523. }
  524. public object Key {
  525. get {
  526. return internalEnum.Current.Key;
  527. }
  528. }
  529. public object Value {
  530. get {
  531. return internalEnum.Current.Value;
  532. }
  533. }
  534. }
  535. object ICollection.SyncRoot {
  536. get {
  537. return this;
  538. }
  539. }
  540. bool IDictionary.IsFixedSize {
  541. get {
  542. return false;
  543. }
  544. }
  545. bool ICollection.IsSynchronized {
  546. get { return true; }
  547. }
  548. bool TryGetBasket (TKey key, out Basket basket)
  549. {
  550. basket = null;
  551. if (!container.GetFromHash (key.GetHashCode (), out basket))
  552. return false;
  553. return true;
  554. }
  555. }
  556. }
  557. #endif