feat: 添加信号量池

This commit is contained in:
SikongJueluo 2025-07-08 21:26:45 +08:00
parent 1af3fa3a8f
commit 67bdec8570
No known key found for this signature in database
1 changed files with 24 additions and 11 deletions

View File

@ -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>