From 0ca9020830eb5f5b9a476e8a84f94cb5ce41813a Mon Sep 17 00:00:00 2001 From: shangfengh <3495281661@qq.com> Date: Tue, 7 Nov 2023 19:47:59 +0800 Subject: [PATCH] feat(SafeValue): :sparkles: Add the SafeValue LockedClassList --- logic/Preparation/Utility/SafeValue/Atomic.cs | 3 +- .../Utility/SafeValue/ListLocked.cs | 164 ++++++++++++++++++ 2 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 logic/Preparation/Utility/SafeValue/ListLocked.cs diff --git a/logic/Preparation/Utility/SafeValue/Atomic.cs b/logic/Preparation/Utility/SafeValue/Atomic.cs index dc9c570..5f9b31e 100644 --- a/logic/Preparation/Utility/SafeValue/Atomic.cs +++ b/logic/Preparation/Utility/SafeValue/Atomic.cs @@ -1,5 +1,4 @@ -using Google.Protobuf.WellKnownTypes; -using System; +using System; using System.Threading; namespace Preparation.Utility diff --git a/logic/Preparation/Utility/SafeValue/ListLocked.cs b/logic/Preparation/Utility/SafeValue/ListLocked.cs new file mode 100644 index 0000000..4705089 --- /dev/null +++ b/logic/Preparation/Utility/SafeValue/ListLocked.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace Preparation.Utility +{ + public class LockedClassList where T : class + { + private readonly ReaderWriterLockSlim listLock = new(); + private List list; + + #region 构造 + public LockedList() + { + list = new List(); + } + public LockedList(int capacity) + { + list = new List(capacity); + } + public LockedList(IEnumerable collection) + { + list = new List(collection); + } + #endregion + + #region 修改 + public TResult WriteLock(Func func) + { + listLock.EnterWriteLock(); + try + { + return func(); + } + finally + { + listLock.ExitWriteLock(); + } + + } + public void WriteLock(Action func) + { + listLock.EnterWriteLock(); + try + { + func(); + } + finally + { + listLock.ExitWriteLock(); + } + + } + + public void Add(T item) + { + WriteLock(() => { list.Add(item); }); + } + + public void Insert(int index, T item) + { + WriteLock(() => { list.Insert(index, item); }); + } + + public void Clear() + { + WriteLock(() => { list.Clear(); }); + } + + public bool Remove(T item) + { + return WriteLock(() => { return list.Remove(item); }); + } + + public int RemoveAll(T item) => WriteLock(() => { return list.RemoveAll((t) => { return t == item; }); }); + + public bool RemoveOne(Predicate match) => + WriteLock(() => + { + int index = list.FindIndex(match); + if (index == -1) return false; + list.RemoveAt(index); + return true; + }); + + public int RemoveAll(Predicate match) => WriteLock(() => { return list.RemoveAll(match); }); + + public bool RemoveAt(int index) + { + return WriteLock(() => + { + if (index > list.Count) return false; + list.RemoveAt(index); + return true; + }); + } + #endregion + + #region 读取与对类操作 + public TResult ReadLock(Func func) + { + listLock.EnterReadLock(); + try + { + return func(); + } + finally + { + listLock.ExitReadLock(); + } + } + public void ReadLock(Action func) + { + listLock.EnterReadLock(); + try + { + func(); + } + finally + { + listLock.ExitReadLock(); + } + + } + + public T this[int index] + { + get + { + return ReadLock(() => { return list[index]; }); + } + set + { + ReadLock(() => { list[index] = value!; }); + } + } + public int Count => ReadLock(() => { return list.Count; }); + + public int IndexOf(T item) + { + return ReadLock(() => { return list.IndexOf(item); }); + } + + public bool Contains(T item) + { + return ReadLock(() => { return list.Contains(item); }); + } + + public T? Find(Predicate match) + { + return ReadLock(() => { return list.Find(match); }); + } + + public List FindAll(Predicate match) + { + return ReadLock(() => { return list.FindAll(match); }); + } + + public int FindIndex(Predicate match) => ReadLock(() => { return list.FindIndex(match); }); + + public void ForEach(Action action) => ReadLock(() => { list.ForEach(action); }); + #endregion + } +} \ No newline at end of file