/Collections/Specialized/ConcurrentList.cs |
@@ -21,13 +21,13 @@ |
public class ConcurrentList<T> : IList<T> |
{ |
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
private readonly List<T> _list = null; |
private readonly List<T> _list; |
|
public ConcurrentList(IEnumerable<T> v) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list = new List<T>(); |
foreach (var i in v) |
{ |
@@ -42,9 +42,9 @@ |
|
public ConcurrentList() |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list = new List<T>(); |
} |
finally |
@@ -58,9 +58,15 @@ |
get |
{ |
_lock.EnterReadLock(); |
var c = _list.Count; |
_lock.ExitReadLock(); |
return c; |
try |
{ |
return _list.Count; |
} |
finally |
{ |
if (_lock.IsReadLockHeld) _lock.ExitReadLock(); |
} |
|
} |
} |
|
@@ -68,9 +74,9 @@ |
{ |
get |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
return ((IList<T>)_list).IsReadOnly; |
} |
finally |
@@ -84,9 +90,9 @@ |
{ |
get |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
return _list[index]; |
} |
finally |
@@ -97,9 +103,9 @@ |
|
set |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list[index] = value; |
} |
finally |
@@ -111,9 +117,9 @@ |
|
public int IndexOf(T item) |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
return _list.IndexOf(item); |
} |
finally |
@@ -124,9 +130,9 @@ |
|
public void Insert(int index, T item) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list.Insert(index, item); |
} |
finally |
@@ -137,9 +143,9 @@ |
|
public void RemoveAt(int index) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list.RemoveAt(index); |
} |
finally |
@@ -150,9 +156,9 @@ |
|
public void Add(T item) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list.Add(item); |
} |
finally |
@@ -163,9 +169,9 @@ |
|
public void Clear() |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
_list.Clear(); |
} |
finally |
@@ -176,9 +182,9 @@ |
|
public bool Contains(T item) |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
return _list.Contains(item); |
} |
finally |
@@ -189,9 +195,9 @@ |
|
public void CopyTo(T[] array, int arrayIndex) |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
_list.CopyTo(array, arrayIndex); |
} |
finally |
@@ -202,9 +208,9 @@ |
|
public bool Remove(T item) |
{ |
_lock.EnterWriteLock(); |
try |
{ |
_lock.EnterWriteLock(); |
return _list.Remove(item); |
} |
finally |
@@ -215,10 +221,14 @@ |
|
public IEnumerator<T> GetEnumerator() |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
return ((IList<T>)_list).GetEnumerator(); |
using (var enumerator = _list.GetEnumerator()) |
{ |
while (enumerator.MoveNext()) |
yield return enumerator.Current; |
} |
} |
finally |
{ |
@@ -228,10 +238,14 @@ |
|
IEnumerator IEnumerable.GetEnumerator() |
{ |
_lock.EnterReadLock(); |
try |
{ |
_lock.EnterReadLock(); |
return ((IList<T>)_list).GetEnumerator(); |
using (var enumerator = _list.GetEnumerator()) |
{ |
while (enumerator.MoveNext()) |
yield return enumerator.Current; |
} |
} |
finally |
{ |