From f1f2c966a7ac1a8e10d82651f1358c46089e2ed5 Mon Sep 17 00:00:00 2001 From: Jimmy Byrd Date: Sat, 17 Dec 2022 15:02:29 -0500 Subject: [PATCH] Various fixes --- src/IcedTasks/CancellableTask.fs | 6 ++-- src/IcedTasks/ColdTask.fs | 4 +-- src/IcedTasks/ValueTasks.fs | 49 +++++++++++++++----------------- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/IcedTasks/CancellableTask.fs b/src/IcedTasks/CancellableTask.fs index abbfc74..a3949c5 100644 --- a/src/IcedTasks/CancellableTask.fs +++ b/src/IcedTasks/CancellableTask.fs @@ -792,9 +792,9 @@ module CancellableTasks = |> Async.AwaitTask } - /// Executes a computation in the thread pool. + /// Runs an asynchronous computation, starting on the current operating system thread. static member inline AsCancellableTask(computation: Async<'T>) : CancellableTask<_> = - fun ct -> Async.StartAsTask(computation, cancellationToken = ct) + fun ct -> Async.StartImmediateAsTask(computation, cancellationToken = ct) // High priority extensions type CancellableTaskBuilderBase with @@ -820,7 +820,7 @@ module CancellableTasks = /// This turns a ColdTask<'T> into a CancellationToken -> ^Awaiter. /// /// CancellationToken -> ^Awaiter - member inline _.Source([] task: ColdTask<'TResult1>) = + member inline _.Source([] task: unit -> Task<'TResult1>) = (fun (ct: CancellationToken) -> (task ()).GetAwaiter()) /// Allows the computation expression to turn other types into CancellationToken -> ^Awaiter diff --git a/src/IcedTasks/ColdTask.fs b/src/IcedTasks/ColdTask.fs index 747d75b..14632d7 100644 --- a/src/IcedTasks/ColdTask.fs +++ b/src/IcedTasks/ColdTask.fs @@ -678,9 +678,9 @@ module ColdTasks = |> Async.AwaitTask ) - /// Executes a computation in the thread pool. + /// Runs an asynchronous computation, starting on the current operating system thread. static member inline AsColdTask(computation: Async<'T>) : ColdTask<_> = - fun () -> Async.StartAsTask(computation) + fun () -> Async.StartImmediateAsTask(computation) type ColdTaskBuilderBase with diff --git a/src/IcedTasks/ValueTasks.fs b/src/IcedTasks/ValueTasks.fs index ce2f769..a28ddc0 100644 --- a/src/IcedTasks/ValueTasks.fs +++ b/src/IcedTasks/ValueTasks.fs @@ -19,26 +19,24 @@ module ValueTaskExtensions = type Microsoft.FSharp.Control.Async with - static member inline AwaitValueTask(v: ValueTask<_>) : Async<_> = async { + static member inline AwaitValueTask(v: ValueTask<_>) : Async<_> = // https://github.com/dotnet/runtime/issues/31503#issuecomment-554415966 if v.IsCompletedSuccessfully then - return v.Result + async.Return v.Result else - return! Async.AwaitTask(v.AsTask()) - } + Async.AwaitTask(v.AsTask()) - static member inline AwaitValueTask(v: ValueTask) : Async = async { + static member inline AwaitValueTask(v: ValueTask) : Async = // https://github.com/dotnet/runtime/issues/31503#issuecomment-554415966 if v.IsCompletedSuccessfully then - return () + async.Return() else - return! Async.AwaitTask(v.AsTask()) - } + Async.AwaitTask(v.AsTask()) - /// Executes a computation in the thread pool. + /// Runs an asynchronous computation, starting immediately on the current operating system thread. static member inline AsValueTask(computation: Async<'T>) : ValueTask<_> = - Async.StartAsTask(computation) + Async.StartImmediateAsTask(computation) |> ValueTask<'T> @@ -235,7 +233,6 @@ module ValueTasks = ) : ValueTaskCode<'TOverall, unit> = ResumableCode.For(sequence, body) -#if NETSTANDARD2_1 /// Creates an ValueTask that runs computation. The action compensation is executed /// after computation completes, whether computation exits normally or by an exception. If compensation raises an exception itself /// the original exception is discarded and the new exception becomes the overall result of the computation. @@ -326,7 +323,6 @@ module ValueTasks = ValueTask() ) ) -#endif type ValueTaskBuilder() = @@ -451,6 +447,7 @@ module ValueTasks = sm.Data.MethodBuilder.SetResult(sm.Data.Result) with exn -> sm.Data.MethodBuilder.SetException exn + //-- RESUMABLE CODE END )) (SetStateMachineMethodImpl<_>(fun sm state -> @@ -627,7 +624,7 @@ module ValueTasks = /// /// This is the identify function. /// - /// CancellationToken -> ^Awaiter + /// ^Awaiter [] member inline _.Source<'TResult1, 'TResult2, ^Awaiter, 'TOverall when ^Awaiter :> ICriticalNotifyCompletion @@ -638,11 +635,11 @@ module ValueTasks = getAwaiter - /// Allows the computation expression to turn other types into CancellationToken -> ^Awaiter + /// Allows the computation expression to turn other types into ^Awaiter /// - /// This turns a ^TaskLike into a CancellationToken -> ^Awaiter. + /// This turns a ^TaskLike into a ^Awaiter. /// - /// CancellationToken -> ^Awaiter + /// ^Awaiter [] member inline _.Source< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter) @@ -689,27 +686,27 @@ module ValueTasks = /// IEnumerable member inline _.Source(s: #seq<_>) : #seq<_> = s - /// Allows the computation expression to turn other types into CancellationToken -> ^Awaiter + /// Allows the computation expression to turn other types into ^Awaiter /// - /// This turns a Task<'T> into a CancellationToken -> ^Awaiter. + /// This turns a Task<'T> into a ^Awaiter. /// - /// CancellationToken -> ^Awaiter + /// ^Awaiter member inline _.Source(task: Task<'T>) = task.GetAwaiter() - /// Allows the computation expression to turn other types into CancellationToken -> ^Awaiter + /// Allows the computation expression to turn other types into ^Awaiter /// - /// This turns a Async<'T> into a CancellationToken -> ^Awaiter. + /// This turns a Async<'T> into a ^Awaiter. /// - /// CancellationToken -> ^Awaiter + /// ^Awaiter member inline this.Source(computation: Async<'TResult1>) = - this.Source(Async.StartAsTask(computation)) + this.Source(Async.StartImmediateAsTask(computation)) - /// Allows the computation expression to turn other types into CancellationToken -> ^Awaiter + /// Allows the computation expression to turn other types into ^Awaiter /// - /// This turns a Async<'T> into a CancellationToken -> ^Awaiter. + /// This turns a Async<'T> into a ^Awaiter. /// - /// CancellationToken -> ^Awaiter + /// ^Awaiter member inline this.Source(awaiter: TaskAwaiter<'TResult1>) = awaiter []