DoubleDictionaryThreadAbortSafe.cs 17 KB

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