ConcurrentHashSet.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.Serialization;
  5. using System.Threading;
  6. namespace Masuit.Tools.Systems;
  7. /// <summary>
  8. /// 并发HashSet
  9. /// </summary>
  10. /// <typeparam name="T"></typeparam>
  11. public sealed class ConcurrentHashSet<T> : ISet<T>, IDisposable
  12. {
  13. private readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.SupportsRecursion);
  14. private readonly HashSet<T> _hashSet = new();
  15. public int Count
  16. {
  17. get
  18. {
  19. _lock.EnterWriteLock();
  20. try
  21. {
  22. return _hashSet.Count;
  23. }
  24. finally
  25. {
  26. if (_lock.IsWriteLockHeld)
  27. {
  28. _lock.ExitWriteLock();
  29. }
  30. }
  31. }
  32. }
  33. public bool IsReadOnly => false;
  34. public ConcurrentHashSet()
  35. {
  36. }
  37. public ConcurrentHashSet(IEqualityComparer<T> comparer)
  38. {
  39. _hashSet = new HashSet<T>(comparer);
  40. }
  41. public ConcurrentHashSet(IEnumerable<T> collection)
  42. {
  43. _hashSet = new HashSet<T>(collection);
  44. }
  45. public ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer)
  46. {
  47. _hashSet = new HashSet<T>(collection, comparer);
  48. }
  49. public ConcurrentHashSet(SerializationInfo info, StreamingContext context)
  50. {
  51. _hashSet = new HashSet<T>();
  52. var iSerializable = (ISerializable)_hashSet;
  53. iSerializable.GetObjectData(info, context);
  54. }
  55. public void OnDeserialization(object sender)
  56. {
  57. _hashSet.OnDeserialization(sender);
  58. }
  59. public void GetObjectData(SerializationInfo info, StreamingContext context)
  60. {
  61. _hashSet.GetObjectData(info, context);
  62. }
  63. IEnumerator IEnumerable.GetEnumerator()
  64. {
  65. return GetEnumerator();
  66. }
  67. public IEnumerator<T> GetEnumerator()
  68. {
  69. return _hashSet.GetEnumerator();
  70. }
  71. public void Add(T item)
  72. {
  73. _lock.EnterWriteLock();
  74. try
  75. {
  76. _hashSet.Add(item);
  77. }
  78. finally
  79. {
  80. if (_lock.IsWriteLockHeld)
  81. {
  82. _lock.ExitWriteLock();
  83. }
  84. }
  85. }
  86. public bool TryAdd(T item)
  87. {
  88. _lock.EnterWriteLock();
  89. try
  90. {
  91. return _hashSet.Add(item);
  92. }
  93. finally
  94. {
  95. if (_lock.IsWriteLockHeld)
  96. {
  97. _lock.ExitWriteLock();
  98. }
  99. }
  100. }
  101. public void UnionWith(IEnumerable<T> other)
  102. {
  103. _lock.EnterWriteLock();
  104. _lock.EnterReadLock();
  105. try
  106. {
  107. _hashSet.UnionWith(other);
  108. }
  109. finally
  110. {
  111. if (_lock.IsWriteLockHeld)
  112. {
  113. _lock.ExitWriteLock();
  114. }
  115. if (_lock.IsReadLockHeld)
  116. {
  117. _lock.ExitReadLock();
  118. }
  119. }
  120. }
  121. public void IntersectWith(IEnumerable<T> other)
  122. {
  123. _lock.EnterWriteLock();
  124. _lock.EnterReadLock();
  125. try
  126. {
  127. _hashSet.IntersectWith(other);
  128. }
  129. finally
  130. {
  131. if (_lock.IsWriteLockHeld)
  132. {
  133. _lock.ExitWriteLock();
  134. }
  135. if (_lock.IsReadLockHeld)
  136. {
  137. _lock.ExitReadLock();
  138. }
  139. }
  140. }
  141. public void ExceptWith(IEnumerable<T> other)
  142. {
  143. _lock.EnterWriteLock();
  144. _lock.EnterReadLock();
  145. try
  146. {
  147. _hashSet.ExceptWith(other);
  148. }
  149. finally
  150. {
  151. if (_lock.IsWriteLockHeld)
  152. {
  153. _lock.ExitWriteLock();
  154. }
  155. if (_lock.IsReadLockHeld)
  156. {
  157. _lock.ExitReadLock();
  158. }
  159. }
  160. }
  161. public void SymmetricExceptWith(IEnumerable<T> other)
  162. {
  163. _lock.EnterWriteLock();
  164. try
  165. {
  166. _hashSet.SymmetricExceptWith(other);
  167. }
  168. finally
  169. {
  170. if (_lock.IsWriteLockHeld)
  171. {
  172. _lock.ExitWriteLock();
  173. }
  174. }
  175. }
  176. public bool IsSubsetOf(IEnumerable<T> other)
  177. {
  178. _lock.EnterWriteLock();
  179. try
  180. {
  181. return _hashSet.IsSubsetOf(other);
  182. }
  183. finally
  184. {
  185. if (_lock.IsWriteLockHeld)
  186. {
  187. _lock.ExitWriteLock();
  188. }
  189. }
  190. }
  191. public bool IsSupersetOf(IEnumerable<T> other)
  192. {
  193. _lock.EnterWriteLock();
  194. try
  195. {
  196. return _hashSet.IsSupersetOf(other);
  197. }
  198. finally
  199. {
  200. if (_lock.IsWriteLockHeld)
  201. {
  202. _lock.ExitWriteLock();
  203. }
  204. }
  205. }
  206. public bool IsProperSupersetOf(IEnumerable<T> other)
  207. {
  208. _lock.EnterWriteLock();
  209. try
  210. {
  211. return _hashSet.IsProperSupersetOf(other);
  212. }
  213. finally
  214. {
  215. if (_lock.IsWriteLockHeld)
  216. {
  217. _lock.ExitWriteLock();
  218. }
  219. }
  220. }
  221. public bool IsProperSubsetOf(IEnumerable<T> other)
  222. {
  223. _lock.EnterWriteLock();
  224. try
  225. {
  226. return _hashSet.IsProperSubsetOf(other);
  227. }
  228. finally
  229. {
  230. if (_lock.IsWriteLockHeld)
  231. {
  232. _lock.ExitWriteLock();
  233. }
  234. }
  235. }
  236. public bool Overlaps(IEnumerable<T> other)
  237. {
  238. _lock.EnterWriteLock();
  239. try
  240. {
  241. return _hashSet.Overlaps(other);
  242. }
  243. finally
  244. {
  245. if (_lock.IsWriteLockHeld)
  246. {
  247. _lock.ExitWriteLock();
  248. }
  249. }
  250. }
  251. public bool SetEquals(IEnumerable<T> other)
  252. {
  253. _lock.EnterWriteLock();
  254. try
  255. {
  256. return _hashSet.SetEquals(other);
  257. }
  258. finally
  259. {
  260. if (_lock.IsWriteLockHeld)
  261. {
  262. _lock.ExitWriteLock();
  263. }
  264. }
  265. }
  266. bool ISet<T>.Add(T item)
  267. {
  268. _lock.EnterWriteLock();
  269. try
  270. {
  271. return _hashSet.Add(item);
  272. }
  273. finally
  274. {
  275. if (_lock.IsWriteLockHeld)
  276. {
  277. _lock.ExitWriteLock();
  278. }
  279. }
  280. }
  281. public void Clear()
  282. {
  283. _lock.EnterWriteLock();
  284. try
  285. {
  286. _hashSet.Clear();
  287. }
  288. finally
  289. {
  290. if (_lock.IsWriteLockHeld)
  291. {
  292. _lock.ExitWriteLock();
  293. }
  294. }
  295. }
  296. public bool Contains(T item)
  297. {
  298. _lock.EnterWriteLock();
  299. try
  300. {
  301. return _hashSet.Contains(item);
  302. }
  303. finally
  304. {
  305. if (_lock.IsWriteLockHeld)
  306. {
  307. _lock.ExitWriteLock();
  308. }
  309. }
  310. }
  311. public void CopyTo(T[] array, int arrayIndex)
  312. {
  313. _lock.EnterWriteLock();
  314. try
  315. {
  316. _hashSet.CopyTo(array, arrayIndex);
  317. }
  318. finally
  319. {
  320. if (_lock.IsWriteLockHeld)
  321. {
  322. _lock.ExitWriteLock();
  323. }
  324. }
  325. }
  326. public bool Remove(T item)
  327. {
  328. _lock.EnterWriteLock();
  329. try
  330. {
  331. return _hashSet.Remove(item);
  332. }
  333. finally
  334. {
  335. if (_lock.IsWriteLockHeld)
  336. {
  337. _lock.ExitWriteLock();
  338. }
  339. }
  340. }
  341. public void Dispose()
  342. {
  343. Dispose(true);
  344. GC.SuppressFinalize(this);
  345. }
  346. private void Dispose(bool disposing)
  347. {
  348. if (disposing && _lock != null)
  349. {
  350. _lock.Dispose();
  351. }
  352. }
  353. ~ConcurrentHashSet()
  354. {
  355. Dispose(false);
  356. }
  357. }