Skip to content

Commit

Permalink
Add ValueTask.factory members (#37507)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jun 6, 2020
1 parent 76a5fda commit 4b339d8
Show file tree
Hide file tree
Showing 34 changed files with 190 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken)) =>
cancellationToken.IsCancellationRequested ?
new ValueTask<int>(Task.FromCanceled<int>(cancellationToken)) :
ValueTask.FromCanceled<int>(cancellationToken) :
new ValueTask<int>(Read(buffer.Span));

public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected virtual ValueTask<DbTransaction> BeginDbTransactionAsync(IsolationLeve
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<DbTransaction>(Task.FromCanceled<DbTransaction>(cancellationToken));
return ValueTask.FromCanceled<DbTransaction>(cancellationToken);
}

try
Expand All @@ -76,7 +76,7 @@ protected virtual ValueTask<DbTransaction> BeginDbTransactionAsync(IsolationLeve
}
catch (Exception e)
{
return new ValueTask<DbTransaction>(Task.FromException<DbTransaction>(e));
return ValueTask.FromException<DbTransaction>(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}
return FinishReadAsyncMemory(buffer, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
EnsureNotDisposed();

return cancellationToken.IsCancellationRequested ?
new ValueTask(Task.FromCanceled<int>(cancellationToken)) :
ValueTask.FromCanceled(cancellationToken) :
WriteAsyncMemoryCore(buffer, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ internal ValueTask<int> ReadAsyncMemory(Memory<byte> buffer, CancellationToken c

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

EnsureBufferInitialized();
Expand Down Expand Up @@ -810,7 +810,7 @@ internal ValueTask WriteAsyncMemory(ReadOnlyMemory<byte> buffer, CancellationTok
EnsureNotDisposed();

return cancellationToken.IsCancellationRequested ?
new ValueTask(Task.FromCanceled<int>(cancellationToken)) :
ValueTask.FromCanceled(cancellationToken) :
WriteAsyncMemoryCore(buffer, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

CheckReadOperations();
Expand Down Expand Up @@ -292,7 +292,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);
}

CheckWriteOperations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Close() { /* nop */ }
public override int Read(Span<byte> buffer) => 0;

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) =>
cancellationToken.IsCancellationRequested ? new ValueTask<int>(Task.FromCanceled<int>(cancellationToken)) :
cancellationToken.IsCancellationRequested ? ValueTask.FromCanceled<int>(cancellationToken) :
new ValueTask<int>(0);

public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
if (cancellationToken.IsCancellationRequested)
{
// Cancellation requested.
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

if (_connection == null || buffer.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,12 +1299,12 @@ public override ValueTask<int> ReadAsync(Memory<byte> destination, CancellationT

if (http2Stream == null)
{
return new ValueTask<int>(Task.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2ReadStream)))));
return ValueTask.FromException<int>(ExceptionDispatchInfo.SetCurrentStackTrace(new ObjectDisposedException(nameof(Http2ReadStream))));
}

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

return http2Stream.ReadDataAsync(destination, _responseMessage, cancellationToken);
Expand Down Expand Up @@ -1366,7 +1366,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo

if (http2Stream == null)
{
return new ValueTask(Task.FromException(new ObjectDisposedException(nameof(Http2WriteStream))));
return ValueTask.FromException(new ObjectDisposedException(nameof(Http2WriteStream)));
}

return http2Stream.SendDataAsync(buffer, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
{
if (_stream == null)
{
return new ValueTask<int>(Task.FromException<int>(new ObjectDisposedException(nameof(Http3RequestStream))));
return ValueTask.FromException<int>(new ObjectDisposedException(nameof(Http3RequestStream)));
}

Debug.Assert(_response != null);
Expand Down Expand Up @@ -1275,7 +1275,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
{
if (_stream == null)
{
return new ValueTask(Task.FromException(new ObjectDisposedException(nameof(Http3WriteStream))));
return ValueTask.FromException(new ObjectDisposedException(nameof(Http3WriteStream)));
}

return _stream.WriteRequestContentAsync(buffer, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public byte[] Http2AltSvcOriginUri
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<HttpConnection?>(Task.FromCanceled<HttpConnection?>(cancellationToken));
return ValueTask.FromCanceled<HttpConnection?>(cancellationToken);
}

TimeSpan pooledConnectionLifetime = _poolManager.Settings._pooledConnectionLifetime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask(Task.FromCanceled(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);
}

HttpConnection? connection = _connection;
if (connection == null)
{
return new ValueTask(Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.ObjectDisposed_StreamClosed))));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.ObjectDisposed_StreamClosed)));
}

if (buffer.Length == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public ValueTask EnterAsync(CancellationToken cancellationToken)
// If the mutex is not currently held nor contended, enter immediately.
// Otherwise, fall back to a more expensive likely-asynchronous wait.
return
cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) :
cancellationToken.IsCancellationRequested ? ValueTask.FromCanceled(cancellationToken) :
Interlocked.Decrement(ref _gate) >= 0 ? default :
Contended(cancellationToken);

Expand Down Expand Up @@ -241,12 +241,6 @@ void Contended()
}
}

/// <summary>Creates a canceled ValueTask.</summary>
/// <remarks>Separated out to reduce asm for this rare path in the call site.</remarks>
[MethodImpl(MethodImplOptions.NoInlining)]
private static ValueTask FromCanceled(CancellationToken cancellationToken) =>
new ValueTask(Task.FromCanceled(cancellationToken));

