wasSharp – Diff between revs 40 and 44
?pathlinks?
Rev 40 | Rev 44 | |||
---|---|---|---|---|
Line 7... | Line 7... | |||
7 | |
7 | |
|
8 | using System; |
8 | using System; |
|
9 | using System.Collections; |
9 | using System.Collections; |
|
10 | using System.Collections.Generic; |
10 | using System.Collections.Generic; |
|
11 | using System.Threading; |
- | ||
Line 12... | Line 11... | |||
12 | using System.Linq; |
11 | using System.Threading; |
|
13 | |
12 | |
|
14 | namespace wasSharp.Collections.Specialized |
13 | namespace wasSharp.Collections.Specialized |
|
15 | { |
14 | { |
|
Line 19... | Line 18... | |||
19 | { |
18 | { |
|
20 | return new ConcurrentHashSet<T>(enumerable); |
19 | return new ConcurrentHashSet<T>(enumerable); |
|
21 | } |
20 | } |
|
22 | } |
21 | } |
|
Line 23... | Line 22... | |||
23 | |
22 | |
|
24 | public class ConcurrentHashSet<T> : IDisposable, ICollection<T>, IEnumerable<T>, ISet<T> |
23 | public class ConcurrentHashSet<T> : IDisposable, ISet<T> |
|
25 | { |
24 | { |
|
26 | private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
25 | private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
|
27 | private readonly HashSet<T> _hashSet = null; |
- | ||
Line 28... | Line 26... | |||
28 | private IEnumerable<string> enumerable; |
26 | private readonly HashSet<T> _hashSet; |
|
29 | |
27 | |
|
30 | public ConcurrentHashSet() |
28 | public ConcurrentHashSet() |
|
31 | { |
29 | { |
|
Line 182... | Line 180... | |||
182 | public IEnumerator<T> GetEnumerator() |
180 | public IEnumerator<T> GetEnumerator() |
|
183 | { |
181 | { |
|
184 | _lock.EnterReadLock(); |
182 | _lock.EnterReadLock(); |
|
185 | try |
183 | try |
|
186 | { |
184 | { |
|
187 | return _hashSet.GetEnumerator(); |
185 | using (var enumerator = _hashSet.GetEnumerator()) |
|
- | 186 | { |
||
- | 187 | while (enumerator.MoveNext()) |
||
- | 188 | yield return enumerator.Current; |
||
- | 189 | } |
||
188 | } |
190 | } |
|
189 | finally |
191 | finally |
|
190 | { |
192 | { |
|
191 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
193 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
|
192 | } |
194 | } |
|
Line 195... | Line 197... | |||
195 | IEnumerator IEnumerable.GetEnumerator() |
197 | IEnumerator IEnumerable.GetEnumerator() |
|
196 | { |
198 | { |
|
197 | _lock.EnterReadLock(); |
199 | _lock.EnterReadLock(); |
|
198 | try |
200 | try |
|
199 | { |
201 | { |
|
200 | return _hashSet.GetEnumerator(); |
202 | using (var enumerator = _hashSet.GetEnumerator()) |
|
- | 203 | { |
||
- | 204 | while (enumerator.MoveNext()) |
||
- | 205 | yield return enumerator.Current; |
||
- | 206 | } |
||
201 | } |
207 | } |
|
202 | finally |
208 | finally |
|
203 | { |
209 | { |
|
204 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
210 | if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
|
205 | } |
211 | } |
|
206 | } |
212 | } |
|
Line 207... | Line -... | |||
207 | |
- | ||
208 | public bool IsReadOnly |
- | ||
209 | { |
- | ||
210 | get |
- | ||
211 | { |
213 | |
|
212 | return ((ICollection<T>)_hashSet).IsReadOnly; |
- | ||
213 | } |
- | ||
Line 214... | Line 214... | |||
214 | } |
214 | public bool IsReadOnly => ((ICollection<T>)_hashSet).IsReadOnly; |
|
Line 215... | Line 215... | |||
215 | |
215 | |
|
Line 223... | Line 223... | |||
223 | GC.SuppressFinalize(this); |
223 | GC.SuppressFinalize(this); |
|
224 | } |
224 | } |
|
Line 225... | Line 225... | |||
225 | |
225 | |
|
226 | protected virtual void Dispose(bool disposing) |
226 | protected virtual void Dispose(bool disposing) |
|
227 | { |
227 | { |
|
228 | if (disposing) |
- | ||
229 | { |
- | ||
230 | if (_lock != null) |
228 | if (!disposing) return; |
|
231 | _lock.Dispose(); |
229 | _lock?.Dispose(); |
|
232 | _hashSet.Clear(); |
- | ||
233 | } |
230 | _hashSet.Clear(); |
|
Line 234... | Line 231... | |||
234 | } |
231 | } |
|
235 | |
232 | |
|
236 | ~ConcurrentHashSet() |
233 | ~ConcurrentHashSet() |