Skip to content

Commit 140b118

Browse files
committed
just start explicitely in thread pool
1 parent 04c7443 commit 140b118

File tree

10 files changed

+47
-77
lines changed

10 files changed

+47
-77
lines changed

src/Compiler/Driver/CompilerImports.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,7 @@ and [<Sealed>] TcImports
23762376
ReportWarnings warns
23772377

23782378
tcImports.RegisterAndImportReferencedAssemblies(ctok, res)
2379-
|> Async2.RunImmediate
2379+
|> Async2.RunSynchronously
23802380
|> ignore
23812381

23822382
true
@@ -2684,7 +2684,7 @@ let RequireReferences (ctok, tcImports: TcImports, tcEnv, thisAssemblyName, reso
26842684

26852685
let ccuinfos =
26862686
tcImports.RegisterAndImportReferencedAssemblies(ctok, resolutions)
2687-
|> Async2.RunImmediate
2687+
|> Async2.RunSynchronously
26882688

26892689
let asms =
26902690
ccuinfos

src/Compiler/Driver/GraphChecking/GraphProcessing.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ let processGraph<'Item, 'Result when 'Item: equality and 'Item: comparison>
139139
(parentCt: CancellationToken)
140140
: ('Item * 'Result)[] =
141141
let work node info = async2 { return work node info }
142-
Async2.RunImmediate(processGraphAsync graph (fun lookup info -> async2 { return! work lookup info }), parentCt)
142+
Async2.RunSynchronously(processGraphAsync graph (fun lookup info -> async2 { return! work lookup info }), parentCt)

src/Compiler/Driver/fsc.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ let main1
605605
// Import basic assemblies
606606
let tcGlobals, frameworkTcImports =
607607
TcImports.BuildFrameworkTcImports(foundationalTcConfigP, sysRes, otherRes)
608-
|> Async2.RunImmediate
608+
|> Async2.RunSynchronously
609609

610610
let ilSourceDocs =
611611
[
@@ -653,7 +653,7 @@ let main1
653653

654654
let tcImports =
655655
TcImports.BuildNonFrameworkTcImports(tcConfigP, frameworkTcImports, otherRes, knownUnresolved, dependencyProvider)
656-
|> Async2.RunImmediate
656+
|> Async2.RunSynchronously
657657

658658
// register tcImports to be disposed in future
659659
disposables.Register tcImports

src/Compiler/Facilities/BuildGraph.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type internal GraphNode<'T> private (computation: Async2<'T>, cachedResult: Valu
5656
Thread.CurrentThread.CurrentUICulture <- GraphNode.culture
5757
let! result = computation
5858
cachedResult <- ValueSome result
59-
cachedResultNode <- Async2.fromValue result
59+
cachedResultNode <- async2 { return result }
6060
computation <- Unchecked.defaultof<_>
6161
return result
6262
finally
@@ -76,7 +76,7 @@ type internal GraphNode<'T> private (computation: Async2<'T>, cachedResult: Valu
7676
member _.IsComputing = requestCount > 0
7777

7878
static member FromResult(result: 'T) =
79-
let nodeResult = Async2.fromValue result
79+
let nodeResult = async2 {return result }
8080
GraphNode(nodeResult, ValueSome result, nodeResult)
8181

8282
new(computation) = GraphNode(computation, ValueNone, Unchecked.defaultof<_>)

src/Compiler/Facilities/DiagnosticsLogger.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,12 +952,11 @@ type StackGuard(maxDepth: int, name: string) =
952952

953953
StackGuardMetrics.countJump memberName $"{fileName}:{line}"
954954

955-
async {
956-
do! Async.SwitchToNewThread()
957-
Thread.CurrentThread.Name <- $"F# Extra Compilation Thread for {name} (depth {depth})"
955+
async2 {
956+
ignore name
958957
return f ()
959958
}
960-
|> Async.RunImmediate
959+
|> Async2.RunSynchronously
961960
else
962961
f ()
963962
finally

src/Compiler/Interactive/fsi.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4678,7 +4678,7 @@ type FsiEvaluationSession
46784678
unresolvedReferences,
46794679
fsiOptions.DependencyProvider
46804680
)
4681-
|> Async2.RunImmediate
4681+
|> Async2.RunSynchronously
46824682
with e ->
46834683
stopProcessingRecovery e range0
46844684
failwithf "Error creating evaluation session: %A" e

src/Compiler/Utilities/Async2.fs

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/FSharp.Compiler.ComponentTests/CompilerService/AsyncMemoize.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let ``Basics``() =
8787
memoize.Get(wrapKey 2, computation 2)
8888
}
8989
|> Async2.Parallel
90-
|> Async2.RunImmediate
90+
|> Async2.RunSynchronously
9191

9292
let expected = [| 10; 10; 4; 10; 6; 4|]
9393

tests/FSharp.Compiler.Service.Tests/BuildGraphTests.fs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module BuildGraphTests =
7474

7575
let work = Async2.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() ))
7676

