Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThreadSafeDictionary.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. //note, rewrite like ThreadSafeQueue
  5. namespace Svelto.DataStructures
  6. {
  7. /// <summary>
  8. /// original code: http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx
  9. /// simplified (not an IDictionary) and apdated (uses FasterList)
  10. /// </summary>
  11. /// <typeparam name = "TKey"></typeparam>
  12. /// <typeparam name = "TValue"></typeparam>
  13. [Serializable]
  14. public class ThreadSafeDictionary<TKey, TValue>
  15. {
  16. public ThreadSafeDictionary(int v)
  17. {
  18. dict = new Dictionary<TKey, TValue>(v);
  19. }
  20. public ThreadSafeDictionary()
  21. {
  22. dict = new Dictionary<TKey, TValue>();
  23. }
  24. // setup the lock;
  25. public virtual int Count
  26. {
  27. get
  28. {
  29. LockQ.EnterReadLock();
  30. try
  31. {
  32. return dict.Count;
  33. }
  34. finally
  35. {
  36. LockQ.ExitReadLock();
  37. }
  38. }
  39. }
  40. public virtual bool IsReadOnly
  41. {
  42. get
  43. {
  44. LockQ.EnterReadLock();
  45. try
  46. {
  47. return dict.IsReadOnly;
  48. }
  49. finally
  50. {
  51. LockQ.ExitReadLock();
  52. }
  53. }
  54. }
  55. public virtual FasterList<TKey> Keys
  56. {
  57. get
  58. {
  59. LockQ.EnterReadLock();
  60. try
  61. {
  62. return new FasterList<TKey>(dict.Keys);
  63. }
  64. finally
  65. {
  66. LockQ.ExitReadLock();
  67. }
  68. }
  69. }
  70. public virtual FasterList<TValue> Values
  71. {
  72. get
  73. {
  74. LockQ.EnterReadLock();
  75. try
  76. {
  77. return new FasterList<TValue>(dict.Values);
  78. }
  79. finally
  80. {
  81. LockQ.ExitReadLock();
  82. }
  83. }
  84. }
  85. public virtual TValue this[TKey key]
  86. {
  87. get
  88. {
  89. LockQ.EnterReadLock();
  90. try
  91. {
  92. return dict[key];
  93. }
  94. finally
  95. {
  96. LockQ.ExitReadLock();
  97. }
  98. }
  99. set
  100. {
  101. LockQ.EnterWriteLock();
  102. try
  103. {
  104. dict[key] = value;
  105. }
  106. finally
  107. {
  108. LockQ.ExitWriteLock();
  109. }
  110. }
  111. }
  112. public virtual void Add(KeyValuePair<TKey, TValue> item)
  113. {
  114. LockQ.EnterWriteLock();
  115. try
  116. {
  117. dict.Add(item);
  118. }
  119. finally
  120. {
  121. LockQ.ExitWriteLock();
  122. }
  123. }
  124. public virtual void Clear()
  125. {
  126. LockQ.EnterWriteLock();
  127. try
  128. {
  129. dict.Clear();
  130. }
  131. finally
  132. {
  133. LockQ.ExitWriteLock();
  134. }
  135. }
  136. public virtual bool Contains(KeyValuePair<TKey, TValue> item)
  137. {
  138. LockQ.EnterReadLock();
  139. try
  140. {
  141. return dict.Contains(item);
  142. }
  143. finally
  144. {
  145. LockQ.ExitReadLock();
  146. }
  147. }
  148. public virtual void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  149. {
  150. LockQ.EnterReadLock();
  151. try
  152. {
  153. dict.CopyTo(array, arrayIndex);
  154. }
  155. finally
  156. {
  157. LockQ.ExitReadLock();
  158. }
  159. }
  160. public virtual bool Remove(KeyValuePair<TKey, TValue> item)
  161. {
  162. LockQ.EnterWriteLock();
  163. try
  164. {
  165. return dict.Remove(item);
  166. }
  167. finally
  168. {
  169. LockQ.ExitWriteLock();
  170. }
  171. }
  172. public virtual void Add(TKey key, TValue value)
  173. {
  174. LockQ.EnterWriteLock();
  175. try
  176. {
  177. dict.Add(key, value);
  178. }
  179. finally
  180. {
  181. LockQ.ExitWriteLock();
  182. }
  183. }
  184. public virtual bool ContainsKey(TKey key)
  185. {
  186. LockQ.EnterReadLock();
  187. try
  188. {
  189. return dict.ContainsKey(key);
  190. }
  191. finally
  192. {
  193. LockQ.ExitReadLock();
  194. }
  195. }
  196. public virtual bool Remove(TKey key)
  197. {
  198. LockQ.EnterWriteLock();
  199. try
  200. {
  201. return dict.Remove(key);
  202. }
  203. finally
  204. {
  205. LockQ.ExitWriteLock();
  206. }
  207. }
  208. public virtual bool TryGetValue(TKey key, out TValue value)
  209. {
  210. LockQ.EnterReadLock();
  211. try
  212. {
  213. return dict.TryGetValue(key, out value);
  214. }
  215. finally
  216. {
  217. LockQ.ExitReadLock();
  218. }
  219. }
  220. /// <summary>
  221. /// Merge does a blind remove, and then add. Basically a blind Upsert.
  222. /// </summary>
  223. /// <param name = "key">Key to lookup</param>
  224. /// <param name = "newValue">New Value</param>
  225. public void MergeSafe(TKey key, TValue newValue)
  226. {
  227. LockQ.EnterWriteLock();
  228. try
  229. {
  230. // take a writelock immediately since we will always be writing
  231. if (dict.ContainsKey(key))
  232. dict.Remove(key);
  233. dict.Add(key, newValue);
  234. }
  235. finally
  236. {
  237. LockQ.ExitWriteLock();
  238. }
  239. }
  240. /// <summary>
  241. /// This is a blind remove. Prevents the need to check for existence first.
  242. /// </summary>
  243. /// <param name = "key">Key to remove</param>
  244. public void RemoveSafe(TKey key)
  245. {
  246. LockQ.EnterReadLock();
  247. try
  248. {
  249. if (dict.ContainsKey(key))
  250. LockQ.EnterWriteLock();
  251. try
  252. {
  253. dict.Remove(key);
  254. }
  255. finally
  256. {
  257. LockQ.ExitWriteLock();
  258. }
  259. }
  260. finally
  261. {
  262. LockQ.ExitReadLock();
  263. }
  264. }
  265. // This is the internal dictionary that we are wrapping
  266. readonly IDictionary<TKey, TValue> dict;
  267. readonly ReaderWriterLockSlim LockQ = new ReaderWriterLockSlim();
  268. }
  269. }