Skip to content

Commit 2c61a6e

Browse files
Use semaphoreslim in incremental builder (#14904)
Co-authored-by: Kevin Ransom (msft) <codecutter@hotmail.com>
1 parent 9fae71f commit 2c61a6e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Compiler/Facilities/BuildGraph.fsi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ type NodeCode =
7070

7171
static member Parallel: computations: (NodeCode<'T> seq) -> NodeCode<'T[]>
7272

73+
static member AwaitTask: task: Task<'T> -> NodeCode<'T>
74+
75+
static member AwaitTask: task: Task -> NodeCode<unit>
76+
7377
/// Execute the cancellable computation synchronously using the ambient cancellation token of
7478
/// the NodeCode.
7579
static member FromCancellable: computation: Cancellable<'T> -> NodeCode<'T>

src/Compiler/Service/IncrementalBuild.fs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ type FrameworkImportsCache(size) =
648648
lazyWork
649649
)
650650
node
651+
651652

652653
/// This function strips the "System" assemblies from the tcConfig and returns a age-cached TcImports for them.
653654
member this.Get(tcConfig: TcConfig) =
@@ -1248,19 +1249,24 @@ type IncrementalBuilder(initialState: IncrementalBuilderInitialState, state: Inc
12481249
let t2 = MaxTimeStampInDependencies state.logicalStampedFileNames fileSlot
12491250
max t1 t2
12501251

1251-
let gate = obj()
1252+
let semaphore = new SemaphoreSlim(1,1)
1253+
12521254
let mutable currentState = state
12531255

12541256
let setCurrentState state cache (ct: CancellationToken) =
1255-
lock gate (fun () ->
1256-
ct.ThrowIfCancellationRequested()
1257-
currentState <- computeStampedFileNames initialState state cache
1258-
)
1257+
node {
1258+
do! semaphore.WaitAsync(ct) |> NodeCode.AwaitTask
1259+
try
1260+
ct.ThrowIfCancellationRequested()
1261+
currentState <- computeStampedFileNames initialState state cache
1262+
finally
1263+
semaphore.Release() |> ignore
1264+
}
12591265

12601266
let checkFileTimeStamps (cache: TimeStampCache) =
12611267
node {
12621268
let! ct = NodeCode.CancellationToken
1263-
setCurrentState currentState cache ct
1269+
do! setCurrentState currentState cache ct
12641270
}
12651271

12661272
do IncrementalBuilderEventTesting.MRU.Add(IncrementalBuilderEventTesting.IBECreated)
@@ -1443,7 +1449,7 @@ type IncrementalBuilder(initialState: IncrementalBuilderInitialState, state: Inc
14431449
let newState = { currentState with notifiedStampedFileNames = currentState.notifiedStampedFileNames.SetItem(slotOfFile, timeStamp) }
14441450
let cache = TimeStampCache defaultTimeStamp
14451451
let! ct = NodeCode.CancellationToken
1446-
setCurrentState newState cache ct
1452+
do! setCurrentState newState cache ct
14471453
}
14481454

14491455
member _.SourceFiles = fileNames |> Seq.map (fun f -> f.Source.FilePath) |> List.ofSeq

0 commit comments

Comments
 (0)