2017-10-17 08:34:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Flow.FlowHelpers
|
|
|
|
|
{
|
2019-08-15 09:26:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Implementation of an asynchronous locking mechanism.
|
|
|
|
|
/// </summary>
|
2022-11-23 08:13:38 +00:00
|
|
|
|
public class LockCollection<T> where T : notnull
|
2017-10-17 08:34:07 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<T, LockItem> locks;
|
|
|
|
|
|
2021-05-29 19:51:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
2017-10-17 08:34:07 +00:00
|
|
|
|
public LockCollection(IEqualityComparer<T> comparer)
|
|
|
|
|
{
|
|
|
|
|
locks = new Dictionary<T, LockItem>(comparer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 09:26:55 +00: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 08:34:07 +00:00
|
|
|
|
public Task<IDisposable> GetLock(T key)
|
|
|
|
|
{
|
2018-12-19 19:50:56 +00:00
|
|
|
|
// ReSharper disable once InconsistentlySynchronizedField - by design
|
2019-08-15 09:26:55 +00:00
|
|
|
|
var nextLi = new LockItem(locks, key);
|
2017-10-17 08:34:07 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-15 14:00:04 +00:00
|
|
|
|
var continueImmediately = false;
|
2017-10-17 08:34:07 +00:00
|
|
|
|
lock (locks)
|
|
|
|
|
{
|
2018-12-19 19:50:56 +00:00
|
|
|
|
if (!locks.TryGetValue(key, out var li))
|
2017-10-17 08:34:07 +00: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 09:26:55 +00:00
|
|
|
|
|
2017-10-17 08:34:07 +00:00
|
|
|
|
private class LockItem : IDisposable
|
|
|
|
|
{
|
2022-11-23 08:13:38 +00:00
|
|
|
|
internal volatile LockItem? Next;
|
2017-10-17 08:34:07 +00:00
|
|
|
|
|
|
|
|
|
private readonly Dictionary<T, LockItem> locks;
|
2022-11-17 15:47:07 +00:00
|
|
|
|
private readonly TaskCompletionSource<IDisposable> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
2017-10-17 08:34:07 +00: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 19:50:56 +00:00
|
|
|
|
if (!locks.TryGetValue(key, out var li))
|
2017-10-17 08:34:07 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (li != this)
|
|
|
|
|
{
|
2019-08-15 09:26:55 +00:00
|
|
|
|
// Something is wrong (comparer is not stable?), but we cannot lose the completions sources
|
2017-10-17 08:34:07 +00:00
|
|
|
|
while (li.Next != null)
|
|
|
|
|
li = li.Next;
|
|
|
|
|
li.Next = Next;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Next == null)
|
|
|
|
|
{
|
|
|
|
|
locks.Remove(key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locks[key] = Next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Next.Continue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|