Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/ChibiRuby/RFiber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public sealed class RFiber : RObject
readonly MRubyContext context = new();
readonly MRubyState state;
readonly MultiConsumerValueTaskNotifier<MRubyValue> resumeSource = new();
readonly TaskCompletionSource<MRubyValue> terminationSource =
new(TaskCreationOptions.RunContinuationsAsynchronously);

internal RFiber(MRubyState state, RClass c) : base(MRubyVType.Fiber, c)
{
Expand All @@ -48,17 +50,17 @@ public ValueTask<MRubyValue> WaitForResumeAsync(CancellationToken cancellation =
return resumeSource.WaitAsync(cancellation);
}

public async ValueTask<MRubyValue> WaitForTerminateAsync(CancellationToken cancellation = default)
public ValueTask<MRubyValue> WaitForTerminateAsync(CancellationToken cancellation = default)
{
// Wait for fiber completion
MRubyValue result = default;
while (IsAlive)
var task = terminationSource.Task;
if (cancellation.CanBeCanceled)
{
var wait = WaitForResumeAsync(cancellation);
if (wait.IsCompleted) continue;
result = await wait;
cancellation.Register(() =>
{
terminationSource.TrySetCanceled(cancellation);
});
}
return result;
return new ValueTask<MRubyValue>(task);
}

public async IAsyncEnumerable<MRubyValue> AsAsyncEnumerable([EnumeratorCancellation] CancellationToken cancellation = default)
Expand Down Expand Up @@ -302,11 +304,19 @@ internal MRubyValue MoveNext(ReadOnlySpan<MRubyValue> args, bool transfer, bool
if (pending is not null) state.Raise(pending);
}

if (context.State == FiberState.Terminated)
{
terminationSource.TrySetResult(result);
}
resumeSource.SetResult(result);
return result;
}
catch (Exception ex)
{
if (context.State == FiberState.Terminated)
{
terminationSource.TrySetException(ex);
}
resumeSource.SetException(ex);
throw;
}
Expand Down
Loading