|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
-
- using System;
- using System.Collections.Generic;
- using System.Threading;
-
- namespace Svelto.DataStructures
- {
- /// <summary>
- /// original code: http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx
- /// simplified (not an IDictionary) and apdated (uses FasterList)
- /// </summary>
- /// <typeparam name = "TKey"></typeparam>
- /// <typeparam name = "TValue"></typeparam>
- [Serializable]
- public class ThreadSafeDictionary<TKey, TValue>
- {
- public ThreadSafeDictionary(int v)
- {
- dict = new Dictionary<TKey, TValue>(v);
- }
-
- public ThreadSafeDictionary()
- {
- dict = new Dictionary<TKey, TValue>();
- }
-
- // setup the lock;
- public virtual int Count
- {
- get
- {
- LockQ.EnterReadLock();
- try
- {
- return dict.Count;
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
- }
-
- public virtual bool IsReadOnly
- {
- get
- {
- LockQ.EnterReadLock();
- try
- {
- return dict.IsReadOnly;
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
- }
-
- public virtual FasterList<TKey> Keys
- {
- get
- {
- LockQ.EnterReadLock();
- try
- {
- return new FasterList<TKey>(dict.Keys);
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
- }
-
- public virtual FasterList<TValue> Values
- {
- get
- {
- LockQ.EnterReadLock();
- try
- {
- return new FasterList<TValue>(dict.Values);
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
- }
-
- public virtual TValue this[TKey key]
- {
- get
- {
- LockQ.EnterReadLock();
- try
- {
- return dict[key];
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
-
- set
- {
- LockQ.EnterWriteLock();
- try
- {
- dict[key] = value;
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
- }
-
- public virtual void Add(KeyValuePair<TKey, TValue> item)
- {
- LockQ.EnterWriteLock();
- try
- {
- dict.Add(item);
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
-
- public virtual void Clear()
- {
- LockQ.EnterWriteLock();
- try
- {
- dict.Clear();
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
-
- public virtual bool Contains(KeyValuePair<TKey, TValue> item)
- {
- LockQ.EnterReadLock();
- try
- {
- return dict.Contains(item);
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
-
- public virtual void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
- {
- LockQ.EnterReadLock();
- try
- {
- dict.CopyTo(array, arrayIndex);
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
-
- public virtual bool Remove(KeyValuePair<TKey, TValue> item)
- {
- LockQ.EnterWriteLock();
- try
- {
- return dict.Remove(item);
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
-
- public virtual void Add(TKey key, TValue value)
- {
- LockQ.EnterWriteLock();
- try
- {
- dict.Add(key, value);
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
-
- public virtual bool ContainsKey(TKey key)
- {
- LockQ.EnterReadLock();
- try
- {
- return dict.ContainsKey(key);
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
-
- public virtual bool Remove(TKey key)
- {
- LockQ.EnterWriteLock();
- try
- {
- return dict.Remove(key);
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
-
- public virtual bool TryGetValue(TKey key, out TValue value)
- {
- LockQ.EnterReadLock();
- try
- {
- return dict.TryGetValue(key, out value);
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
-
- /// <summary>
- /// Merge does a blind remove, and then add. Basically a blind Upsert.
- /// </summary>
- /// <param name = "key">Key to lookup</param>
- /// <param name = "newValue">New Value</param>
- public void MergeSafe(TKey key, TValue newValue)
- {
- LockQ.EnterWriteLock();
- try
- {
- // take a writelock immediately since we will always be writing
- if (dict.ContainsKey(key))
- dict.Remove(key);
-
- dict.Add(key, newValue);
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
-
- /// <summary>
- /// This is a blind remove. Prevents the need to check for existence first.
- /// </summary>
- /// <param name = "key">Key to remove</param>
- public void RemoveSafe(TKey key)
- {
- LockQ.EnterReadLock();
- try
- {
- if (dict.ContainsKey(key))
- LockQ.EnterWriteLock();
- try
- {
- dict.Remove(key);
- }
- finally
- {
- LockQ.ExitWriteLock();
- }
- }
- finally
- {
- LockQ.ExitReadLock();
- }
- }
-
- // This is the internal dictionary that we are wrapping
- readonly IDictionary<TKey, TValue> dict;
-
- readonly ReaderWriterLockSlim LockQ = new ReaderWriterLockSlim();
- }
- }
|