using System; using System.Threading; using System.Collections; using System.Collections.Generic; namespace DuckEmulator.Security { /// /// Implements a thread-safe dictionary with multiple readers and one writer /// /// Key /// Value /// public sealed class SecurityDictionary : IDictionary, IDisposable { private readonly Dictionary _inner; private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); public SecurityDictionary() { _inner = new Dictionary(); } public SecurityDictionary(int capacity) { _inner = new Dictionary(capacity); } public SecurityDictionary(SecurityDictionary original) { _inner = new Dictionary(original); } public void Dispose() { _lock.Dispose(); } public int Count { get { using (RWLSExtension.ReadLock(_lock)) { return _inner.Count; } } } public bool IsReadOnly { get { return false; } } public void Clear() { using (RWLSExtension.WriteLock(_lock)) { _inner.Clear(); } } public void Add(TKey Item, TValue Value) { using (RWLSExtension.WriteLock(_lock)) { _inner.Add(Item, Value); } } public void Remove(TKey Item) { using (RWLSExtension.WriteLock(_lock)) { _inner.Remove(Item); } } public bool ContainsKey(TKey Item) { using (RWLSExtension.ReadLock(_lock)) { return _inner.ContainsKey(Item); } } public ICollection Keys { get { using (RWLSExtension.ReadLock(_lock)) { return new List(_inner.Keys); } } } public ICollection Values { get { using (RWLSExtension.ReadLock(_lock)) { return new List(_inner.Values); } } } public IEnumerator> GetEnumerator() { using (RWLSExtension.ReadLock(_lock)) { return new Dictionary(_inner).GetEnumerator(); } } public TValue this[TKey Item] { get { using (RWLSExtension.ReadLock(_lock)) { return _inner[Item]; } } set { using (RWLSExtension.WriteLock(_lock)) { _inner[Item] = value; } } } bool IDictionary.Remove(TKey key) { using (RWLSExtension.WriteLock(_lock)) { return _inner.Remove(key); } } IEnumerator IEnumerable.GetEnumerator() { using (RWLSExtension.ReadLock(_lock)) { return new Dictionary(_inner).GetEnumerator(); } } public bool TryGetValue(TKey key, out TValue value) { throw new NotImplementedException(); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { throw new NotImplementedException(); } public void Add(KeyValuePair item) { throw new NotImplementedException(); } public bool Contains(KeyValuePair item) { throw new NotImplementedException(); } public bool Remove(KeyValuePair item) { throw new NotImplementedException(); } } }