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