/// <summary>Represents a waiter for the mutex.</summary>
/// <remarks>Implemented as a reusable backing source for a value task.</remarks>
private sealed class Waiter : IValueTaskSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override ValueTask DisposeAsync()
}
catch (Exception exc)
{
return new ValueTask(Task.FromException(exc));
return ValueTask.FromException(exc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ private ValueTask WriteSingleChunk<TIOAdapter>(TIOAdapter writeAdapter, ReadOnly
if (status.ErrorCode != SecurityStatusPalErrorCode.OK)
{
ArrayPool<byte>.Shared.Return(rentedBuffer);
return new ValueTask(Task.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.net_io_encrypt, SslStreamPal.GetException(status)))));
return ValueTask.FromException(ExceptionDispatchInfo.SetCurrentStackTrace(new IOException(SR.net_io_encrypt, SslStreamPal.GetException(status))));
}

ValueTask t = writeAdapter.WriteAsync(outBuffer, 0, encryptedBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal ValueTask<int> ReceiveAsync(Memory<byte> buffer, SocketFlags socketFlag
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

AwaitableSocketAsyncEventArgs saea = LazyInitializer.EnsureInitialized(ref EventArgs.ValueTaskReceive, () => new AwaitableSocketAsyncEventArgs());
Expand Down Expand Up @@ -344,7 +344,7 @@ internal ValueTask<int> SendAsync(ReadOnlyMemory<byte> buffer, SocketFlags socke
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

AwaitableSocketAsyncEventArgs saea = LazyInitializer.EnsureInitialized(ref EventArgs.ValueTaskSend, () => new AwaitableSocketAsyncEventArgs());
Expand All @@ -368,7 +368,7 @@ internal ValueTask SendAsyncForNetworkStream(ReadOnlyMemory<byte> buffer, Socket
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask(Task.FromCanceled(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);
}

AwaitableSocketAsyncEventArgs saea = LazyInitializer.EnsureInitialized(ref EventArgs.ValueTaskSend, () => new AwaitableSocketAsyncEventArgs());
Expand Down Expand Up @@ -906,7 +906,7 @@ public ValueTask<int> ReceiveAsync(Socket socket, CancellationToken cancellation

return error == SocketError.Success ?
new ValueTask<int>(bytesTransferred) :
new ValueTask<int>(Task.FromException<int>(CreateException(error)));
ValueTask.FromException<int>(CreateException(error));
}

/// <summary>Initiates a send operation on the associated socket.</summary>
Expand All @@ -928,7 +928,7 @@ public ValueTask<int> SendAsync(Socket socket, CancellationToken cancellationTok

return error == SocketError.Success ?
new ValueTask<int>(bytesTransferred) :
new ValueTask<int>(Task.FromException<int>(CreateException(error)));
ValueTask.FromException<int>(CreateException(error));
}

public ValueTask SendAsyncForNetworkStream(Socket socket, CancellationToken cancellationToken)
Expand All @@ -947,7 +947,7 @@ public ValueTask SendAsyncForNetworkStream(Socket socket, CancellationToken canc

return error == SocketError.Success ?
default :
new ValueTask(Task.FromException(CreateException(error)));
ValueTask.FromException(CreateException(error));
}

public ValueTask ConnectAsync(Socket socket)
Expand All @@ -973,7 +973,7 @@ public ValueTask ConnectAsync(Socket socket)

return error == SocketError.Success ?
default :
new ValueTask(Task.FromException(CreateException(error)));
ValueTask.FromException(CreateException(error));
}

/// <summary>Gets the status of the operation.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override ValueTask<ValueWebSocketReceiveResult> ReceiveAsync(Memory<byte>
}
catch (Exception exc)
{
return new ValueTask<ValueWebSocketReceiveResult>(Task.FromException<ValueWebSocketReceiveResult>(exc));
return ValueTask.FromException<ValueWebSocketReceiveResult>(exc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public virtual ValueTask DisposeAsync()
}
catch (Exception exc)
{
return new ValueTask(Task.FromException(exc));
return ValueTask.FromException(exc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

EnsureNotClosed();
Expand Down Expand Up @@ -1066,7 +1066,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
// Fast path check for cancellation already requested
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);
}

EnsureNotClosed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ private ValueTask WriteAsyncInternal(ReadOnlyMemory<byte> source, CancellationTo
Debug.Assert(_asyncState != null);

if (cancellationToken.IsCancellationRequested)
return new ValueTask(Task.FromCanceled(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);

if (_fileHandle.IsClosed)
throw Error.GetFileNotOpen();
Expand Down Expand Up @@ -705,7 +705,7 @@ private ValueTask WriteAsyncInternal(ReadOnlyMemory<byte> source, CancellationTo
}
catch (Exception exc)
{
return new ValueTask(Task.FromException(exc));
return ValueTask.FromException(exc);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

if (IsClosed)
Expand Down Expand Up @@ -505,7 +505,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo

if (cancellationToken.IsCancellationRequested)
{
return new ValueTask(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);
}

if (IsClosed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<int>(Task.FromCanceled<int>(cancellationToken));
return ValueTask.FromCanceled<int>(cancellationToken);
}

try
Expand Down Expand Up @@ -456,7 +456,7 @@ public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken
}
catch (Exception exception)
{
return new ValueTask<int>(Task.FromException<int>(exception));
return ValueTask.FromException<int>(exception);
}
}

Expand Down Expand Up @@ -797,7 +797,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
{
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask(Task.FromCanceled(cancellationToken));
return ValueTask.FromCanceled(cancellationToken);
}

try
Expand All @@ -820,7 +820,7 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
}
catch (Exception exception)
{
return new ValueTask(Task.FromException(exception));
return ValueTask.FromException(exception);
}
}

Expand Down
Loading

0 comments on commit 4b339d8

Please sign in to comment.