Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections;
  4. using System.Collections.ObjectModel;
  5. using System.Collections.Generic;
  6.  
  7. namespace Uber.Collections
  8. {
  9.     /// <summary>
  10.     /// Implements a thread-safe list with support for multiple readers and one writer
  11.     /// </summary>
  12.     /// <typeparam name="T">Values</typeparam>
  13.     ///
  14.     public class SafeList<T> : IList<T>
  15.     {
  16.         private readonly List<T> _inner;
  17.  
  18.         [NonSerialized]
  19.         private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
  20.  
  21.         public SafeList()
  22.         {
  23.             _inner = new List<T>();
  24.         }
  25.  
  26.         public int Count
  27.         {
  28.             get
  29.             {
  30.                 _lock.EnterReadLock();
  31.                 try
  32.                 {
  33.                     return _inner.Count;
  34.                 }
  35.                 finally { _lock.ExitReadLock(); }
  36.             }
  37.         }
  38.  
  39.         public bool IsReadOnly
  40.         {
  41.             get { return false; }
  42.         }
  43.  
  44.         public T this[int index]
  45.         {
  46.             get
  47.             {
  48.                 _lock.EnterReadLock();
  49.                 try
  50.                 {
  51.                     return _inner[index];
  52.                 }
  53.                 finally { _lock.ExitReadLock(); }
  54.             }
  55.             set
  56.             {
  57.                 _lock.EnterWriteLock();
  58.                 try
  59.                 {
  60.                     _inner[index] = value;
  61.                 }
  62.                 finally { _lock.ExitWriteLock(); }
  63.             }
  64.         }
  65.  
  66.         IEnumerator<T> IEnumerable<T>.GetEnumerator()
  67.         {
  68.             _lock.EnterReadLock();
  69.             try
  70.             {
  71.                 return new SafeEnumerator<T>(_inner.GetEnumerator(), _lock);
  72.             }
  73.             finally { _lock.ExitReadLock(); }
  74.         }
  75.  
  76.         public void Add(T item)
  77.         {
  78.             _lock.EnterWriteLock();
  79.             try
  80.             {
  81.                 _inner.Add(item);
  82.             }
  83.             finally { _lock.ExitWriteLock(); }
  84.         }
  85.  
  86.         public void Clear()
  87.         {
  88.             _lock.EnterWriteLock();
  89.             try
  90.             {
  91.                 _inner.Clear();
  92.             }
  93.             finally { _lock.ExitWriteLock(); }
  94.         }
  95.  
  96.         public bool Contains(T item)
  97.         {
  98.             _lock.EnterReadLock();
  99.             try
  100.             {
  101.                 return _inner.Contains(item);
  102.             }
  103.             finally { _lock.ExitReadLock(); }
  104.         }
  105.  
  106.         public void CopyTo(T[] array, int arrayIndex)
  107.         {
  108.             _lock.EnterReadLock();
  109.             try
  110.             {
  111.                 _inner.CopyTo(array, arrayIndex);
  112.             }
  113.             finally { _lock.ExitReadLock(); }
  114.         }
  115.  
  116.         public bool Remove(T item)
  117.         {
  118.             _lock.EnterWriteLock();
  119.             try
  120.             {
  121.                 return _inner.Remove(item);
  122.             }
  123.             finally { _lock.ExitWriteLock(); }
  124.         }
  125.  
  126.         public IEnumerator GetEnumerator()
  127.         {
  128.             _lock.EnterReadLock();
  129.             try
  130.             {
  131.                 return new SafeEnumerator<T>(_inner.GetEnumerator(), _lock);
  132.             }
  133.             finally { _lock.ExitReadLock(); }
  134.         }
  135.  
  136.         public int IndexOf(T item)
  137.         {
  138.             _lock.EnterReadLock();
  139.             try
  140.             {
  141.                 return _inner.IndexOf(item);
  142.             }
  143.             finally { _lock.ExitReadLock(); }
  144.         }
  145.  
  146.         public void Insert(int index, T item)
  147.         {
  148.             _lock.EnterWriteLock();
  149.             try
  150.             {
  151.                 _inner.Insert(index, item);
  152.             }
  153.             finally { _lock.ExitWriteLock(); }
  154.         }
  155.  
  156.         public void RemoveAt(int index)
  157.         {
  158.             _lock.EnterWriteLock();
  159.             try
  160.             {
  161.                 _inner.RemoveAt(index);
  162.             }
  163.             finally { _lock.ExitWriteLock(); }
  164.         }
  165.  
  166.         public ReadOnlyCollection<T> AsReadOnly()
  167.         {
  168.             _lock.EnterReadLock();
  169.             try
  170.             {
  171.                 return new ReadOnlyCollection<T>(this);
  172.             }
  173.             finally { _lock.ExitReadLock(); }
  174.         }
  175.  
  176.         public void ForEach(Action<T> action)
  177.         {
  178.             _lock.EnterReadLock();
  179.             try
  180.             {
  181.                 foreach (var item in _inner)
  182.                 {
  183.                     action(item);
  184.                 }
  185.             }
  186.             finally { _lock.ExitReadLock(); }
  187.         }
  188.  
  189.         public bool Exists(Predicate<T> match)
  190.         {
  191.             _lock.EnterReadLock();
  192.             try
  193.             {
  194.                 foreach (var item in _inner)
  195.                 {
  196.                     if (match(item))
  197.                     {
  198.                         return true;
  199.                     }
  200.                 }
  201.             }
  202.             finally { _lock.ExitReadLock(); }
  203.  
  204.             return false;
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement