-
Notifications
You must be signed in to change notification settings - Fork 800
/
Copy pathBuildGraph.fs
105 lines (83 loc) · 4.15 KB
/
BuildGraph.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
module FSharp.Compiler.BuildGraph
open System.Threading
open System.Threading.Tasks
open System.Globalization
[<RequireQualifiedAccess>]
module GraphNode =
// We need to store the culture for the VS thread that is executing now,
// so that when the agent in the async lazy object picks up thread from the thread pool we can set the culture
let mutable culture = CultureInfo(CultureInfo.CurrentUICulture.Name)
let SetPreferredUILang (preferredUiLang: string option) =
match preferredUiLang with
| Some s ->
culture <- CultureInfo s
Thread.CurrentThread.CurrentUICulture <- culture
| None -> ()
[<Sealed>]
type GraphNode<'T> private (computation: Async<'T>, cachedResult: ValueOption<'T>, cachedResultNode: Async<'T>) =
let mutable computation = computation
let mutable requestCount = 0
let mutable cachedResult = cachedResult
let mutable cachedResultNode: Async<'T> = cachedResultNode
let isCachedResultNodeNotNull () =
not (obj.ReferenceEquals(cachedResultNode, null))
let semaphore = new SemaphoreSlim(1, 1)
member _.GetOrComputeValue() =
// fast path
if isCachedResultNodeNotNull () then
cachedResultNode
else
async {
Interlocked.Increment(&requestCount) |> ignore
try
let! ct = Async.CancellationToken
// We must set 'taken' before any implicit cancellation checks
// occur, making sure we are under the protection of the 'try'.
// For example, NodeCode's 'try/finally' (TryFinally) uses async.TryFinally which does
// implicit cancellation checks even before the try is entered, as do the
// de-sugaring of 'do!' and other NodeCode constructs.
let mutable taken = false
try
do!
semaphore
.WaitAsync(ct)
.ContinueWith(
(fun _ -> taken <- true),
(TaskContinuationOptions.NotOnCanceled
||| TaskContinuationOptions.NotOnFaulted
||| TaskContinuationOptions.ExecuteSynchronously)
)
|> Async.AwaitTask
match cachedResult with
| ValueSome value -> return value
| _ ->
let tcs = TaskCompletionSource<'T>()
Async.StartWithContinuations(
async {
Thread.CurrentThread.CurrentUICulture <- GraphNode.culture
return! computation
},
(fun res ->
cachedResult <- ValueSome res
cachedResultNode <- async.Return res
computation <- Unchecked.defaultof<_>
tcs.SetResult(res)),
(fun ex -> tcs.SetException(ex)),
(fun _ -> tcs.SetCanceled()),
ct
)
return! tcs.Task |> Async.AwaitTask
finally
if taken then
semaphore.Release() |> ignore
finally
Interlocked.Decrement(&requestCount) |> ignore
}
member _.TryPeekValue() = cachedResult
member _.HasValue = cachedResult.IsSome
member _.IsComputing = requestCount > 0
static member FromResult(result: 'T) =
let nodeResult = async.Return result
GraphNode(nodeResult, ValueSome result, nodeResult)
new(computation) = GraphNode(computation, ValueNone, Unchecked.defaultof<_>)