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.

291 lines
6.8KB

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