From 876d275400885a65922f34b625e3cbd3a3e98a02 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 10 Apr 2020 21:19:39 +0200 Subject: [PATCH] Revert "Remove denepdency on Nito.AsyncEx.Coordination" This reverts commit e05ea63e0869aebb80b0fe568c19bdaea6e33f84. --- ArchiSteamFarm/ArchiSteamFarm.csproj | 1 + .../Collections/ConcurrentEnumerator.cs | 9 +-- ArchiSteamFarm/Collections/ConcurrentList.cs | 78 ++++--------------- 3 files changed, 19 insertions(+), 69 deletions(-) diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index ecc48e962..6ae56e07b 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -60,6 +60,7 @@ + diff --git a/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs b/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs index 8a4bbc06d..04e489291 100644 --- a/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs +++ b/ArchiSteamFarm/Collections/ConcurrentEnumerator.cs @@ -22,7 +22,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Threading; using JetBrains.Annotations; namespace ArchiSteamFarm.Collections { @@ -30,24 +29,22 @@ namespace ArchiSteamFarm.Collections { public T Current => Enumerator.Current; private readonly IEnumerator Enumerator; - private readonly ReaderWriterLockSlim Lock; + private readonly IDisposable Lock; object IEnumerator.Current => Current; - internal ConcurrentEnumerator([NotNull] IReadOnlyCollection collection, [NotNull] ReaderWriterLockSlim @lock) { + internal ConcurrentEnumerator([NotNull] IReadOnlyCollection collection, [NotNull] IDisposable @lock) { if ((collection == null) || (@lock == null)) { throw new ArgumentNullException(nameof(collection) + " || " + nameof(@lock)); } - @lock.EnterReadLock(); - Lock = @lock; Enumerator = collection.GetEnumerator(); } public void Dispose() { Enumerator.Dispose(); - Lock.ExitReadLock(); + Lock.Dispose(); } public bool MoveNext() => Enumerator.MoveNext(); diff --git a/ArchiSteamFarm/Collections/ConcurrentList.cs b/ArchiSteamFarm/Collections/ConcurrentList.cs index 2303c97ed..ac2878955 100644 --- a/ArchiSteamFarm/Collections/ConcurrentList.cs +++ b/ArchiSteamFarm/Collections/ConcurrentList.cs @@ -22,7 +22,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Threading; +using Nito.AsyncEx; namespace ArchiSteamFarm.Collections { internal sealed class ConcurrentList : IList, IReadOnlyList { @@ -30,125 +30,81 @@ namespace ArchiSteamFarm.Collections { internal int Count { get { - Lock.EnterReadLock(); - - try { + using (Lock.ReaderLock()) { return BackingCollection.Count; - } finally { - Lock.ExitReadLock(); } } } private readonly List BackingCollection = new List(); - private readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim(); + private readonly AsyncReaderWriterLock Lock = new AsyncReaderWriterLock(); int ICollection.Count => Count; int IReadOnlyCollection.Count => Count; public T this[int index] { get { - Lock.EnterReadLock(); - - try { + using (Lock.ReaderLock()) { return BackingCollection[index]; - } finally { - Lock.ExitReadLock(); } } set { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { BackingCollection[index] = value; - } finally { - Lock.ExitWriteLock(); } } } public void Add(T item) { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { BackingCollection.Add(item); - } finally { - Lock.ExitWriteLock(); } } public void Clear() { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { BackingCollection.Clear(); - } finally { - Lock.ExitWriteLock(); } } public bool Contains(T item) { - Lock.EnterReadLock(); - - try { + using (Lock.ReaderLock()) { return BackingCollection.Contains(item); - } finally { - Lock.ExitReadLock(); } } public void CopyTo(T[] array, int arrayIndex) { - Lock.EnterReadLock(); - - try { + using (Lock.ReaderLock()) { BackingCollection.CopyTo(array, arrayIndex); - } finally { - Lock.ExitReadLock(); } } [JetBrains.Annotations.NotNull] [SuppressMessage("ReSharper", "AnnotationRedundancyInHierarchy")] - public IEnumerator GetEnumerator() => new ConcurrentEnumerator(BackingCollection, Lock); + public IEnumerator GetEnumerator() => new ConcurrentEnumerator(BackingCollection, Lock.ReaderLock()); public int IndexOf(T item) { - Lock.EnterReadLock(); - - try { + using (Lock.ReaderLock()) { return BackingCollection.IndexOf(item); - } finally { - Lock.ExitReadLock(); } } public void Insert(int index, T item) { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { BackingCollection.Insert(index, item); - } finally { - Lock.ExitWriteLock(); } } public bool Remove(T item) { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { return BackingCollection.Remove(item); - } finally { - Lock.ExitWriteLock(); } } public void RemoveAt(int index) { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { BackingCollection.RemoveAt(index); - } finally { - Lock.ExitWriteLock(); } } @@ -157,13 +113,9 @@ namespace ArchiSteamFarm.Collections { IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); internal void ReplaceWith([JetBrains.Annotations.NotNull] IEnumerable collection) { - Lock.EnterWriteLock(); - - try { + using (Lock.WriterLock()) { BackingCollection.Clear(); BackingCollection.AddRange(collection); - } finally { - Lock.ExitWriteLock(); } } }