-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements semaphore queue to handle events sequentually
- Loading branch information
1 parent
c4609d4
commit 0482892
Showing
6 changed files
with
57 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/OpenFTTH.GDBIntegrator.Integrator/Queue/SemaphoreQueue.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenFTTH.GDBIntegrator.Integrator.Queue | ||
{ | ||
public class SemaphoreQueue | ||
{ | ||
private SemaphoreSlim semaphore; | ||
private ConcurrentQueue<TaskCompletionSource<bool>> queue = | ||
new ConcurrentQueue<TaskCompletionSource<bool>>(); | ||
public SemaphoreQueue(int initialCount) | ||
{ | ||
semaphore = new SemaphoreSlim(initialCount); | ||
} | ||
public SemaphoreQueue(int initialCount, int maxCount) | ||
{ | ||
semaphore = new SemaphoreSlim(initialCount, maxCount); | ||
} | ||
public void Wait() | ||
{ | ||
WaitAsync().Wait(); | ||
} | ||
public Task WaitAsync() | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
queue.Enqueue(tcs); | ||
semaphore.WaitAsync().ContinueWith(t => | ||
{ | ||
TaskCompletionSource<bool> popped; | ||
if (queue.TryDequeue(out popped)) | ||
popped.SetResult(true); | ||
}); | ||
return tcs.Task; | ||
} | ||
public void Release() | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters