tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

271 lignes · 6409 octetsBrut · Historique
1// Handler-stack mappings are reusable only when no continuation can restore
2// their old addresses (issue #329). Exercise one-shot return/cancellation,
3// multi-shot copies and final cancellation with fresh handlers in between;
4// the last stale resume must still panic rather than reach a new
5// occupant of its old mapping.
6
7import std/io
8import std/list
9import std/process.{type Subject, type proc}
10
11effect ask
12{
13 ctl ask :: -> Int
14}
15
16effect amb
17{
18 multi ctl choose :: -> Bool
19}
20
21// Every iteration installs and completes a fresh one-shot handler.
22fn one_shots :: left: Int total: Int -> Int
23{
24 if left == 0
25 {
26 total
27 }
28 else
29 {
30 let answer = handle { ask () + 1 } with { ask -> resume 40 }
31 one_shots (left - 1) (total + answer)
32 }
33}
34
35// Not resuming abandons the body. Its suspended stack is not finished until
36// cancellation has run the cleanup and driven the coroutine to Return.
37fn cancel_once :: -> Int / <io>
38{
39 handle
40 {
41 defer io.println "one-shot cancelled"
42 ask ()
43 }
44 with
45 { ask -> 5 }
46}
47
48// A non-local return leaves the resumed handler while unwinding is active.
49fn return_from_once :: -> Int
50{
51 let answer = handle
52 {
53 let answer = ask ()
54 return answer + 2
55 }
56 with
57 { ask -> resume 40 }
58 answer
59}
60
61// The first call copies the capture and the second is its last use, so this
62// case exercises both resume modes and ends with no saved capture left.
63fn choose_both :: -> List<Int>
64{
65 handle
66 {
67 if choose ()
68 {
69 10
70 }
71 else
72 {
73 20
74 }
75 }
76 with
77 { choose -> list.append (resume True) (resume False), return n -> [n] }
78}
79
80fn many_multi :: left: Int total: Int -> Int
81{
82 if left == 0
83 {
84 total
85 }
86 else
87 {
88 many_multi (left - 1) (total + list.sum (choose_both ()))
89 }
90}
91
92// No resume at all: the live capture is cancelled at final finish.
93fn cancel_multi :: -> Int / <io>
94{
95 handle
96 {
97 defer io.println "multi-shot cancelled"
98 if choose ()
99 {
100 1
101 }
102 else
103 {
104 2
105 }
106 }
107 with
108 { choose -> 9 }
109}
110
111fn first_of ::
112 ks: List<fn(Bool) -> List<Int> / <e>>
113 value: Bool
114 -> List<Int> / <e>
115{
116 match ks { [k, ..] -> k value, [] -> [] }
117}
118
119// After the first copied resume, the original capture is Saved. Complete
120// fresh one-shot handlers before the second, last-use resume materialises
121// that capture and marks it Gone. Final-discard coverage stays in
122// multishot.lse's all_kept; this case checks intervening-stack restoration.
123fn saved_with_handlers_between :: -> List<Int>
124{
125 handle
126 {
127 if choose ()
128 {
129 10
130 }
131 else
132 {
133 20
134 }
135 }
136 with
137 { choose ->
138 {
139 let kept = [resume]
140 let first = first_of kept True
141 let between = one_shots 2 0
142 let second = first_of kept False
143 list.append (list.append first [between]) second
144 }
145 , return n -> [n]
146 }
147}
148
149// The copied first resume leaves the original capture Saved. Park this actor
150// with that capture live; a peer then completes fresh handlers before waking
151// it. In particular, generic actor hibernation is not being mistaken for
152// coverage of a parked, saved handler. The resume closure stays local: only
153// subjects and unit/int messages cross actor boundaries. Each peer creates
154// and offers its own `ready` subject, whose owner is its only receiver.
155fn saved_while_parked ::
156 ready: Subject<()>
157 wake: Subject<Int>
158 -> List<Int> / <proc>
159{
160 handle
161 {
162 if choose ()
163 {
164 10
165 }
166 else
167 {
168 20
169 }
170 }
171 with
172 { choose ->
173 {
174 let kept = [resume]
175 let first = first_of kept True
176 process.send ready ()
177 // Park the coordinating actor itself (main), not just a handler's
178 // parent, while the copied resume leaves its original capture Saved.
179 let between = process.receive_forever wake
180 let second = first_of kept False
181 list.append (list.append first [between]) second
182 }
183 , return n -> [n]
184 }
185}
186
187fn parked_capture_rounds ::
188 left: Int
189 offers: Subject<Subject<()>>
190 wake: Subject<Int>
191 total: Int
192 -> Int / <proc>
193{
194 if left == 0
195 {
196 total
197 }
198 else
199 {
200 let _ =
201 process.spawn
202 fn
203 {
204 let ready = process.subject ()
205 process.send offers ready
206 process.receive_forever ready
207 // Both actors park here. LOUISE_PARKED_EPOCH_MS=0 therefore hibernates
208 // the actor whose multi-shot capture is Saved before the fresh handlers.
209 process.sleep 1
210 let between = one_shots 64 0
211 process.send wake between
212 }
213 let ready = process.receive_forever offers
214 let answer = list.sum (saved_while_parked ready wake)
215 parked_capture_rounds (left - 1) offers wake (total + answer)
216 }
217}
218
219type Escaped { Done(Int), Later(fn(Bool) -> Escaped / <io>) }
220
221// Escaping the closure retains the frame after final finish, not the right
222// to restore its old stack. Later handlers are free to reuse that mapping.
223fn escape_multi :: -> Escaped / <io>
224{
225 handle
226 {
227 defer io.println "escaped capture cancelled"
228 Done(if choose () { 11 } else { 22 })
229 }
230 with
231 { choose -> Later(fn value { resume value }) }
232}
233
234pub fn main :: -> () / <io, proc>
235{
236 io.println ("one-shot total " <> show (one_shots 1024 0))
237 io.println
238 ("cancelled " <> show (cancel_once ()) <> ", " <> show (cancel_once ()))
239 io.println ("returned " <> show (return_from_once ()))
240 io.println ("multi-shot total " <> show (many_multi 128 0))
241 io.println ("cancelled multi " <> show (cancel_multi ()))
242 io.println ("saved between " <> show (saved_with_handlers_between ()))
243 let offers = process.subject ()
244 let wake = process.subject ()
245 io.println
246 ("parked captures " <> show (parked_capture_rounds 4 offers wake 0))
247 match escape_multi ()
248 { Done(n) -> io.println ("unexpected " <> show n)
249 , Later(k) ->
250 {
251 io.println ("after final finish " <> show (one_shots 64 0))
252 match k True
253 { Done(n) -> io.println ("stale resume ran " <> show n)
254 , Later(_) -> io.println "stale resume returned a closure"
255 }
256 }
257 }
258}
259//> one-shot total 41984
260//> one-shot cancelled
261//> one-shot cancelled
262//> cancelled 5, 5
263//> returned 42
264//> multi-shot total 3840
265//> multi-shot cancelled
266//> cancelled multi 9
267//> saved between [10, 82, 20]
268//> parked captures 10616
269//> escaped capture cancelled
270//> after final finish 2624
271//! panic: `resume` called after its handler finished