@@ -52,8 +52,6 @@ module Async2Implementation =
5252
5353 module BindContext =
5454 let bindCount = new ThreadLocal< int>()
55- // Used to prevent sync over async deadlocks.
56- let started = new AsyncLocal< bool>()
5755
5856 [<Literal>]
5957 let bindLimit = 100
@@ -429,10 +427,10 @@ module MediumPriority =
429427 bindCancellable (( fun ct -> Async.StartAsTask( expr, cancellationToken = ct) .GetAwaiter()), continuation)
430428
431429 member inline this.Bind ( task : Task , [<InlineIfLambda>] continuation ) =
432- bindAwaiter ( task.ConfigureAwait ( false ) . GetAwaiter(), continuation)
430+ bindAwaiter ( task.GetAwaiter(), continuation)
433431
434432 member inline this.Bind ( task : Task < _ >, [<InlineIfLambda>] continuation ) =
435- bindAwaiter ( task.ConfigureAwait ( false ) . GetAwaiter(), continuation)
433+ bindAwaiter ( task.GetAwaiter(), continuation)
436434
437435 member inline this.ReturnFrom ( task : Task ) = this.Bind( task, this.Return)
438436 member inline this.ReturnFrom ( task : Task < _ >) = this.Bind( task, this.Return)
@@ -474,46 +472,23 @@ module Async2 =
474472
475473 let CheckAndThrowToken = AsyncLocal< CancellationToken>()
476474
477- let inline start ct ( code : Async2 < _ >) =
478- let oldCt = CheckAndThrowToken.Value
479- let oldStarted = BindContext.started.Value
480-
481- let immediate =
482- not oldStarted
483- && isNull SynchronizationContext.Current
484- && TaskScheduler.Current = TaskScheduler.Default
485-
486- try
487- BindContext.started.Value <- true
475+ let startInThreadPool ct ( code : Async2 < 't >) =
476+ Task.Run< 't>( fun () ->
488477 CheckAndThrowToken.Value <- ct
478+ code.StartImmediate ct |> _. Task)
489479
490- if immediate then
491- code.StartImmediate ct |> _. Task
492- else
493- Task.Run< 't>( fun () -> code.StartImmediate ct |> _. Task)
494- finally
495- CheckAndThrowToken.Value <- oldCt
496- BindContext.started.Value <- oldStarted
497-
498- let run ct ( code : Async2 < 't >) =
499- start ct code |> _. GetAwaiter() .GetResult()
480+ let runInThreadPool ct ( code : Async2 < 't >) =
481+ startInThreadPool ct code |> _. GetAwaiter() .GetResult()
500482
501- let runWithoutCancellation code = run CancellationToken.None code
502-
503- //let queueTask ct code =
504- // Task.Run<'t>(fun () -> start ct code)
505-
506- let startAsTaskWithoutCancellation code = start CancellationToken.None code
483+ let runWithoutCancellation code = runInThreadPool CancellationToken.None code
507484
508485 let toAsync ( code : Async2 < 't >) =
509486 async {
510487 let! ct = Async.CancellationToken
511- let task = start ct code
488+ let task = startInThreadPool ct code
512489 return ! Async.AwaitTask task
513490 }
514491
515- let fromValue ( value : 't ) : Async2 < 't > = async2 { return value }
516-
517492 let CancellationToken =
518493 async2.Run(
519494 Async2Code( fun sm ->
@@ -530,15 +505,15 @@ type Async2 =
530505
531506 static member Start ( computation : Async2 < _ >, ? cancellationToken : CancellationToken ) : unit =
532507 let ct = defaultArg cancellationToken CancellationToken.None
533- Async2.start ct computation |> ignore
508+ Async2.startInThreadPool ct computation |> ignore
534509
535510 static member StartAsTask ( computation : Async2 < _ >, ? cancellationToken : CancellationToken ) : Task < _ > =
536511 let ct = defaultArg cancellationToken CancellationToken.None
537- Async2.start ct computation
512+ Async2.startInThreadPool ct computation
538513
539- static member RunImmediate ( computation : Async2 < 'T >, ? cancellationToken : CancellationToken ) : 'T =
514+ static member RunSynchronously ( computation : Async2 < 'T >, ? cancellationToken : CancellationToken ) : 'T =
540515 let ct = defaultArg cancellationToken CancellationToken.None
541- Async2.run ct computation
516+ Async2.runInThreadPool ct computation
542517
543518 static member Parallel ( computations : Async2 < _ > seq ) =
544519 async2 {
@@ -555,7 +530,7 @@ type Async2 =
555530 lcts.Cancel()
556531 return raise exn
557532 }
558- |> Async2.start lcts.Token
533+ |> Async2.startInThreadPool lcts.Token
559534 }
560535
561536 return ! Task.WhenAll tasks
@@ -584,25 +559,25 @@ type Async2 =
584559 static member TryCancelled ( computation : Async2 < 'T >, compensation ) =
585560 async2 {
586561 let! ct = Async2.CancellationToken
587- let task = Async2.start ct computation
562+ let invocation = computation.StartImmediate ct
588563
589564 try
590- return ! task
565+ return ! invocation
591566 finally
592- if task .IsCanceled then
567+ if invocation.Task .IsCanceled then
593568 compensation ()
594569 }
595570
596571 static member StartChild ( computation : Async2 < 'T >) : Async2 < Async2 < 'T >> =
597572 async2 {
598573 let! ct = Async2.CancellationToken
599- return async2 { return ! computation |> Async2.start ct }
574+ return async2 { return ! computation |> Async2.startInThreadPool ct }
600575 }
601576
602577 static member StartChildAsTask ( computation : Async2 < 'T >) : Async2 < Task < 'T >> =
603578 async2 {
604579 let! ct = Async2.CancellationToken
605- let task = computation |> Async2.start ct
580+ let task = computation |> Async2.startInThreadPool ct
606581 return task
607582 }
608583
0 commit comments