-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial checkin of async CompletePending - no tests yet.
- Loading branch information
Showing
9 changed files
with
177 additions
and
57 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
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
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,71 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Collections.Concurrent; | ||
|
||
namespace FASTER.core | ||
{ | ||
/// <summary> | ||
/// Async queue | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class AsyncQueue<T> | ||
{ | ||
private readonly SemaphoreSlim semaphore; | ||
private readonly ConcurrentQueue<T> queue; | ||
|
||
/// <summary> | ||
/// Queue count | ||
/// </summary> | ||
public int Count => queue.Count; | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
public AsyncQueue() | ||
{ | ||
semaphore = new SemaphoreSlim(0); | ||
queue = new ConcurrentQueue<T>(); | ||
} | ||
|
||
/// <summary> | ||
/// Enqueue item | ||
/// </summary> | ||
/// <param name="item"></param> | ||
public void Enqueue(T item) | ||
{ | ||
queue.Enqueue(item); | ||
semaphore.Release(); | ||
} | ||
|
||
/// <summary> | ||
/// Async dequeue | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task<T> DequeueAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
for (; ; ) | ||
{ | ||
await semaphore.WaitAsync(cancellationToken); | ||
|
||
if (queue.TryDequeue(out T item)) | ||
{ | ||
return item; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Try dequeue (if item exists) | ||
/// </summary> | ||
/// <param name="item"></param> | ||
/// <returns></returns> | ||
public bool TryDequeue(out T item) | ||
{ | ||
return queue.TryDequeue(out item); | ||
} | ||
} | ||
} |
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