77-
Async2.RunImmediate(work)
77+
Async2.RunSynchronously(work)
7878
|> ignore
7979

8080
Assert.shouldBe 1 computationCount
@@ -87,7 +87,7 @@ module BuildGraphTests =
8787

8888
let work = Async2.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() ))
8989

90-
let result = Async2.RunImmediate(work)
90+
let result = Async2.RunSynchronously(work)
9191

9292
Assert.shouldNotBeEmpty result
9393
Assert.shouldBe requests result.Length
@@ -102,7 +102,7 @@ module BuildGraphTests =
102102

103103
Assert.shouldBeTrue weak.IsAlive
104104

105-
Async2.RunImmediate(graphNode.GetOrComputeValue())
105+
Async2.RunSynchronously(graphNode.GetOrComputeValue())
106106
|> ignore
107107

108108
GC.Collect(2, GCCollectionMode.Forced, true)
@@ -119,7 +119,7 @@ module BuildGraphTests =
119119

120120
Assert.shouldBeTrue weak.IsAlive
121121

122-
Async2.RunImmediate(Async2.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() )))
122+
Async2.RunSynchronously(Async2.Parallel(Array.init requests (fun _ -> graphNode.GetOrComputeValue() )))
123123
|> ignore
124124

125125
GC.Collect(2, GCCollectionMode.Forced, true)
@@ -143,7 +143,7 @@ module BuildGraphTests =
143143

144144
let ex =
145145
try
146-
Async2.RunImmediate(work, cancellationToken = cts.Token)
146+
Async2.RunSynchronously(work, cancellationToken = cts.Token)
147147
|> ignore
148148
failwith "Should have canceled"
149149
with
@@ -173,7 +173,7 @@ module BuildGraphTests =
173173

174174
let ex =
175175
try
176-
Async2.RunImmediate(graphNode.GetOrComputeValue(), cancellationToken = cts.Token)
176+
Async2.RunSynchronously(graphNode.GetOrComputeValue(), cancellationToken = cts.Token)
177177
|> ignore
178178
failwith "Should have canceled"
179179
with
@@ -218,7 +218,7 @@ module BuildGraphTests =
218218

219219
cts.Cancel()
220220
resetEvent.Set() |> ignore
221-
Async2.RunImmediate(work)
221+
Async2.RunSynchronously(work)
222222
|> ignore
223223

224224
Assert.shouldBeTrue cts.IsCancellationRequested
@@ -287,7 +287,7 @@ module BuildGraphTests =
287287
Seq.init 100 pickRandomPhase
288288
|> Seq.map work
289289
|> Async2.Parallel
290-
|> Async2.RunImmediate
290+
|> Async2.RunSynchronously
291291

292292
exception TestException
293293

@@ -365,12 +365,12 @@ module BuildGraphTests =
365365

