Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 21 additions & 9 deletions src/fsharp/FSharp.Core/async.fs
Original file line number Diff line number Diff line change
Expand Up @@ -871,11 +871,9 @@ namespace Microsoft.FSharp.Control

[<DebuggerHidden>]
let RunSynchronously cancellationToken (computation: Async<'T>) timeout =
// Reuse the current ThreadPool thread if possible. Unfortunately
// Thread.IsThreadPoolThread isn't available on all profiles so
// we approximate it by testing synchronization context for null.
match SynchronizationContext.Current, timeout with
| null, None -> RunSynchronouslyInCurrentThread (cancellationToken, computation)
// Reuse the current ThreadPool thread if possible.
match Thread.CurrentThread.IsThreadPoolThread, timeout with
| true, None -> RunSynchronouslyInCurrentThread (cancellationToken, computation)
// When the timeout is given we need a dedicated thread
// which cancels the computation.
// Performing the cancellation in the ThreadPool eg. by using
Expand Down Expand Up @@ -941,7 +939,11 @@ namespace Microsoft.FSharp.Control
else
ctxt.cont completedTask.Result) |> unfake

task.ContinueWith(Action<Task<'T>>(continuation)) |> ignore |> fake
if task.IsCompleted then
continuation task |> fake
else
task.ContinueWith(Action<Task<'T>>(continuation), TaskContinuationOptions.ExecuteSynchronously)
|> ignore |> fake

[<DebuggerHidden>]
let taskContinueWithUnit (task: Task) (ctxt: AsyncActivation<unit>) useCcontForTaskCancellation =
Expand All @@ -960,7 +962,11 @@ namespace Microsoft.FSharp.Control
else
ctxt.cont ()) |> unfake

task.ContinueWith(Action<Task>(continuation)) |> ignore |> fake
if task.IsCompleted then
continuation task |> fake
else
task.ContinueWith(Action<Task>(continuation), TaskContinuationOptions.ExecuteSynchronously)
|> ignore |> fake

[<Sealed; AutoSerializable(false)>]
type AsyncIAsyncResult<'T>(callback: System.AsyncCallback, state:obj) =
Expand Down Expand Up @@ -1692,10 +1698,16 @@ namespace Microsoft.FSharp.Control
CreateWhenCancelledAsync compensation computation

static member AwaitTask (task:Task<'T>) : Async<'T> =
CreateDelimitedUserCodeAsync (fun ctxt -> taskContinueWith task ctxt false)
if task.IsCompleted then
CreateProtectedAsync (fun ctxt -> taskContinueWith task ctxt false)
else
CreateDelimitedUserCodeAsync (fun ctxt -> taskContinueWith task ctxt false)

static member AwaitTask (task:Task) : Async<unit> =
CreateDelimitedUserCodeAsync (fun ctxt -> taskContinueWithUnit task ctxt false)
if task.IsCompleted then
CreateProtectedAsync (fun ctxt -> taskContinueWithUnit task ctxt false)
else
CreateDelimitedUserCodeAsync (fun ctxt -> taskContinueWithUnit task ctxt false)

module CommonExtensions =

Expand Down
Loading