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