Skip to content

Commit b6ee38c

Browse files
committed
revert some
1 parent 0a25a60 commit b6ee38c

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/Compiler/Utilities/Cancellable.fs

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type ITrampolineInvocation =
7272
type CancellableStateMachineData<'T> =
7373

7474
[<DefaultValue(false)>]
75-
val mutable Result: 'T
75+
val mutable Result: Result<'T, ExceptionDispatchInfo>
7676

7777
and CancellableStateMachine<'TOverall> = ResumableStateMachine<CancellableStateMachineData<'TOverall>>
7878
and ICancellableStateMachine<'TOverall> = IResumableStateMachine<CancellableStateMachineData<'TOverall>>
@@ -84,7 +84,7 @@ and CancellableCode<'TOverall, 'T> = ResumableCode<CancellableStateMachineData<'
8484
type Trampoline(cancellationToken: CancellationToken) =
8585
let mutable bindDepth = 0
8686

87-
let stack = Collections.Generic.Stack<ITrampolineInvocation>()
87+
let stack = System.Collections.Generic.Stack<ITrampolineInvocation>()
8888

8989
static let current = new ThreadLocal<Trampoline>()
9090

@@ -99,6 +99,8 @@ type Trampoline(cancellationToken: CancellationToken) =
9999

100100
member _.Set(invocation) = stack.Push(invocation)
101101

102+
member val ExceptionMap = ConditionalWeakTable<exn, ExceptionDispatchInfo>()
103+
102104
[<DebuggerHidden>]
103105
member this.Execute(invocation) =
104106
bindDepth <- bindDepth + 1
@@ -119,24 +121,44 @@ type ITrampolineInvocation<'T> =
119121
inherit ITrampolineInvocation
120122
abstract Result: 'T
121123

124+
[<AutoOpen>]
125+
module ExceptionDispatchInfoHelpers =
126+
type ExceptionDispatchInfo with
127+
member edi.ThrowAny() =
128+
edi.Throw()
129+
Unchecked.defaultof<_>
130+
131+
static member RestoreOrCapture(exn: exn) =
132+
match Trampoline.Current.ExceptionMap.TryGetValue exn with
133+
| true, edi -> edi
134+
| _ ->
135+
let edi = ExceptionDispatchInfo.Capture exn
136+
Trampoline.Current.ExceptionMap.Add(exn, edi)
137+
edi
138+
139+
[<NoEquality; NoComparison>]
122140
type ICancellableInvokable<'T> =
123141
abstract Create: unit -> ITrampolineInvocation<'T>
124142

125143
[<NoEquality; NoComparison>]
126144
type CancellableInvocation<'T, 'Machine when 'Machine :> IAsyncStateMachine and 'Machine :> ICancellableStateMachine<'T>>(machine: 'Machine)
127145
=
128-
129146
let mutable machine = machine
130147

131148
interface ITrampolineInvocation<'T> with
132-
member _.MoveNext() = machine.MoveNext()
149+
member this.MoveNext() = machine.MoveNext()
150+
151+
member _.Result =
152+
match machine.Data.Result with
153+
| Ok value -> value
154+
| Error edi -> edi.ThrowAny()
155+
133156
member _.IsCompleted = machine.ResumptionPoint = -1
134-
member _.Result = machine.Data.Result
135157

136158
interface ICancellableInvokable<'T> with
137159
member _.Create() = CancellableInvocation<_, _>(machine)
138160

139-
[<Struct; NoComparison>]
161+
[<Struct; NoComparison; NoEquality>]
140162
type Cancellable<'T>(invokable: ICancellableInvokable<'T>) =
141163

142164
member _.GetInvocation() = invokable.Create()
@@ -166,7 +188,7 @@ type CancellableBuilder() =
166188

167189
member inline _.Return(value: 'T) : CancellableCode<'T, 'T> =
168190
CancellableCode<'T, _>(fun sm ->
169-
sm.Data.Result <- value
191+
sm.Data.Result <- Ok value
170192
true)
171193

