@@ -16,18 +16,11 @@ open System.Threading
16
16
17
17
[<Sealed>]
18
18
type Cancellable =
19
- static let tokenHolder = AsyncLocal< CancellationToken voption >()
19
+ static let tokenHolder = AsyncLocal< CancellationToken>()
20
20
21
- static let guard =
22
- String.IsNullOrWhiteSpace( Environment.GetEnvironmentVariable( " DISABLE_CHECKANDTHROW_ASSERT" ))
21
+ static member HasCancellationToken = tokenHolder.Value <> CancellationToken.None
23
22
24
- static let ensureToken msg =
25
- tokenHolder.Value
26
- |> ValueOption.defaultWith ( fun () -> if guard then failwith msg else CancellationToken.None)
27
-
28
- static member HasCancellationToken = tokenHolder.Value.IsSome
29
-
30
- static member Token = ensureToken " Token not available outside of Cancellable computation."
23
+ static member Token = tokenHolder.Value
31
24
32
25
static member UseToken () =
33
26
async {
@@ -37,23 +30,22 @@ type Cancellable =
37
30
38
31
static member UsingToken ( ct ) =
39
32
let oldCt = tokenHolder.Value
40
- tokenHolder.Value <- ValueSome ct
33
+ tokenHolder.Value <- ct
41
34
42
35
{ new IDisposable with
43
36
member _.Dispose () = tokenHolder.Value <- oldCt
44
37
}
45
38
46
39
static member CheckAndThrow () =
47
- let token = ensureToken " CheckAndThrow invoked outside of Cancellable computation."
48
- token.ThrowIfCancellationRequested()
40
+ tokenHolder.Value.ThrowIfCancellationRequested()
49
41
50
42
static member TryCheckAndThrow () =
51
- match tokenHolder.Value with
52
- | ValueNone -> ()
53
- | ValueSome token -> token.ThrowIfCancellationRequested()
43
+ tokenHolder.Value.ThrowIfCancellationRequested()
54
44
55
45
namespace Internal.Utilities.Library.CancellableImplementation
56
46
47
+ type Cancellable = FSharp.Compiler.Cancellable
48
+
57
49
open System
58
50
open System.Threading
59
51
@@ -62,7 +54,6 @@ open FSharp.Core.CompilerServices.StateMachineHelpers
62
54
open Microsoft.FSharp .Core .CompilerServices
63
55
open System.Runtime .CompilerServices
64
56
open System.Runtime .ExceptionServices
65
- open System.Diagnostics
66
57
67
58
type ITrampolineInvocation =
68
59
abstract member MoveNext: unit -> bool
@@ -80,7 +71,7 @@ type PendingInvocation =
80
71
| Immediate of ITrampolineInvocation
81
72
82
73
[<Sealed>]
83
- type Trampoline ( cancellationToken : CancellationToken ) =
74
+ type Trampoline () =
84
75
85
76
let mutable bindDepth = 0
86
77
@@ -101,11 +92,6 @@ type Trampoline(cancellationToken: CancellationToken) =
101
92
edi.Throw()
102
93
| _ -> ()
103
94
104
- member this.IsCancelled = cancellationToken.IsCancellationRequested
105
-
106
- member this.ThrowIfCancellationRequested () =
107
- cancellationToken.ThrowIfCancellationRequested()
108
-
109
95
member this.ShoudBounce = bindDepth % bindDepthLimit = 0
110
96
111
97
member this.SetDelayed ( invocation ) = pending.Push( Delayed invocation)
@@ -142,8 +128,8 @@ type Trampoline(cancellationToken: CancellationToken) =
142
128
143
129
static member Current = current.Value.Value
144
130
145
- static member Install ct =
146
- current.Value <- ValueSome <| Trampoline ct
131
+ static member Install () =
132
+ current.Value <- ValueSome <| Trampoline()
147
133
148
134
type ITrampolineInvocation < 'T > =
149
135
inherit ITrampolineInvocation
@@ -174,12 +160,15 @@ type Cancellable<'T>(clone: unit -> ITrampolineInvocation<'T>) =
174
160
[<AutoOpen>]
175
161
module CancellableCode =
176
162
177
- let inline filterCancellation ( catch : exn -> CancellableCode < _ , _ >) exn =
178
- CancellableCode( fun sm -> Trampoline.Current.IsCancelled || ( catch exn) .Invoke(& sm))
163
+ let inline filterCancellation ( catch : exn -> CancellableCode < _ , _ >) ( exn : exn ) =
164
+ CancellableCode( fun sm ->
165
+ match exn with
166
+ | :? OperationCanceledException as oce when oce.CancellationToken = Cancellable.Token -> raise exn
167
+ | _ -> ( catch exn) .Invoke(& sm))
179
168
180
169
let inline throwIfCancellationRequested ( code : CancellableCode < _ , _ >) =
181
170
CancellableCode( fun sm ->
182
- Trampoline.Current .ThrowIfCancellationRequested()
171
+ Cancellable.Token .ThrowIfCancellationRequested()
183
172
code.Invoke(& sm))
184
173
185
174
type CancellableBuilder () =
@@ -194,6 +183,7 @@ type CancellableBuilder() =
194
183
CancellableCode< 'T, _>( fun sm ->
195
184
sm.Data <- value
196
185
true )
186
+ |> throwIfCancellationRequested
197
187
198
188
member inline _.Combine
199
189
( code1 : CancellableCode < 'TOverall , unit >, code2 : CancellableCode < 'TOverall , 'T >)
@@ -307,7 +297,6 @@ type CancellableBuilder() =
307
297
308
298
namespace Internal.Utilities.Library
309
299
310
- open System
311
300
open System.Threading
312
301
313
302
type Cancellable < 'T > = CancellableImplementation.Cancellable< 'T>
@@ -322,19 +311,19 @@ module Cancellable =
322
311
323
312
let run ( code : Cancellable < _ >) =
324
313
let invocation = code.GetInvocation()
325
- Trampoline.Install FSharp.Compiler.Cancellable.Token
314
+ Trampoline.Install()
326
315
Trampoline.Current.RunImmediate invocation
327
316
invocation.Result
328
317
329
318
let runWithoutCancellation code =
330
- use _ = FSharp.Compiler. Cancellable.UsingToken CancellationToken.None
319
+ use _ = Cancellable.UsingToken CancellationToken.None
331
320
run code
332
321
333
322
let toAsync code =
334
323
async {
335
- use! _holder = FSharp.Compiler. Cancellable.UseToken()
324
+ use! _holder = Cancellable.UseToken()
336
325
return run code
337
326
}
338
327
339
328
let token () =
340
- cancellable { FSharp.Compiler. Cancellable.Token }
329
+ cancellable { Cancellable.Token }
0 commit comments