DoubleDictionaryThreadAbortSafe.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. ~DoubleDictionaryThreadAbortSafe()
  54. {
  55. rwLock.Dispose();
  56. }
  57. public void Add(TKey1 key1, TKey2 key2, TValue value)
  58. {
  59. bool gotLock = false;
  60. try
  61. {
  62. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  63. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  64. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  65. try {}
  66. finally
  67. {
  68. rwLock.EnterWriteLock();
  69. gotLock = true;
  70. }
  71. if (Dictionary1.ContainsKey(key1))
  72. {
  73. if (!Dictionary2.ContainsKey(key2))
  74. throw new ArgumentException("key1 exists in the dictionary but not key2");
  75. }
  76. else if (Dictionary2.ContainsKey(key2))
  77. {
  78. if (!Dictionary1.ContainsKey(key1))
  79. throw new ArgumentException("key2 exists in the dictionary but not key1");
  80. }
  81. Dictionary1[key1] = value;
  82. Dictionary2[key2] = value;
  83. }
  84. finally
  85. {
  86. if (gotLock)
  87. rwLock.ExitWriteLock();
  88. }
  89. }
  90. public bool Remove(TKey1 key1, TKey2 key2)
  91. {
  92. bool success;
  93. bool gotLock = false;
  94. try
  95. {
  96. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  97. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  98. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  99. try {}
  100. finally
  101. {
  102. rwLock.EnterWriteLock();
  103. gotLock = true;
  104. }
  105. Dictionary1.Remove(key1);
  106. success = Dictionary2.Remove(key2);
  107. }
  108. finally
  109. {
  110. if (gotLock)
  111. rwLock.ExitWriteLock();
  112. }
  113. return success;
  114. }
  115. public bool Remove(TKey1 key1)
  116. {
  117. bool found = false;
  118. bool gotLock = false;
  119. try
  120. {
  121. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  122. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  123. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  124. try {}
  125. finally
  126. {
  127. rwLock.EnterWriteLock();
  128. gotLock = true;
  129. }
  130. // This is an O(n) operation!
  131. TValue value;
  132. if (Dictionary1.TryGetValue(key1, out value))
  133. {
  134. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2)
  135. {
  136. if (kvp.Value.Equals(value))
  137. {
  138. Dictionary1.Remove(key1);
  139. Dictionary2.Remove(kvp.Key);
  140. found = true;
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. finally
  147. {
  148. if (gotLock)
  149. rwLock.ExitWriteLock();
  150. }
  151. return found;
  152. }
  153. public bool Remove(TKey2 key2)
  154. {
  155. bool found = false;
  156. bool gotLock = false;
  157. try
  158. {
  159. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  160. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  161. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  162. try {}
  163. finally
  164. {
  165. rwLock.EnterWriteLock();
  166. gotLock = true;
  167. }
  168. // This is an O(n) operation!
  169. TValue value;
  170. if (Dictionary2.TryGetValue(key2, out value))
  171. {
  172. foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1)
  173. {
  174. if (kvp.Value.Equals(value))
  175. {
  176. Dictionary2.Remove(key2);
  177. Dictionary1.Remove(kvp.Key);
  178. found = true;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. finally
  185. {
  186. if (gotLock)
  187. rwLock.ExitWriteLock();
  188. }
  189. return found;
  190. }
  191. public void Clear()
  192. {
  193. bool gotLock = false;
  194. try
  195. {
  196. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  197. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  198. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  199. try {}
  200. finally
  201. {
  202. rwLock.EnterWriteLock();
  203. gotLock = true;
  204. }
  205. Dictionary1.Clear();
  206. Dictionary2.Clear();
  207. }
  208. finally
  209. {
  210. if (gotLock)
  211. rwLock.ExitWriteLock();
  212. }
  213. }
  214. public int Count
  215. {
  216. get { return Dictionary1.Count; }
  217. }
  218. public bool ContainsKey(TKey1 key)
  219. {
  220. return Dictionary1.ContainsKey(key);
  221. }
  222. public bool ContainsKey(TKey2 key)
  223. {
  224. return Dictionary2.ContainsKey(key);
  225. }
  226. public bool TryGetValue(TKey1 key, out TValue value)
  227. {
  228. bool success;
  229. bool gotLock = false;
  230. try
  231. {
  232. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  233. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  234. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  235. try {}
  236. finally
  237. {
  238. rwLock.EnterReadLock();
  239. gotLock = true;
  240. }
  241. success = Dictionary1.TryGetValue(key, out value);
  242. }
  243. finally
  244. {
  245. if (gotLock)
  246. rwLock.ExitReadLock();
  247. }
  248. return success;
  249. }
  250. public bool TryGetValue(TKey2 key, out TValue value)
  251. {
  252. bool success;
  253. bool gotLock = false;
  254. try
  255. {
  256. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  257. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  258. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  259. try {}
  260. finally
  261. {
  262. rwLock.EnterReadLock();
  263. gotLock = true;
  264. }
  265. success = Dictionary2.TryGetValue(key, out value);
  266. }
  267. finally
  268. {
  269. if (gotLock)
  270. rwLock.ExitReadLock();
  271. }
  272. return success;
  273. }
  274. public void ForEach(Action<TValue> action)
  275. {
  276. bool gotLock = false;
  277. try
  278. {
  279. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  280. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  281. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  282. try {}
  283. finally
  284. {
  285. rwLock.EnterReadLock();
  286. gotLock = true;
  287. }
  288. foreach (TValue value in Dictionary1.Values)
  289. action(value);
  290. }
  291. finally
  292. {
  293. if (gotLock)
  294. rwLock.ExitReadLock();
  295. }
  296. }
  297. public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
  298. {
  299. bool gotLock = false;
  300. try
  301. {
  302. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  303. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  304. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  305. try {}
  306. finally
  307. {
  308. rwLock.EnterReadLock();
  309. gotLock = true;
  310. }
  311. foreach (KeyValuePair<TKey1, TValue> entry in Dictionary1)
  312. action(entry);
  313. }
  314. finally
  315. {
  316. if (gotLock)
  317. rwLock.ExitReadLock();
  318. }
  319. }
  320. public void ForEach(Action<KeyValuePair<TKey2, TValue>> action)
  321. {
  322. bool gotLock = false;
  323. try
  324. {
  325. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  326. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  327. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  328. try {}
  329. finally
  330. {
  331. rwLock.EnterReadLock();
  332. gotLock = true;
  333. }
  334. foreach (KeyValuePair<TKey2, TValue> entry in Dictionary2)
  335. action(entry);
  336. }
  337. finally
  338. {
  339. if (gotLock)
  340. rwLock.ExitReadLock();
  341. }
  342. }
  343. public TValue FindValue(Predicate<TValue> predicate)
  344. {
  345. bool gotLock = false;
  346. try
  347. {
  348. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  349. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  350. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  351. try {}
  352. finally
  353. {
  354. rwLock.EnterReadLock();
  355. gotLock = true;
  356. }
  357. foreach (TValue value in Dictionary1.Values)
  358. {
  359. if (predicate(value))
  360. return value;
  361. }
  362. }
  363. finally
  364. {
  365. if (gotLock)
  366. rwLock.ExitReadLock();
  367. }
  368. return default(TValue);
  369. }
  370. public IList<TValue> FindAll(Predicate<TValue> predicate)
  371. {
  372. IList<TValue> list = new List<TValue>();
  373. bool gotLock = false;
  374. try
  375. {
  376. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  377. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  378. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  379. try {}
  380. finally
  381. {
  382. rwLock.EnterReadLock();
  383. gotLock = true;
  384. }
  385. foreach (TValue value in Dictionary1.Values)
  386. {
  387. if (predicate(value))
  388. list.Add(value);
  389. }
  390. }
  391. finally
  392. {
  393. if (gotLock)
  394. rwLock.ExitReadLock();
  395. }
  396. return list;
  397. }
  398. public int RemoveAll(Predicate<TValue> predicate)
  399. {
  400. IList<TKey1> list = new List<TKey1>();
  401. bool gotUpgradeableLock = false;
  402. try
  403. {
  404. // Avoid an asynchronous Thread.Abort() from possibly never existing an acquired lock by placing
  405. // the acquision inside the main try. The inner finally block is needed because thread aborts cannot
  406. // interrupt code in these blocks (hence gotLock is guaranteed to be set correctly).
  407. try {}
  408. finally
  409. {
  410. rwLock.EnterUpgradeableReadLock();
  411. gotUpgradeableLock = true;
  412. }
  413. foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1)
  414. {
  415. if (predicate(kvp.Value))
  416. list.Add(kvp.Key);
  417. }
  418. IList<TKey2> list2 = new List<TKey2>(list.Count);
  419. foreach (KeyValuePair<TKey2, TValue> kvp in Dictionary2)
  420. {
  421. if (predicate(kvp.Value))
  422. list2.Add(kvp.Key);
  423. }
  424. bool gotWriteLock = false;
  425. try
  426. {
  427. try {}
  428. finally
  429. {
  430. rwLock.EnterUpgradeableReadLock();
  431. gotWriteLock = true;
  432. }
  433. for (int i = 0; i < list.Count; i++)
  434. Dictionary1.Remove(list[i]);
  435. for (int i = 0; i < list2.Count; i++)
  436. Dictionary2.Remove(list2[i]);
  437. }
  438. finally
  439. {
  440. if (gotWriteLock)
  441. rwLock.ExitWriteLock();
  442. }
  443. }
  444. finally
  445. {
  446. if (gotUpgradeableLock)
  447. rwLock.ExitUpgradeableReadLock();
  448. }
  449. return list.Count;
  450. }
  451. }
  452. }