172194
member inline _.Combine
@@ -237,7 +259,6 @@ type CancellableBuilder() =
237259
else
238260
Trampoline.Current.Execute invocation
239261
(continuation invocation.Result).Invoke(&sm))
240-
|> throwIfCancellationRequested
241262

242263
member inline this.ReturnFrom(comp: Cancellable<'T>) : CancellableCode<'T, 'T> = this.Bind(comp, this.Return)
243264

@@ -247,10 +268,15 @@ type CancellableBuilder() =
247268

248269
(MoveNextMethodImpl<_>(fun sm ->
249270
__resumeAt sm.ResumptionPoint
250-
let __stack_code_fin = code.Invoke(&sm)
251271

252-
if __stack_code_fin then
253-
sm.ResumptionPoint <- -1))
272+
try
273+
let __stack_code_fin = code.Invoke(&sm)
274+
275+
if __stack_code_fin then
276+
sm.ResumptionPoint <- -1
277+
with exn ->
278+
sm.ResumptionPoint <- -1
279+
sm.Data.Result <- Error <| ExceptionDispatchInfo.RestoreOrCapture exn))
254280

255281
(SetStateMachineMethodImpl<_>(fun _ _ -> ()))
256282

@@ -263,8 +289,12 @@ type CancellableBuilder() =
263289
let resumptionInfo =
264290
{ new CancellableResumptionDynamicInfo<_>(initialResumptionFunc) with
265291
member info.MoveNext(sm) =
266-
if info.ResumptionFunc.Invoke(&sm) then
292+
try
293+
if info.ResumptionFunc.Invoke(&sm) then
294+
sm.ResumptionPoint <- -1
295+
with exn ->
267296
sm.ResumptionPoint <- -1
297+
sm.Data.Result <- Error <| ExceptionDispatchInfo.RestoreOrCapture exn
268298

269299
member _.SetStateMachine(_, _) = ()
270300
}

src/Compiler/Utilities/Cancellable.fsi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ type Cancellable =
1515
namespace Internal.Utilities.Library.CancellableImplementation
1616

1717
open System
18-
open System.Threading
1918
open Microsoft.FSharp.Core.CompilerServices
2019
open System.Runtime.CompilerServices
20+
open System.Runtime.ExceptionServices
2121

2222
type internal ITrampolineInvocation =
2323
abstract MoveNext: unit -> unit
2424
abstract IsCompleted: bool
2525

2626
[<Struct; NoComparison; NoEquality>]
2727
type internal CancellableStateMachineData<'T> =
28-
val mutable Result: 'T
28+
val mutable Result: Result<'T, ExceptionDispatchInfo>
2929

3030
and internal CancellableStateMachine<'TOverall> = ResumableStateMachine<CancellableStateMachineData<'TOverall>>
3131
and internal ICancellableStateMachine<'TOverall> = IResumableStateMachine<CancellableStateMachineData<'TOverall>>
@@ -56,11 +56,17 @@ type internal CancellableInvocation<'T, 'Machine
5656
interface ITrampolineInvocation<'T>
5757
new: machine: 'Machine -> CancellableInvocation<'T, 'Machine>
5858

59-
[<Struct; NoComparison>]
59+
[<Struct; NoComparison; NoEquality>]
6060
type internal Cancellable<'T> =
6161
new: invokable: ICancellableInvokable<'T> -> Cancellable<'T>
6262
member GetInvocation: unit -> ITrampolineInvocation<'T>
6363

64+
[<AutoOpen>]
65+
module internal ExceptionDispatchInfoHelpers =
66+
type ExceptionDispatchInfo with
67+
member ThrowAny: unit -> 'T
68+
static member RestoreOrCapture: exn -> ExceptionDispatchInfo
69+
6470
type internal CancellableBuilder =
6571
new: unit -> CancellableBuilder
6672

0 commit comments

Comments
 (0)