Skip to content

Commit ed662c5

Browse files
committed
keep on fighting
1 parent 2653caf commit ed662c5

12 files changed

+389
-497
lines changed

src/IcedTasks/CancellablePoolingValueTask.fs

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,71 +59,58 @@ module CancellablePoolingValueTasks =
5959

6060
{ new CancellableTaskBaseResumptionDynamicInfo<'T, _>(initialResumptionFunc) with
6161
member info.MoveNext(sm) =
62-
let mutable keepGoing = true
62+
let mutable continuation = Stop
63+
let current = state
6364

64-
while keepGoing do
65-
keepGoing <- false
66-
67-
let current = state
68-
69-
match current with
70-
| InitialYield ->
71-
state <- Running
65+
match current with
66+
| InitialYield ->
67+
state <- Running
7268

69+
continuation <-
7370
if BindDepthCounter.Check() then
74-
MethodBuilder.AwaitUnsafeOnCompleted(
75-
&sm.Data.MethodBuilder,
76-
Trampoline.Current.AwaiterRef,
77-
&sm
78-
)
71+
Await Trampoline.Current
7972
else
80-
keepGoing <- true
81-
| Running ->
82-
try
83-
let step = info.ResumptionFunc.Invoke(&sm)
73+
Immediate
74+
| Running ->
75+
try
76+
let step = info.ResumptionFunc.Invoke(&sm)
8477

85-
if step then
86-
state <- SetResult
78+
if step then
79+
state <- SetResult
8780

81+
continuation <-
8882
if BindDepthCounter.Check() then
89-
// Yield before setting result to prevent stack overflow.
90-
MethodBuilder.AwaitUnsafeOnCompleted(
91-
&sm.Data.MethodBuilder,
92-
Trampoline.Current.AwaiterRef,
93-
&sm
94-
)
83+
Await Trampoline.Current
9584
else
96-
keepGoing <- true
97-
else
98-
match sm.ResumptionDynamicInfo.ResumptionData with
99-
| :? ICriticalNotifyCompletion as awaiter ->
100-
let mutable awaiter = awaiter
101-
102-
MethodBuilder.AwaitUnsafeOnCompleted(
103-
&sm.Data.MethodBuilder,
104-
&awaiter,
105-
&sm
106-
)
107-
| _ -> ()
108-
with exn ->
109-
state <- SetException(ExceptionCache.CaptureOrRetrieve exn)
85+
Immediate
86+
else
87+
continuation <-
88+
Await(downcast sm.ResumptionDynamicInfo.ResumptionData)
89+
with exn ->
90+
state <- SetException(ExceptionCache.CaptureOrRetrieve exn)
11091

92+
continuation <-
11193
if BindDepthCounter.Check() then
112-
// Yield before setting exception to prevent stack overflow.
113-
MethodBuilder.AwaitUnsafeOnCompleted(
114-
&sm.Data.MethodBuilder,
115-
Trampoline.Current.AwaiterRef,
116-
&sm
117-
)
94+
Await Trampoline.Current
11895
else
119-
keepGoing <- true
120-
| SetResult ->
121-
MethodBuilder.SetResult(&sm.Data.MethodBuilder, sm.Data.Result)
122-
| SetException edi ->
123-
MethodBuilder.SetException(
124-
&sm.Data.MethodBuilder,
125-
edi.SourceException
126-
)
96+
Immediate
97+
| SetResult ->
98+
MethodBuilder.SetResult(&sm.Data.MethodBuilder, sm.Data.Result)
99+
| SetException edi ->
100+
MethodBuilder.SetException(&sm.Data.MethodBuilder, edi.SourceException)
101+
102+
match continuation with
103+
| Await awaiter ->
104+
sm.ResumptionDynamicInfo.ResumptionData <- null
105+
let mutable awaiter = awaiter
106+
107+
MethodBuilder.AwaitUnsafeOnCompleted(
108+
&sm.Data.MethodBuilder,
109+
&awaiter,
110+
&sm
111+
)
112+
| Immediate -> info.MoveNext &sm
113+
| Stop -> ()
127114

128115
member _.SetStateMachine(sm, state) =
129116
MethodBuilder.SetStateMachine(&sm.Data.MethodBuilder, state)

src/IcedTasks/CancellableTask.fs

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -57,71 +57,58 @@ module CancellableTasks =
5757

5858
{ new CancellableTaskBaseResumptionDynamicInfo<'T, _>(initialResumptionFunc) with
5959
member info.MoveNext(sm) =
60-
let mutable keepGoing = true
60+
let mutable continuation = Stop
61+
let current = state
6162

62-
while keepGoing do
63-
keepGoing <- false
64-
65-
let current = state
66-
67-
match current with
68-
| InitialYield ->
69-
state <- Running
63+
match current with
64+
| InitialYield ->
65+
state <- Running
7066

67+
continuation <-
7168
if BindDepthCounter.Check() then
72-
MethodBuilder.AwaitUnsafeOnCompleted(
73-
&sm.Data.MethodBuilder,
74-
Trampoline.Current.AwaiterRef,
75-
&sm
76-
)
69+
Await Trampoline.Current
7770
else
78-
keepGoing <- true
79-
| Running ->
80-
try
81-
let step = info.ResumptionFunc.Invoke(&sm)
71+
Immediate
72+
| Running ->
73+
try
74+
let step = info.ResumptionFunc.Invoke(&sm)
8275

83-
if step then
84-
state <- SetResult
76+
if step then
77+
state <- SetResult
8578

79+
continuation <-
8680
if BindDepthCounter.Check() then
87-
// Yield before setting result to prevent stack overflow.
88-
MethodBuilder.AwaitUnsafeOnCompleted(
89-
&sm.Data.MethodBuilder,
90-
Trampoline.Current.AwaiterRef,
91-
&sm
92-
)
81+
Await Trampoline.Current
9382
else
94-
keepGoing <- true
95-
else
96-
match sm.ResumptionDynamicInfo.ResumptionData with
97-
| :? ICriticalNotifyCompletion as awaiter ->
98-
let mutable awaiter = awaiter
99-
100-
MethodBuilder.AwaitUnsafeOnCompleted(
101-
&sm.Data.MethodBuilder,
102-
&awaiter,
103-
&sm
104-
)
105-
| _ -> ()
106-
with exn ->
107-
state <- SetException(ExceptionCache.CaptureOrRetrieve exn)
83+
Immediate
84+
else
85+
continuation <-
86+
Await(downcast sm.ResumptionDynamicInfo.ResumptionData)
87+
with exn ->
88+
state <- SetException(ExceptionCache.CaptureOrRetrieve exn)
10889

90+
continuation <-
10991
if BindDepthCounter.Check() then
110-
// Yield before setting exception to prevent stack overflow.
111-
MethodBuilder.AwaitUnsafeOnCompleted(
112-
&sm.Data.MethodBuilder,
113-
Trampoline.Current.AwaiterRef,
114-
&sm
115-
)
92+
Await Trampoline.Current
11693
else
117-
keepGoing <- true
118-
| SetResult ->
119-
MethodBuilder.SetResult(&sm.Data.MethodBuilder, sm.Data.Result)
120-
| SetException edi ->
121-
MethodBuilder.SetException(
122-
&sm.Data.MethodBuilder,
123-
edi.SourceException
124-
)
94+
Immediate
95+
| SetResult ->
96+
MethodBuilder.SetResult(&sm.Data.MethodBuilder, sm.Data.Result)
97+
| SetException edi ->
98+
MethodBuilder.SetException(&sm.Data.MethodBuilder, edi.SourceException)
99+
100+
match continuation with
101+
| Await awaiter ->
102+
sm.ResumptionDynamicInfo.ResumptionData <- null
103+
let mutable awaiter = awaiter
104+
105+
MethodBuilder.AwaitUnsafeOnCompleted(
106+
&sm.Data.MethodBuilder,
107+
&awaiter,
108+
&sm
109+
)
110+
| Immediate -> info.MoveNext &sm
111+
| Stop -> ()
125112

126113
member _.SetStateMachine(sm, state) =
127114
MethodBuilder.SetStateMachine(&sm.Data.MethodBuilder, state)

src/IcedTasks/CancellableTaskBuilderBase.fs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ module CancellableTaskBase =
5757
if not __stack_yield_fin then
5858
let mutable __stack_awaiter = Trampoline.Current
5959

60-
MethodBuilder.AwaitUnsafeOnCompleted(
61-
&sm.Data.MethodBuilder,
62-
&__stack_awaiter,
63-
&sm
64-
)
60+
MethodBuilder.AwaitOnCompleted(&sm.Data.MethodBuilder, &__stack_awaiter, &sm)
6561

6662
__stack_yield_fin
6763
else

src/IcedTasks/CancellableValueTask.fs

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,69 +59,58 @@ module CancellableValueTasks =
5959

6060
{ new CancellableTaskBaseResumptionDynamicInfo<'T, _>(initialResumptionFunc) with
6161
member info.MoveNext(sm) =
62-
let mutable keepGoing = true
62+
let mutable continuation = Stop
63+
let current = state
6364

64-
while keepGoing do
65-
keepGoing <- false
66-
67-
let current = state
68-
69-
match current with
70-
| InitialYield ->
71-
state <- Running
65+
match current with
66+
| InitialYield ->
67+
state <- Running
7268

69+
continuation <-
7370
if BindDepthCounter.Check() then
74-
MethodBuilder.AwaitUnsafeOnCompleted(
75-
&sm.Data.MethodBuilder,
76-
Trampoline.Current.AwaiterRef,
77-
&sm
78-
)
71+
Await Trampoline.Current
7972
else
80-
keepGoing <- true
81-
| Running ->
82-
try
83-
let step = info.ResumptionFunc.Invoke(&sm)
73+
Immediate
74+
| Running ->
75+
try
76+
let step = info.ResumptionFunc.Invoke(&sm)
8477

85-
if step then
86-
state <- SetResult
78+
if step then
79+
state <- SetResult
8780

81+
continuation <-
8882
if BindDepthCounter.Check() then
89-
MethodBuilder.AwaitUnsafeOnCompleted(
90-
&sm.Data.MethodBuilder,
91-
Trampoline.Current.AwaiterRef,
92-
&sm
93-
)
83+
Await Trampoline.Current
9484
else
95-
keepGoing <- true
96-
else
97-
match sm.ResumptionDynamicInfo.ResumptionData with
98-
| :? ICriticalNotifyCompletion as awaiter ->
99-
let mutable awaiter = awaiter
100-
101-
MethodBuilder.AwaitUnsafeOnCompleted(
102-
&sm.Data.MethodBuilder,
103-
&awaiter,
104-
&sm
105-
)
106-
| _ -> ()
107-
with exn ->
108-
state <- SetException(ExceptionCache.CaptureOrRetrieve exn)
85+
Immediate
86+
else
87+
continuation <-
88+
Await(downcast sm.ResumptionDynamicInfo.ResumptionData)
89+
with exn ->
90+
state <- SetException(ExceptionCache.CaptureOrRetrieve exn)
10991

92+
continuation <-
11093
if BindDepthCounter.Check() then
111-
MethodBuilder.AwaitUnsafeOnCompleted(
112-
&sm.Data.MethodBuilder,
113-
Trampoline.Current.AwaiterRef,
114-
&sm
115-
)
94+
Await Trampoline.Current
11695
else
117-
keepGoing <- true
118-
| SetResult ->
119-
MethodBuilder.SetResult(&sm.Data.MethodBuilder, sm.Data.Result)
120-
| SetException edi ->
121-
MethodBuilder.SetException(
122-
&sm.Data.MethodBuilder,
123-
edi.SourceException
124-
)
96+
Immediate
97+
| SetResult ->
98+
MethodBuilder.SetResult(&sm.Data.MethodBuilder, sm.Data.Result)
99+
| SetException edi ->
100+
MethodBuilder.SetException(&sm.Data.MethodBuilder, edi.SourceException)
101+
102+
match continuation with
103+
| Await awaiter ->
104+
sm.ResumptionDynamicInfo.ResumptionData <- null
105+
let mutable awaiter = awaiter
106+
107+
MethodBuilder.AwaitUnsafeOnCompleted(
108+
&sm.Data.MethodBuilder,
109+
&awaiter,
110+
&sm
111+
)
112+
| Immediate -> info.MoveNext &sm
113+
| Stop -> ()
125114

126115
member _.SetStateMachine(sm, state) =
127116
MethodBuilder.SetStateMachine(&sm.Data.MethodBuilder, state)

0 commit comments

Comments
 (0)