Remove denepdency on Nito.AsyncEx.Coordination

We don't really need async operations here, ReaderWriterLockSlim is enough
This commit is contained in:
JustArchi
2020-04-10 21:18:45 +02:00
parent d57662811e
commit e05ea63e08
3 changed files with 69 additions and 19 deletions

View File

@@ -60,7 +60,6 @@
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" />
<PackageReference Include="Markdig.Signed" Version="0.18.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.0.0" />
<PackageReference Include="NLog" Version="4.7.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.1" />
<PackageReference Include="SteamKit2" Version="2.3.0-Beta.1" />

View File

@@ -22,6 +22,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Collections {
@@ -29,22 +30,24 @@ namespace ArchiSteamFarm.Collections {
public T Current => Enumerator.Current;
private readonly IEnumerator<T> Enumerator;
private readonly IDisposable Lock;
private readonly ReaderWriterLockSlim Lock;
object IEnumerator.Current => Current;
internal ConcurrentEnumerator([NotNull] IReadOnlyCollection<T> collection, [NotNull] IDisposable @lock) {
internal ConcurrentEnumerator([NotNull] IReadOnlyCollection<T> collection, [NotNull] ReaderWriterLockSlim @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.Dispose();
Lock.ExitReadLock();
}
public bool MoveNext() => Enumerator.MoveNext();

View File

@@ -22,7 +22,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Nito.AsyncEx;
using System.Threading;
namespace ArchiSteamFarm.Collections {
internal sealed class ConcurrentList<T> : IList<T>, IReadOnlyList<T> {
@@ -30,81 +30,125 @@ namespace ArchiSteamFarm.Collections {
internal int Count {
get {
using (Lock.ReaderLock()) {
Lock.EnterReadLock();
try {
return BackingCollection.Count;
} finally {
Lock.ExitReadLock();
}
}
}
private readonly List<T> BackingCollection = new List<T>();
private readonly AsyncReaderWriterLock Lock = new AsyncReaderWriterLock();
private readonly ReaderWriterLockSlim Lock = new ReaderWriterLockSlim();
int ICollection<T>.Count => Count;
int IReadOnlyCollection<T>.Count => Count;
public T this[int index] {
get {
using (Lock.ReaderLock()) {
Lock.EnterReadLock();
try {
return BackingCollection[index];
} finally {
Lock.ExitReadLock();
}
}
set {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
BackingCollection[index] = value;
} finally {
Lock.ExitWriteLock();
}
}
}
public void Add(T item) {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
BackingCollection.Add(item);
} finally {
Lock.ExitWriteLock();
}
}
public void Clear() {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
BackingCollection.Clear();
} finally {
Lock.ExitWriteLock();
}
}
public bool Contains(T item) {
using (Lock.ReaderLock()) {
Lock.EnterReadLock();
try {
return BackingCollection.Contains(item);
} finally {
Lock.ExitReadLock();
}
}
public void CopyTo(T[] array, int arrayIndex) {
using (Lock.ReaderLock()) {
Lock.EnterReadLock();
try {
BackingCollection.CopyTo(array, arrayIndex);
} finally {
Lock.ExitReadLock();
}
}
[JetBrains.Annotations.NotNull]
[SuppressMessage("ReSharper", "AnnotationRedundancyInHierarchy")]
public IEnumerator<T> GetEnumerator() => new ConcurrentEnumerator<T>(BackingCollection, Lock.ReaderLock());
public IEnumerator<T> GetEnumerator() => new ConcurrentEnumerator<T>(BackingCollection, Lock);
public int IndexOf(T item) {
using (Lock.ReaderLock()) {
Lock.EnterReadLock();
try {
return BackingCollection.IndexOf(item);
} finally {
Lock.ExitReadLock();
}
}
public void Insert(int index, T item) {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
BackingCollection.Insert(index, item);
} finally {
Lock.ExitWriteLock();
}
}
public bool Remove(T item) {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
return BackingCollection.Remove(item);
} finally {
Lock.ExitWriteLock();
}
}
public void RemoveAt(int index) {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
BackingCollection.RemoveAt(index);
} finally {
Lock.ExitWriteLock();
}
}
@@ -113,9 +157,13 @@ namespace ArchiSteamFarm.Collections {
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
internal void ReplaceWith([JetBrains.Annotations.NotNull] IEnumerable<T> collection) {
using (Lock.WriterLock()) {
Lock.EnterWriteLock();
try {
BackingCollection.Clear();
BackingCollection.AddRange(collection);
} finally {
Lock.ExitWriteLock();
}
}
}