366366
let logger = DiagnosticsLoggerWithCallback errorCommitted
367367
use _ = UseDiagnosticsLogger logger
368-
tasks |> Seq.take 50 |> MultipleDiagnosticsLoggers.Parallel |> Async2.Ignore |> Async2.RunImmediate
368+
tasks |> Seq.take 50 |> MultipleDiagnosticsLoggers.Parallel |> Async2.Ignore |> Async2.RunSynchronously
369369

370370
// all errors committed
371371
errorCountShouldBe 300
372372

373-
tasks |> Seq.skip 50 |> MultipleDiagnosticsLoggers.Sequential |> Async2.Ignore |> Async2.RunImmediate
373+
tasks |> Seq.skip 50 |> MultipleDiagnosticsLoggers.Sequential |> Async2.Ignore |> Async2.RunSynchronously
374374

375375
errorCountShouldBe 600
376376

@@ -492,7 +492,7 @@ module BuildGraphTests =
492492

493493
Task.WaitAll(noErrorsTask, btask, task)
494494

495-
Seq.init 11 (fun _ -> async2 { errorR TestException; loggerShouldBe logger } ) |> Async2.Parallel |> Async2.RunImmediate |> ignore
495+
Seq.init 11 (fun _ -> async2 { errorR TestException; loggerShouldBe logger } ) |> Async2.Parallel |> Async2.RunSynchronously |> ignore
496496

497497
loggerShouldBe logger
498498
errorCountShouldBe 17
@@ -506,7 +506,7 @@ module BuildGraphTests =
506506

507507
loggerShouldBe logger
508508
}
509-
|> Async2.RunImmediate
509+
|> Async2.RunSynchronously
510510

511511
SetThreadDiagnosticsLoggerNoUnwind logger
512512
// This runs in async continuation, so the context is forked.
@@ -516,7 +516,7 @@ module BuildGraphTests =
516516
do! Async.SwitchToNewThread()
517517
loggerShouldBe DiscardErrorsLogger
518518
}
519-
|> Async2.RunImmediate
519+
|> Async2.RunSynchronously
520520
loggerShouldBe logger
521521

522522

