Skip to content

Commit 7cdfbae

Browse files
committed
Use Wait without wait handle
1 parent a74073c commit 7cdfbae

File tree

4 files changed

+26
-42
lines changed

4 files changed

+26
-42
lines changed

src/DotNext/IO/AsyncWriterStream.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace DotNext.IO;
22

3+
using static Threading.Tasks.Synchronization;
4+
35
internal sealed class AsyncWriterStream<TOutput>(TOutput output) : WriterStream<TOutput>(output)
46
where TOutput : ISupplier<ReadOnlyMemory<byte>, CancellationToken, ValueTask>, IFlushable
57
{
@@ -25,24 +27,23 @@ public override void Write(ReadOnlySpan<byte> buffer)
2527
{
2628
if (!buffer.IsEmpty)
2729
{
28-
using var rental = buffer.Copy();
29-
30+
var rental = buffer.Copy();
3031
timeoutSource ??= new();
3132
timeoutSource.CancelAfter(timeout);
32-
var task = WriteAsync(rental.Memory, timeoutSource.Token).AsTask();
33+
var task = WriteAsync(rental.Memory, timeoutSource.Token);
3334
try
3435
{
3536
task.Wait();
3637
}
3738
finally
3839
{
39-
task.Dispose();
40-
4140
if (!timeoutSource.TryReset())
4241
{
4342
timeoutSource.Dispose();
4443
timeoutSource = null;
4544
}
45+
46+
rental.Dispose();
4647
}
4748
}
4849
}

src/DotNext/IO/ReadOnlyStream.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace DotNext.IO;
44

5+
using Buffers;
6+
using static Threading.Tasks.Synchronization;
7+
58
internal abstract class ReadOnlyStream : Stream
69
{
710
public sealed override bool CanRead => true;
@@ -84,7 +87,6 @@ internal sealed class ReadOnlyStream<TArg>(Func<Memory<byte>, TArg, Cancellation
8487
{
8588
private const int DefaultTimeout = 4000;
8689
private int timeout = DefaultTimeout;
87-
private byte[]? synchronousBuffer;
8890
private CancellationTokenSource? timeoutSource;
8991

9092
public override int ReadTimeout
@@ -121,40 +123,30 @@ public override int Read(Span<byte> buffer)
121123
}
122124
else
123125
{
124-
var tempBuffer = RentBuffer(buffer.Length);
126+
var tempBuffer = Memory.AllocateExactly<byte>(buffer.Length);
125127
timeoutSource ??= new();
126128
timeoutSource.CancelAfter(timeout);
127-
var task = ReadAsync(tempBuffer, timeoutSource.Token).AsTask();
129+
var task = ReadAsync(tempBuffer.Memory, timeoutSource.Token);
128130
try
129131
{
130-
task.Wait();
131-
writtenCount = task.Result;
132+
writtenCount = task.Wait();
133+
tempBuffer.Span.Slice(0, writtenCount).CopyTo(buffer);
132134
}
133135
finally
134136
{
135-
task.Dispose();
136-
137137
if (!timeoutSource.TryReset())
138138
{
139139
timeoutSource.Dispose();
140140
timeoutSource = null;
141141
}
142+
143+
tempBuffer.Dispose();
142144
}
143-
144-
tempBuffer.AsSpan(0, writtenCount).CopyTo(buffer);
145145
}
146146

147147
return writtenCount;
148148
}
149149

150-
private ArraySegment<byte> RentBuffer(int length)
151-
{
152-
if (synchronousBuffer is null || synchronousBuffer.Length < length)
153-
synchronousBuffer = GC.AllocateUninitializedArray<byte>(length);
154-
155-
return new(synchronousBuffer, 0, length);
156-
}
157-
158150
protected override void Dispose(bool disposing)
159151
{
160152
if (disposing)

src/DotNext/Runtime/CompilerServices/Scope.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DotNext.Runtime.CompilerServices;
66

7+
using static Threading.Tasks.Synchronization;
78
using ExceptionAggregator = ExceptionServices.ExceptionAggregator;
89

910
/// <summary>
@@ -114,8 +115,6 @@ public void Dispose()
114115

115116
static void ExecuteCallbacks(ReadOnlySpan<object?> callbacks, ref ExceptionAggregator aggregator)
116117
{
117-
Task t;
118-
119118
foreach (var cb in callbacks)
120119
{
121120
try
@@ -128,22 +127,14 @@ static void ExecuteCallbacks(ReadOnlySpan<object?> callbacks, ref ExceptionAggre
128127
callback();
129128
break;
130129
case Func<ValueTask> callback:
131-
using (t = callback().AsTask())
132-
{
133-
t.Wait();
134-
}
135-
130+
callback().Wait();
136131
break;
137132
case IDisposable disposable:
138133
// IDisposable in synchronous implementation has higher priority than IAsyncDisposable
139134
disposable.Dispose();
140135
break;
141136
case IAsyncDisposable disposable:
142-
using (t = disposable.DisposeAsync().AsTask())
143-
{
144-
t.Wait();
145-
}
146-
137+
disposable.DisposeAsync().Wait();
147138
break;
148139
}
149140
}

src/DotNext/Threading/Tasks/Synchronization.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static Result<TResult> GetResult<TResult>(this Task<TResult> task, TimeSp
2222
{
2323
result = task.Wait(timeout) ? new(task.Result) : new(new TimeoutException());
2424
}
25-
catch (AggregateException e) when (e.InnerExceptions.Count == 1)
25+
catch (AggregateException e) when (e.InnerExceptions is [var innerEx])
2626
{
27-
result = new(e.InnerExceptions[0]);
27+
result = new(innerEx);
2828
}
2929
catch (Exception e)
3030
{
@@ -49,9 +49,9 @@ public static Result<TResult> GetResult<TResult>(this Task<TResult> task, Cancel
4949
task.Wait(token);
5050
result = task.Result;
5151
}
52-
catch (AggregateException e) when (e.InnerExceptions.Count == 1)
52+
catch (AggregateException e) when (e.InnerExceptions is [var innerEx])
5353
{
54-
result = new(e.InnerExceptions[0]);
54+
result = new(innerEx);
5555
}
5656
catch (Exception e)
5757
{
@@ -87,9 +87,9 @@ public static Result<TResult> GetResult<TResult>(this Task<TResult> task, Cancel
8787
var awaiter = new DynamicTaskAwaitable.Awaiter(task, ConfigureAwaitOptions.None);
8888
result = new(awaiter.GetRawResult());
8989
}
90-
catch (AggregateException e) when (e.InnerExceptions.Count is 1)
90+
catch (AggregateException e) when (e.InnerExceptions is [var innerEx])
9191
{
92-
result = new(e.InnerExceptions[0]);
92+
result = new(innerEx);
9393
}
9494
catch (Exception e)
9595
{
@@ -119,9 +119,9 @@ public static Result<TResult> GetResult<TResult>(this Task<TResult> task, Cancel
119119
var awaiter = new DynamicTaskAwaitable.Awaiter(task, ConfigureAwaitOptions.None);
120120
result = new(awaiter.GetRawResult());
121121
}
122-
catch (AggregateException e) when (e.InnerExceptions.Count is 1)
122+
catch (AggregateException e) when (e.InnerExceptions is [var innerEx])
123123
{
124-
result = new(e.InnerExceptions[0]);
124+
result = new(innerEx);
125125
}
126126
catch (Exception e)
127127
{

0 commit comments

Comments
 (0)