2017-10-17 10:34:07 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.FlowHelpers
|
|
|
|
|
{
|
2019-08-15 11:26:55 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Implementation of an asynchronous locking mechanism.
|
|
|
|
|
/// </summary>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
public class LockCollection<T> where T : notnull
|
2017-10-17 10:34:07 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<T, LockItem> locks;
|
|
|
|
|
|
2021-05-29 21:51:58 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
2017-10-17 10:34:07 +02:00
|
|
|
|
public LockCollection(IEqualityComparer<T> comparer)
|
|
|
|
|
{
|
|
|
|
|
locks = new Dictionary<T, LockItem>(comparer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 11:26:55 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Waits for and acquires a lock on the specified key. Dispose the returned value to release the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key"></param>
|
2017-10-17 10:34:07 +02:00
|
|
|
|
public Task<IDisposable> GetLock(T key)
|
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
// ReSharper disable once InconsistentlySynchronizedField - by design
|
2019-08-15 11:26:55 +02:00
|
|
|
|
var nextLi = new LockItem(locks, key);
|
2017-10-17 10:34:07 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-15 16:00:04 +02:00
|
|
|
|
var continueImmediately = false;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
lock (locks)
|
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
if (!locks.TryGetValue(key, out var li))
|
2017-10-17 10:34:07 +02:00
|
|
|
|
{
|
|
|
|
|
locks.Add(key, nextLi);
|
|
|
|
|
continueImmediately = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
while (li.Next != null)
|
|
|
|
|
li = li.Next;
|
|
|
|
|
|
|
|
|
|
li.Next = nextLi;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (continueImmediately)
|
|
|
|
|
nextLi.Continue();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
nextLi.Error(e);
|
|
|
|
|
}
|
|
|
|
|
return nextLi.GetTask();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 11:26:55 +02:00
|
|
|
|
|
2017-10-17 10:34:07 +02:00
|
|
|
|
private class LockItem : IDisposable
|
|
|
|
|
{
|
2022-11-23 09:13:38 +01:00
|
|
|
|
internal volatile LockItem? Next;
|
2017-10-17 10:34:07 +02:00
|
|
|
|
|
|
|
|
|
private readonly Dictionary<T, LockItem> locks;
|
2022-11-17 16:47:07 +01:00
|
|
|
|
private readonly TaskCompletionSource<IDisposable> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
2017-10-17 10:34:07 +02:00
|
|
|
|
private readonly T key;
|
|
|
|
|
|
|
|
|
|
public LockItem(Dictionary<T, LockItem> locks, T key)
|
|
|
|
|
{
|
|
|
|
|
this.locks = locks;
|
|
|
|
|
this.key = key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Continue()
|
|
|
|
|
{
|
|
|
|
|
tcs.TrySetResult(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void Error(Exception e)
|
|
|
|
|
{
|
|
|
|
|
tcs.SetException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal Task<IDisposable> GetTask()
|
|
|
|
|
{
|
|
|
|
|
return tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
lock (locks)
|
|
|
|
|
{
|
2018-12-19 20:50:56 +01:00
|
|
|
|
if (!locks.TryGetValue(key, out var li))
|
2017-10-17 10:34:07 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (li != this)
|
|
|
|
|
{
|
2019-08-15 11:26:55 +02:00
|
|
|
|
// Something is wrong (comparer is not stable?), but we cannot lose the completions sources
|
2017-10-17 10:34:07 +02:00
|
|
|
|
while (li.Next != null)
|
|
|
|
|
li = li.Next;
|
|
|
|
|
li.Next = Next;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Next == null)
|
|
|
|
|
{
|
|
|
|
|
locks.Remove(key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locks[key] = Next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Next.Continue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|