DoubleDictionaryThreadAbortSafe.cs 17 KB

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