-
Notifications
You must be signed in to change notification settings - Fork 788
/
Cancellable.fs
201 lines (157 loc) · 6.39 KB
/
Cancellable.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
namespace FSharp.Compiler
open System
open System.Threading
open Internal.Utilities.Library
[<Sealed>]
type Cancellable =
static let token = AsyncLocal<CancellationToken>()
static member Token = token.Value
static member UsingToken(ct) =
let oldCt = token.Value
token.Value <- ct
{ new IDisposable with
member this.Dispose() = token.Value <- oldCt
}
static member CheckAndThrow() =
token.Value.ThrowIfCancellationRequested()
namespace Internal.Utilities.Library
open System
open System.Threading
open FSharp.Compiler
#if !FSHARPCORE_USE_PACKAGE
open FSharp.Core.CompilerServices.StateMachineHelpers
#endif
[<RequireQualifiedAccess; Struct>]
type ValueOrCancelled<'TResult> =
| Value of result: 'TResult
| Cancelled of ``exception``: OperationCanceledException
[<Struct>]
type Cancellable<'T> = Cancellable of (CancellationToken -> ValueOrCancelled<'T>)
module Cancellable =
let inline run (ct: CancellationToken) (Cancellable oper) =
if ct.IsCancellationRequested then
ValueOrCancelled.Cancelled(OperationCanceledException ct)
else
try
use _ = Cancellable.UsingToken(ct)
oper ct
with :? OperationCanceledException as e ->
ValueOrCancelled.Cancelled(OperationCanceledException e.CancellationToken)
let fold f acc seq =
Cancellable(fun ct ->
let mutable acc = ValueOrCancelled.Value acc
for x in seq do
match acc with
| ValueOrCancelled.Value accv -> acc <- run ct (f accv x)
| ValueOrCancelled.Cancelled _ -> ()
acc)
let runWithoutCancellation comp =
let res = run CancellationToken.None comp
match res with
| ValueOrCancelled.Cancelled _ -> failwith "unexpected cancellation"
| ValueOrCancelled.Value r -> r
let toAsync c =
async {
let! ct = Async.CancellationToken
let res = run ct c
return!
Async.FromContinuations(fun (cont, _econt, ccont) ->
match res with
| ValueOrCancelled.Value v -> cont v
| ValueOrCancelled.Cancelled ce -> ccont ce)
}
let token () = Cancellable(ValueOrCancelled.Value)
type CancellableBuilder() =
member inline _.Delay([<InlineIfLambda>] f) =
Cancellable(fun ct ->
let (Cancellable g) = f ()
g ct)
member inline _.Bind(comp, [<InlineIfLambda>] k) =
Cancellable(fun ct ->
#if !FSHARPCORE_USE_PACKAGE
__debugPoint ""
#endif
match Cancellable.run ct comp with
| ValueOrCancelled.Value v1 -> Cancellable.run ct (k v1)
| ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1)
member inline _.BindReturn(comp, [<InlineIfLambda>] k) =
Cancellable(fun ct ->
#if !FSHARPCORE_USE_PACKAGE
__debugPoint ""
#endif
match Cancellable.run ct comp with
| ValueOrCancelled.Value v1 -> ValueOrCancelled.Value(k v1)
| ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1)
member inline _.Combine(comp1, comp2) =
Cancellable(fun ct ->
#if !FSHARPCORE_USE_PACKAGE
__debugPoint ""
#endif
match Cancellable.run ct comp1 with
| ValueOrCancelled.Value() -> Cancellable.run ct comp2
| ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1)
member inline _.TryWith(comp, [<InlineIfLambda>] handler) =
Cancellable(fun ct ->
#if !FSHARPCORE_USE_PACKAGE
__debugPoint ""
#endif
let compRes =
try
match Cancellable.run ct comp with
| ValueOrCancelled.Value res -> ValueOrCancelled.Value(Choice1Of2 res)
| ValueOrCancelled.Cancelled exn -> ValueOrCancelled.Cancelled exn
with err ->
ValueOrCancelled.Value(Choice2Of2 err)
match compRes with
| ValueOrCancelled.Value res ->
match res with
| Choice1Of2 r -> ValueOrCancelled.Value r
| Choice2Of2 err -> Cancellable.run ct (handler err)
| ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1)
member inline _.Using(resource, [<InlineIfLambda>] comp) =
Cancellable(fun ct ->
#if !FSHARPCORE_USE_PACKAGE
__debugPoint ""
#endif
let body = comp resource
let compRes =
try
match Cancellable.run ct body with
| ValueOrCancelled.Value res -> ValueOrCancelled.Value(Choice1Of2 res)
| ValueOrCancelled.Cancelled exn -> ValueOrCancelled.Cancelled exn
with err ->
ValueOrCancelled.Value(Choice2Of2 err)
match compRes with
| ValueOrCancelled.Value res ->
Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.Dispose resource
match res with
| Choice1Of2 r -> ValueOrCancelled.Value r
| Choice2Of2 err -> raise err
| ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1)
member inline _.TryFinally(comp, [<InlineIfLambda>] compensation) =
Cancellable(fun ct ->
#if !FSHARPCORE_USE_PACKAGE
__debugPoint ""
#endif
let compRes =
try
match Cancellable.run ct comp with
| ValueOrCancelled.Value res -> ValueOrCancelled.Value(Choice1Of2 res)
| ValueOrCancelled.Cancelled exn -> ValueOrCancelled.Cancelled exn
with err ->
ValueOrCancelled.Value(Choice2Of2 err)
match compRes with
| ValueOrCancelled.Value res ->
compensation ()
match res with
| Choice1Of2 r -> ValueOrCancelled.Value r
| Choice2Of2 err -> raise err
| ValueOrCancelled.Cancelled err1 -> ValueOrCancelled.Cancelled err1)
member inline _.Return v =
Cancellable(fun _ -> ValueOrCancelled.Value v)
member inline _.ReturnFrom(v: Cancellable<'T>) = v
member inline _.Zero() =
Cancellable(fun _ -> ValueOrCancelled.Value())
[<AutoOpen>]
module CancellableAutoOpens =
let cancellable = CancellableBuilder()