tests/FSharp.Compiler.Service.Tests/FSharp.Compiler.Service.SurfaceArea.netstandard20.bsl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12397,7 +12397,7 @@ Internal.Utilities.Library.Async2: Internal.Utilities.Library.Async2`1[T] TryCan
1239712397
Internal.Utilities.Library.Async2: Internal.Utilities.Library.Async2`1[a[]] Parallel[a](System.Collections.Generic.IEnumerable`1[Internal.Utilities.Library.Async2`1[a]])
1239812398
Internal.Utilities.Library.Async2: Internal.Utilities.Library.Async2`1[a[]] Sequential[a](System.Collections.Generic.IEnumerable`1[Internal.Utilities.Library.Async2`1[a]])
1239912399
Internal.Utilities.Library.Async2: System.Threading.Tasks.Task`1[a] StartAsTask[a](Internal.Utilities.Library.Async2`1[a], Microsoft.FSharp.Core.FSharpOption`1[System.Threading.CancellationToken])
12400-
Internal.Utilities.Library.Async2: T RunImmediate[T](Internal.Utilities.Library.Async2`1[T], Microsoft.FSharp.Core.FSharpOption`1[System.Threading.CancellationToken])
12400+
Internal.Utilities.Library.Async2: T RunSynchronously[T](Internal.Utilities.Library.Async2`1[T], Microsoft.FSharp.Core.FSharpOption`1[System.Threading.CancellationToken])
1240112401
Internal.Utilities.Library.Async2: Void Start[a](Internal.Utilities.Library.Async2`1[a], Microsoft.FSharp.Core.FSharpOption`1[System.Threading.CancellationToken])
1240212402
Internal.Utilities.Library.Async2AutoOpens: Async2Builder async2
1240312403
Internal.Utilities.Library.Async2AutoOpens: Async2Builder get_async2()
@@ -12455,8 +12455,6 @@ Internal.Utilities.Library.Async2Implementation+Awaiter: b getResult$W[a,b](Micr
1245512455
Internal.Utilities.Library.Async2Implementation+Awaiter: b getResult[a,b](a)
1245612456
Internal.Utilities.Library.Async2Implementation+BindContext: Boolean IncrementBindCount()
1245712457
Internal.Utilities.Library.Async2Implementation+BindContext: Int32 bindLimit
12458-
Internal.Utilities.Library.Async2Implementation+BindContext: System.Threading.AsyncLocal`1[System.Boolean] get_started()
12459-
Internal.Utilities.Library.Async2Implementation+BindContext: System.Threading.AsyncLocal`1[System.Boolean] started
1246012458
Internal.Utilities.Library.Async2Implementation+BindContext: System.Threading.ThreadLocal`1[System.Int32] bindCount
1246112459
Internal.Utilities.Library.Async2Implementation+BindContext: System.Threading.ThreadLocal`1[System.Int32] get_bindCount()
1246212460
Internal.Utilities.Library.Async2Implementation+BindContext: Void Reset()
@@ -12531,14 +12529,12 @@ Internal.Utilities.Library.Async2Implementation: Internal.Utilities.Library.Asyn
1253112529
Internal.Utilities.Library.Async2Implementation: Internal.Utilities.Library.Async2Implementation+Trampoline
1253212530
Internal.Utilities.Library.Async2Module: Internal.Utilities.Library.Async2`1[System.Threading.CancellationToken] CancellationToken
1253312531
Internal.Utilities.Library.Async2Module: Internal.Utilities.Library.Async2`1[System.Threading.CancellationToken] get_CancellationToken()
12534-
Internal.Utilities.Library.Async2Module: Internal.Utilities.Library.Async2`1[t] fromValue[t](t)
1253512532
Internal.Utilities.Library.Async2Module: Microsoft.FSharp.Control.FSharpAsync`1[t] toAsync[t](Internal.Utilities.Library.Async2`1[t])
1253612533
Internal.Utilities.Library.Async2Module: System.Threading.AsyncLocal`1[System.Threading.CancellationToken] CheckAndThrowToken
1253712534
Internal.Utilities.Library.Async2Module: System.Threading.AsyncLocal`1[System.Threading.CancellationToken] get_CheckAndThrowToken()
12538-
Internal.Utilities.Library.Async2Module: System.Threading.Tasks.Task`1[a] startAsTaskWithoutCancellation[a](Internal.Utilities.Library.Async2`1[a])
12539-
Internal.Utilities.Library.Async2Module: System.Threading.Tasks.Task`1[t] start[t](System.Threading.CancellationToken, Internal.Utilities.Library.Async2`1[t])
12535+
Internal.Utilities.Library.Async2Module: System.Threading.Tasks.Task`1[t] startInThreadPool[t](System.Threading.CancellationToken, Internal.Utilities.Library.Async2`1[t])
1254012536
Internal.Utilities.Library.Async2Module: a runWithoutCancellation[a](Internal.Utilities.Library.Async2`1[a])
12541-
Internal.Utilities.Library.Async2Module: t run[t](System.Threading.CancellationToken, Internal.Utilities.Library.Async2`1[t])
12537+
Internal.Utilities.Library.Async2Module: t runInThreadPool[t](System.Threading.CancellationToken, Internal.Utilities.Library.Async2`1[t])
1254212538
Internal.Utilities.Library.Async2`1[t]: Internal.Utilities.Library.IAsync2Invocation`1[t] StartImmediate(System.Threading.CancellationToken)
1254312539
Internal.Utilities.Library.Async2`1[t]: System.Runtime.CompilerServices.TaskAwaiter`1[t] GetAwaiter()
1254412540
Internal.Utilities.Library.Async2`1[t]: System.Runtime.CompilerServices.TaskAwaiter`1[t] StartBound(System.Threading.CancellationToken)

0 commit comments

Comments
 (0)