1
0

DoubleDictionaryThreadAbortSafe.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (c) 2008, openmetaverse.org, http://opensimulator.org/
  3. * All rights reserved.
  4. *
  5. * - Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * - Neither the name of the openmetaverse.org nor the names
  11. * of its contributors may be used to endorse or promote products derived from
  12. * this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.Threading;
  28. using System.Collections.Generic;
  29. namespace OpenSim.Framework
  30. {
  31. /// <summary>
  32. /// A double dictionary that is thread abort safe.
  33. /// </summary>
  34. /// <remarks>
  35. /// This adapts OpenMetaverse.DoubleDictionary to be thread-abort safe by acquiring ReaderWriterLockSlim within
  36. /// a finally section (which can't be interrupted by Thread.Abort()).
  37. /// </remarks>
  38. public class DoubleDictionaryThreadAbortSafe<TKey1, TKey2, TValue>
  39. {
  40. Dictionary<TKey1, TValue> Dictionary1;
  41. Dictionary<TKey2, TValue> Dictionary2;
  42. ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();
  43. public DoubleDictionaryThreadAbortSafe()
  44. {
  45. Dictionary1 = new Dictionary<TKey1,TValue>();
  46. Dictionary2 = new Dictionary<TKey2,TValue>();
  47. }
  48. public DoubleDictionaryThreadAbortSafe(int capacity)
  49. {
  50. Dictionary1 = new Dictionary<TKey1, TValue>(capacity);
  51. Dictionary2 = new Dictionary<TKey2, TValue>(capacity);
  52. }
  53. public void Add(TKey1 key1, TKey2 key2, TValue value)
  54. {
  55. bool gotLock = false;
  56. try
  57. {
  58. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  59. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  60. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  61. try {}
  62. finally
  63. {
  64. rwLock.EnterWriteLock();
  65. gotLock = true;
  66. }
  67. if (Dictionary1.ContainsKey(key1))
  68. {
  69. if (!Dictionary2.ContainsKey(key2))
  70. throw new ArgumentException("key1 exists in the dictionary but not key2");
  71. }
  72. else if (Dictionary2.ContainsKey(key2))
  73. {
  74. if (!Dictionary1.ContainsKey(key1))
  75. throw new ArgumentException("key2 exists in the dictionary but not key1");
  76. }
  77. Dictionary1[key1] = value;
  78. Dictionary2[key2] = value;
  79. }
  80. finally
  81. {
  82. if (gotLock)
  83. rwLock.ExitWriteLock();
  84. }
  85. }
  86. public bool Remove(TKey1 key1, TKey2 key2)
  87. {
  88. bool success;
  89. bool gotLock = false;
  90. try
  91. {
  92. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  93. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  94. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  95. try {}
  96. finally
  97. {
  98. rwLock.EnterWriteLock();
  99. gotLock = true;
  100. }
  101. Dictionary1.Remove(key1);
  102. success = Dictionary2.Remove(key2);
  103. }
  104. finally
  105. {
  106. if (gotLock)
  107. rwLock.ExitWriteLock();
  108. }
  109. return success;
  110. }
  111. public bool Remove(TKey1 key1)
  112. {
  113. bool found = false;
  114. bool gotLock = false;
  115. try
  116. {
  117. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  118. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  119. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  120. try {}
  121. finally
  122. {
  123. rwLock.EnterWriteLock();
  124. gotLock = true;
  125. }
  126. // This is an O(n) operation!
  127. TValue value;
  128. if (Dictionary1.TryGetValue(key1, out value))
  129. {
  130. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2)
  131. {
  132. if (kvp.Value.Equals(value))
  133. {
  134. Dictionary1.Remove(key1);
  135. Dictionary2.Remove(kvp.Key);
  136. found = true;
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. finally
  143. {
  144. if (gotLock)
  145. rwLock.ExitWriteLock();
  146. }
  147. return found;
  148. }
  149. public bool Remove(TKey2 key2)
  150. {
  151. bool found = false;
  152. bool gotLock = false;
  153. try
  154. {
  155. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  156. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  157. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  158. try {}
  159. finally
  160. {
  161. rwLock.EnterWriteLock();
  162. gotLock = true;
  163. }
  164. // This is an O(n) operation!
  165. TValue value;
  166. if (Dictionary2.TryGetValue(key2, out value))
  167. {
  168. foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1)
  169. {
  170. if (kvp.Value.Equals(value))
  171. {
  172. Dictionary2.Remove(key2);
  173. Dictionary1.Remove(kvp.Key);
  174. found = true;
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. finally
  181. {
  182. if (gotLock)
  183. rwLock.ExitWriteLock();
  184. }
  185. return found;
  186. }
  187. public void Clear()
  188. {
  189. bool gotLock = false;
  190. try
  191. {
  192. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  193. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  194. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  195. try {}
  196. finally
  197. {
  198. rwLock.EnterWriteLock();
  199. gotLock = true;
  200. }
  201. Dictionary1.Clear();
  202. Dictionary2.Clear();
  203. }
  204. finally
  205. {
  206. if (gotLock)
  207. rwLock.ExitWriteLock();
  208. }
  209. }
  210. public int Count
  211. {
  212. get { return Dictionary1.Count; }
  213. }
  214. public bool ContainsKey(TKey1 key)
  215. {
  216. return Dictionary1.ContainsKey(key);
  217. }
  218. public bool ContainsKey(TKey2 key)
  219. {
  220. return Dictionary2.ContainsKey(key);
  221. }
  222. public bool TryGetValue(TKey1 key, out TValue value)
  223. {
  224. bool success;
  225. bool gotLock = false;
  226. try
  227. {
  228. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  229. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  230. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  231. try {}
  232. finally
  233. {
  234. rwLock.EnterReadLock();
  235. gotLock = true;
  236. }
  237. success = Dictionary1.TryGetValue(key, out value);
  238. }
  239. finally
  240. {
  241. if (gotLock)
  242. rwLock.ExitReadLock();
  243. }
  244. return success;
  245. }
  246. public bool TryGetValue(TKey2 key, out TValue value)
  247. {
  248. bool success;
  249. bool gotLock = false;
  250. try
  251. {
  252. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  253. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  254. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  255. try {}
  256. finally
  257. {
  258. rwLock.EnterReadLock();
  259. gotLock = true;
  260. }
  261. success = Dictionary2.TryGetValue(key, out value);
  262. }
  263. finally
  264. {
  265. if (gotLock)
  266. rwLock.ExitReadLock();
  267. }
  268. return success;
  269. }
  270. public void ForEach(Action<TValue> action)
  271. {
  272. bool gotLock = false;
  273. try
  274. {
  275. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  276. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  277. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  278. try {}
  279. finally
  280. {
  281. rwLock.EnterReadLock();
  282. gotLock = true;
  283. }
  284. foreach (TValue value in Dictionary1.Values)
  285. action(value);
  286. }
  287. finally
  288. {
  289. if (gotLock)
  290. rwLock.ExitReadLock();
  291. }
  292. }
  293. public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
  294. {
  295. bool gotLock = false;
  296. try
  297. {
  298. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  299. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  300. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  301. try {}
  302. finally
  303. {
  304. rwLock.EnterReadLock();
  305. gotLock = true;
  306. }
  307. foreach (KeyValuePair<TKey1, TValue> entry in Dictionary1)
  308. action(entry);
  309. }
  310. finally
  311. {
  312. if (gotLock)
  313. rwLock.ExitReadLock();
  314. }
  315. }
  316. public void ForEach(Action<KeyValuePair<TKey2, TValue>> action)
  317. {
  318. bool gotLock = false;
  319. try
  320. {
  321. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  322. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  323. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  324. try {}
  325. finally
  326. {
  327. rwLock.EnterReadLock();
  328. gotLock = true;
  329. }
  330. foreach (KeyValuePair<TKey2, TValue> entry in Dictionary2)
  331. action(entry);
  332. }
  333. finally
  334. {
  335. if (gotLock)
  336. rwLock.ExitReadLock();
  337. }
  338. }
  339. public TValue FindValue(Predicate<TValue> predicate)
  340. {
  341. bool gotLock = false;
  342. try
  343. {
  344. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  345. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  346. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  347. try {}
  348. finally
  349. {
  350. rwLock.EnterReadLock();
  351. gotLock = true;
  352. }
  353. foreach (TValue value in Dictionary1.Values)
  354. {
  355. if (predicate(value))
  356. return value;
  357. }
  358. }
  359. finally
  360. {
  361. if (gotLock)
  362. rwLock.ExitReadLock();
  363. }
  364. return default(TValue);
  365. }
  366. public IList<TValue> FindAll(Predicate<TValue> predicate)
  367. {
  368. IList<TValue> list = new List<TValue>();
  369. bool gotLock = false;
  370. try
  371. {
  372. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  373. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  374. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  375. try {}
  376. finally
  377. {
  378. rwLock.EnterReadLock();
  379. gotLock = true;
  380. }
  381. foreach (TValue value in Dictionary1.Values)
  382. {
  383. if (predicate(value))
  384. list.Add(value);
  385. }
  386. }
  387. finally
  388. {
  389. if (gotLock)
  390. rwLock.ExitReadLock();
  391. }
  392. return list;
  393. }
  394. public int RemoveAll(Predicate<TValue> predicate)
  395. {
  396. IList<TKey1> list = new List<TKey1>();
  397. bool gotUpgradeableLock = false;
  398. try
  399. {
  400. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  401. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  402. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  403. try {}
  404. finally
  405. {
  406. rwLock.EnterUpgradeableReadLock();
  407. gotUpgradeableLock = true;
  408. }
  409. foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1)
  410. {
  411. if (predicate(kvp.Value))
  412. list.Add(kvp.Key);
  413. }
  414. IList<TKey2> list2 = new List<TKey2>(list.Count);
  415. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2)
  416. {
  417. if (predicate(kvp.Value))
  418. list2.Add(kvp.Key);
  419. }
  420. bool gotWriteLock = false;
  421. try
  422. {
  423. try {}
  424. finally
  425. {
  426. rwLock.EnterUpgradeableReadLock();
  427. gotWriteLock = true;
  428. }
  429. for (int i = 0; i < list.Count; i++)
  430. Dictionary1.Remove(list[i]);
  431. for (int i = 0; i < list2.Count; i++)
  432. Dictionary2.Remove(list2[i]);
  433. }
  434. finally
  435. {
  436. if (gotWriteLock)
  437. rwLock.ExitWriteLock();
  438. }
  439. }
  440. finally
  441. {
  442. if (gotUpgradeableLock)
  443. rwLock.ExitUpgradeableReadLock();
  444. }
  445. return list.Count;
  446. }
  447. }
  448. }