@@ -16,18 +16,11 @@ open System.Threading
1616
1717[<Sealed>]
1818type Cancellable =
19- static let tokenHolder = AsyncLocal< CancellationToken voption >()
19+ static let tokenHolder = AsyncLocal< CancellationToken>()
2020
21- static let guard =
22- String.IsNullOrWhiteSpace( Environment.GetEnvironmentVariable( " DISABLE_CHECKANDTHROW_ASSERT" ))
21+ static member HasCancellationToken = tokenHolder.Value <> CancellationToken.None
2322
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
3124
3225 static member UseToken () =
3326 async {
@@ -37,23 +30,22 @@ type Cancellable =
3730
3831 static member UsingToken ( ct ) =
3932 let oldCt = tokenHolder.Value
40- tokenHolder.Value <- ValueSome ct
33+ tokenHolder.Value <- ct
4134
4235 { new IDisposable with
4336 member _.Dispose () = tokenHolder.Value <- oldCt
4437 }
4538
4639 static member CheckAndThrow () =
47- let token = ensureToken " CheckAndThrow invoked outside of Cancellable computation."
48- token.ThrowIfCancellationRequested()
40+ tokenHolder.Value.ThrowIfCancellationRequested()
4941
5042 static member TryCheckAndThrow () =
51- match tokenHolder.Value with
52- | ValueNone -> ()
53- | ValueSome token -> token.ThrowIfCancellationRequested()
43+ tokenHolder.Value.ThrowIfCancellationRequested()
5444
5545namespace Internal.Utilities.Library.CancellableImplementation
5646
47+ type Cancellable = FSharp.Compiler.Cancellable
48+
5749open System
5850open System.Threading
5951
@@ -62,7 +54,6 @@ open FSharp.Core.CompilerServices.StateMachineHelpers
6254open Microsoft.FSharp .Core .CompilerServices
6355open System.Runtime .CompilerServices
6456open System.Runtime .ExceptionServices
65- open System.Diagnostics
6657
6758type ITrampolineInvocation =
6859 abstract member MoveNext: unit -> bool
@@ -80,7 +71,7 @@ type PendingInvocation =
8071 | Immediate of ITrampolineInvocation
8172
8273[<Sealed>]
83- type Trampoline ( cancellationToken : CancellationToken ) =
74+ type Trampoline () =
8475
8576 let mutable bindDepth = 0
8677
@@ -101,11 +92,6 @@ type Trampoline(cancellationToken: CancellationToken) =
10192 edi.Throw()
10293 | _ -> ()
10394
104- member this.IsCancelled = cancellationToken.IsCancellationRequested
105-
106- member this.ThrowIfCancellationRequested () =
107- cancellationToken.ThrowIfCancellationRequested()
108-
10995 member this.ShoudBounce = bindDepth % bindDepthLimit = 0
11096
11197 member this.SetDelayed ( invocation ) = pending.Push( Delayed invocation)
@@ -142,8 +128,8 @@ type Trampoline(cancellationToken: CancellationToken) =
142128
143129 static member Current = current.Value.Value
144130
145- static member Install ct =
146- current.Value <- ValueSome <| Trampoline ct
131+ static member Install () =
132+ current.Value <- ValueSome <| Trampoline()
147133
148134type ITrampolineInvocation < 'T > =
149135 inherit ITrampolineInvocation
@@ -174,12 +160,15 @@ type Cancellable<'T>(clone: unit -> ITrampolineInvocation<'T>) =
174160[<AutoOpen>]
175161module CancellableCode =
176162
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))
179168
180169 let inline throwIfCancellationRequested ( code : CancellableCode < _ , _ >) =
181170 CancellableCode( fun sm ->
182- Trampoline.Current .ThrowIfCancellationRequested()
171+ Cancellable.Token .ThrowIfCancellationRequested()
183172 code.Invoke(& sm))
184173
185174type CancellableBuilder () =
@@ -194,6 +183,7 @@ type CancellableBuilder() =
194183 CancellableCode< 'T, _>( fun sm ->
195184 sm.Data <- value
196185 true )
186+ |> throwIfCancellationRequested
197187
198188 member inline _.Combine
199189 ( code1 : CancellableCode < 'TOverall , unit >, code2 : CancellableCode < 'TOverall , 'T >)
@@ -307,7 +297,6 @@ type CancellableBuilder() =
307297
308298namespace Internal.Utilities.Library
309299
310- open System
311300open System.Threading
312301
313302type Cancellable < 'T > = CancellableImplementation.Cancellable< 'T>
@@ -322,19 +311,19 @@ module Cancellable =
322311
323312 let run ( code : Cancellable < _ >) =
324313 let invocation = code.GetInvocation()
325- Trampoline.Install FSharp.Compiler.Cancellable.Token
314+ Trampoline.Install()
326315 Trampoline.Current.RunImmediate invocation
327316 invocation.Result
328317
329318 let runWithoutCancellation code =
330- use _ = FSharp.Compiler. Cancellable.UsingToken CancellationToken.None
319+ use _ = Cancellable.UsingToken CancellationToken.None
331320 run code
332321
333322 let toAsync code =
334323 async {
335- use! _holder = FSharp.Compiler. Cancellable.UseToken()
324+ use! _holder = Cancellable.UseToken()
336325 return run code
337326 }
338327
339328 let token () =
340- cancellable { FSharp.Compiler. Cancellable.Token }
329+ cancellable { Cancellable.Token }
0 commit comments