feat: 添加信号量池
This commit is contained in:
parent
1af3fa3a8f
commit
67bdec8570
|
@ -1,4 +1,5 @@
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using DotNext;
|
||||||
|
|
||||||
namespace Common;
|
namespace Common;
|
||||||
|
|
||||||
|
@ -65,26 +66,38 @@ public class SemaphorePool
|
||||||
/// [TODO:description]
|
/// [TODO:description]
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>[TODO:return]</returns>
|
/// <returns>[TODO:return]</returns>
|
||||||
public void Wait()
|
public Result<int> Wait()
|
||||||
{
|
{
|
||||||
WaitAsync().Wait();
|
semaphore.Wait();
|
||||||
|
|
||||||
|
int pop;
|
||||||
|
if (queue.TryDequeue(out pop))
|
||||||
|
{
|
||||||
|
return pop;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new(new Exception($"TODO"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// [TODO:description]
|
/// [TODO:description]
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>[TODO:return]</returns>
|
/// <returns>[TODO:return]</returns>
|
||||||
public Task WaitAsync()
|
public async ValueTask<Result<int>> WaitAsync()
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<bool>();
|
await semaphore.WaitAsync();
|
||||||
queue.Enqueue(tcs);
|
|
||||||
semaphore.WaitAsync().ContinueWith(t =>
|
int pop;
|
||||||
|
if (queue.TryDequeue(out pop))
|
||||||
{
|
{
|
||||||
TaskCompletionSource<bool> popped;
|
return pop;
|
||||||
if (queue.TryDequeue(out popped))
|
}
|
||||||
popped.SetResult(true);
|
else
|
||||||
});
|
{
|
||||||
return tcs.Task;
|
return new(new Exception($"TODO"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in New Issue