This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Await writes so natural back pressure is applied (#1195)
- Loading branch information
Showing
3 changed files
with
91 additions
and
43 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/System.IO.Pipelines.Networking.Libuv/Internal/LibuvAwaitable.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,72 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using System.IO.Pipelines.Networking.Libuv.Interop; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
|
||
namespace System.IO.Pipelines.Networking.Libuv | ||
{ | ||
public class LibuvAwaitable<TRequest> : ICriticalNotifyCompletion where TRequest : UvRequest | ||
{ | ||
private readonly static Action CALLBACK_RAN = () => { }; | ||
|
||
private Action _callback; | ||
|
||
private Exception _exception; | ||
|
||
private int _status; | ||
|
||
public static Action<TRequest, int, object> Callback = (req, status, state) => | ||
{ | ||
var awaitable = (LibuvAwaitable<TRequest>)state; | ||
|
||
Exception exception; | ||
req.Libuv.Check(status, out exception); | ||
awaitable._exception = exception; | ||
awaitable._status = status; | ||
|
||
var continuation = Interlocked.Exchange(ref awaitable._callback, CALLBACK_RAN); | ||
|
||
continuation?.Invoke(); | ||
}; | ||
|
||
public LibuvAwaitable<TRequest> GetAwaiter() => this; | ||
public bool IsCompleted => _callback == CALLBACK_RAN; | ||
|
||
public int GetResult() | ||
{ | ||
var exception = _exception; | ||
var status = _status; | ||
|
||
// Reset the awaitable state | ||
_exception = null; | ||
_status = 0; | ||
_callback = null; | ||
|
||
if (exception != null) | ||
{ | ||
throw exception; | ||
} | ||
|
||
return status; | ||
} | ||
|
||
public void OnCompleted(Action continuation) | ||
{ | ||
if (_callback == CALLBACK_RAN || | ||
Interlocked.CompareExchange(ref _callback, continuation, null) == CALLBACK_RAN) | ||
{ | ||
Task.Run(continuation); | ||
} | ||
} | ||
|
||
public void UnsafeOnCompleted(Action continuation) | ||
{ | ||
OnCompleted(continuation); | ||
} | ||
} | ||
} |
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