269 lines
6.8 KiB
C#
Executable File
269 lines
6.8 KiB
C#
Executable File
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared.Data
|
|
{
|
|
|
|
internal class BlockingDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
|
|
{
|
|
private readonly IDictionary<TKey, TValue> _dic;
|
|
|
|
private readonly ReaderWriterLockSlim _locker;
|
|
|
|
public ICollection<TKey> Keys
|
|
{
|
|
get
|
|
{
|
|
_locker.EnterReadLock();
|
|
List<TKey> result = _dic.Keys.ToList();
|
|
_locker.ExitReadLock();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public ICollection<TValue> Values
|
|
{
|
|
get
|
|
{
|
|
_locker.EnterReadLock();
|
|
List<TValue> result = _dic.Values.ToList();
|
|
_locker.ExitReadLock();
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public TValue this[TKey key]
|
|
{
|
|
get
|
|
{
|
|
_locker.EnterReadLock();
|
|
try
|
|
{
|
|
return _dic[key];
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitReadLock();
|
|
}
|
|
}
|
|
set
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
_dic[key] = value;
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
}
|
|
|
|
public int Count
|
|
{
|
|
get
|
|
{
|
|
_locker.EnterReadLock();
|
|
int count = _dic.Count;
|
|
_locker.ExitReadLock();
|
|
return count;
|
|
}
|
|
}
|
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
|
|
|
|
public void Add(TKey key, TValue value)
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
_dic.Add(key, value);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
public bool ContainsKey(TKey key)
|
|
{
|
|
_locker.EnterReadLock();
|
|
try
|
|
{
|
|
return _dic.ContainsKey(key);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitReadLock();
|
|
}
|
|
}
|
|
|
|
public bool Remove(TKey key)
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
return _dic.Remove(key);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
public bool TryGetValue(TKey key, out TValue value)
|
|
{
|
|
_locker.EnterReadLock();
|
|
try
|
|
{
|
|
return _dic.TryGetValue(key, out value);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitReadLock();
|
|
}
|
|
}
|
|
|
|
public TValue GetOrAdd(TKey key, TValue value)
|
|
{
|
|
if (TryGetValue(key, out var value2))
|
|
{
|
|
return value2;
|
|
}
|
|
return GetOrAddInternal(key, (TKey _) => value);
|
|
}
|
|
|
|
public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
|
|
{
|
|
if (TryGetValue(key, out var value))
|
|
{
|
|
return value;
|
|
}
|
|
return GetOrAddInternal(key, valueFactory);
|
|
}
|
|
|
|
public TValue GetOrAddInternal(TKey key, Func<TKey, TValue> valueFactory)
|
|
{
|
|
_locker.EnterUpgradeableReadLock();
|
|
try
|
|
{
|
|
if (_dic.TryGetValue(key, out var value))
|
|
{
|
|
return value;
|
|
}
|
|
TValue val = valueFactory(key);
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
_dic.Add(key, val);
|
|
return val;
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitUpgradeableReadLock();
|
|
}
|
|
}
|
|
|
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
|
{
|
|
_locker.EnterReadLock();
|
|
foreach (KeyValuePair<TKey, TValue> item in _dic)
|
|
{
|
|
yield return item;
|
|
}
|
|
_locker.ExitReadLock();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
_dic.Clear();
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
_dic.Add(item);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
_locker.EnterReadLock();
|
|
try
|
|
{
|
|
return _dic.Contains(item);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitReadLock();
|
|
}
|
|
}
|
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
_dic.CopyTo(array, arrayIndex);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
_locker.EnterWriteLock();
|
|
try
|
|
{
|
|
return _dic.Remove(item);
|
|
}
|
|
finally
|
|
{
|
|
_locker.ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
public BlockingDictionary()
|
|
{
|
|
_dic = new Dictionary<TKey, TValue>();
|
|
_locker = new ReaderWriterLockSlim();
|
|
}
|
|
|
|
public BlockingDictionary(int capacity)
|
|
{
|
|
_dic = new Dictionary<TKey, TValue>(capacity);
|
|
_locker = new ReaderWriterLockSlim();
|
|
}
|
|
}
|
|
}
|