Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Collections;
- using System.Collections.ObjectModel;
- using System.Collections.Generic;
- namespace Uber.Collections
- {
- /// <summary>
- /// Implements a thread-safe list with support for multiple readers and one writer
- /// </summary>
- /// <typeparam name="T">Values</typeparam>
- ///
- public class SafeList<T> : IList<T>
- {
- private readonly List<T> _inner;
- [NonSerialized]
- private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
- public SafeList()
- {
- _inner = new List<T>();
- }
- public int Count
- {
- get
- {
- _lock.EnterReadLock();
- try
- {
- return _inner.Count;
- }
- finally { _lock.ExitReadLock(); }
- }
- }
- public bool IsReadOnly
- {
- get { return false; }
- }
- public T this[int index]
- {
- get
- {
- _lock.EnterReadLock();
- try
- {
- return _inner[index];
- }
- finally { _lock.ExitReadLock(); }
- }
- set
- {
- _lock.EnterWriteLock();
- try
- {
- _inner[index] = value;
- }
- finally { _lock.ExitWriteLock(); }
- }
- }
- IEnumerator<T> IEnumerable<T>.GetEnumerator()
- {
- _lock.EnterReadLock();
- try
- {
- return new SafeEnumerator<T>(_inner.GetEnumerator(), _lock);
- }
- finally { _lock.ExitReadLock(); }
- }
- public void Add(T item)
- {
- _lock.EnterWriteLock();
- try
- {
- _inner.Add(item);
- }
- finally { _lock.ExitWriteLock(); }
- }
- public void Clear()
- {
- _lock.EnterWriteLock();
- try
- {
- _inner.Clear();
- }
- finally { _lock.ExitWriteLock(); }
- }
- public bool Contains(T item)
- {
- _lock.EnterReadLock();
- try
- {
- return _inner.Contains(item);
- }
- finally { _lock.ExitReadLock(); }
- }
- public void CopyTo(T[] array, int arrayIndex)
- {
- _lock.EnterReadLock();
- try
- {
- _inner.CopyTo(array, arrayIndex);
- }
- finally { _lock.ExitReadLock(); }
- }
- public bool Remove(T item)
- {
- _lock.EnterWriteLock();
- try
- {
- return _inner.Remove(item);
- }
- finally { _lock.ExitWriteLock(); }
- }
- public IEnumerator GetEnumerator()
- {
- _lock.EnterReadLock();
- try
- {
- return new SafeEnumerator<T>(_inner.GetEnumerator(), _lock);
- }
- finally { _lock.ExitReadLock(); }
- }
- public int IndexOf(T item)
- {
- _lock.EnterReadLock();
- try
- {
- return _inner.IndexOf(item);
- }
- finally { _lock.ExitReadLock(); }
- }
- public void Insert(int index, T item)
- {
- _lock.EnterWriteLock();
- try
- {
- _inner.Insert(index, item);
- }
- finally { _lock.ExitWriteLock(); }
- }
- public void RemoveAt(int index)
- {
- _lock.EnterWriteLock();
- try
- {
- _inner.RemoveAt(index);
- }
- finally { _lock.ExitWriteLock(); }
- }
- public ReadOnlyCollection<T> AsReadOnly()
- {
- _lock.EnterReadLock();
- try
- {
- return new ReadOnlyCollection<T>(this);
- }
- finally { _lock.ExitReadLock(); }
- }
- public void ForEach(Action<T> action)
- {
- _lock.EnterReadLock();
- try
- {
- foreach (var item in _inner)
- {
- action(item);
- }
- }
- finally { _lock.ExitReadLock(); }
- }
- public bool Exists(Predicate<T> match)
- {
- _lock.EnterReadLock();
- try
- {
- foreach (var item in _inner)
- {
- if (match(item))
- {
- return true;
- }
- }
- }
- finally { _lock.ExitReadLock(); }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement