Pull ConcurrentList<T> from ArchiBot

Previous implementation of ConcurrentSortedHashSet was prone to deadlocks and very suboptimal, on top of no guarantee to do what it claimed to.
This commit is contained in:
JustArchi
2019-07-28 22:17:41 +02:00
parent 214a6a68c7
commit fbe41b12a2
5 changed files with 151 additions and 256 deletions

View File

@@ -22,7 +22,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Collections {
@@ -30,22 +29,20 @@ namespace ArchiSteamFarm.Collections {
public T Current => Enumerator.Current;
private readonly IEnumerator<T> Enumerator;
private readonly SemaphoreSlim Semaphore;
private readonly IDisposable Lock;
object IEnumerator.Current => Current;
internal ConcurrentEnumerator([NotNull] IReadOnlyCollection<T> collection, [NotNull] SemaphoreSlim semaphore) {
if ((collection == null) || (semaphore == null)) {
throw new ArgumentNullException(nameof(collection) + " || " + nameof(semaphore));
internal ConcurrentEnumerator([NotNull] IReadOnlyCollection<T> collection, [NotNull] IDisposable @lock) {
if ((collection == null) || (@lock == null)) {
throw new ArgumentNullException(nameof(collection) + " || " + nameof(@lock));
}
Semaphore = semaphore;
semaphore.Wait();
Lock = @lock;
Enumerator = collection.GetEnumerator();
}
public void Dispose() => Semaphore.Release();
public void Dispose() => Lock.Dispose();
public bool MoveNext() => Enumerator.MoveNext();
public void Reset() => Enumerator.Reset();
}