tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

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

230 lignes · 4384 octetsBrut · Historique
1// An 80-actor rendezvous holds and then returns enough one-shot handlers to
2// fill the real pool; stack-pool churn then runs across eight actors. Both
3// one-shot and multi-shot handlers complete and cancel repeatedly, with copied
4// and last-use resumes; fresh one-shot handlers also run while a multi-shot
5// capture is saved. Scheduling/migration order cannot change the output.
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
21fn one_shots :: left: Int total: Int -> Int
22{
23 if left == 0
24 {
25 total
26 }
27 else
28 {
29 let answer = handle { ask () + 1 } with { ask -> resume 40 }
30 one_shots (left - 1) (total + answer)
31 }
32}
33
34fn cancel_once :: -> Int
35{
36 handle
37 {
38 ask ()
39 }
40 with
41 { ask -> 5 }
42}
43
44fn cancel_multi :: -> Int
45{
46 handle
47 {
48 if choose ()
49 {
50 1
51 }
52 else
53 {
54 2
55 }
56 }
57 with
58 { choose -> 9 }
59}
60
61fn choose_both :: -> List<Int>
62{
63 handle
64 {
65 if choose ()
66 {
67 10
68 }
69 else
70 {
71 20
72 }
73 }
74 with
75 { choose -> list.append (resume True) (resume False), return n -> [n] }
76}
77
78fn first_of ::
79 ks: List<fn(Bool) -> List<Int> / <e>>
80 value: Bool
81 -> List<Int> / <e>
82{
83 match ks { [k, ..] -> k value, [] -> [] }
84}
85
86fn saved_with_handlers_between :: -> List<Int>
87{
88 handle
89 {
90 if choose ()
91 {
92 10
93 }
94 else
95 {
96 20
97 }
98 }
99 with
100 { choose ->
101 {
102 let kept = [resume]
103 let first = first_of kept True
104 let between = one_shots 2 0
105 let second = first_of kept False
106 list.append (list.append first [between]) second
107 }
108 , return n -> [n]
109 }
110}
111
112// Hold every mapping live at a real ctl suspension before releasing any
113// peer. Each child owns and receives from its own release subject. 80 one-shot
114// completions fill the 64-entry pool on one Shared even though the aging/churn
115// workload below still uses only eight actors.
116fn hold_mapping :: offers: Subject<Subject<()>> -> Int / <proc>
117{
118 handle
119 {
120 ask () + 1
121 }
122 with
123 { ask ->
124 {
125 let release = process.subject ()
126 process.send offers release
127 process.receive_forever release
128 resume 40
129 }
130 }
131}
132
133fn work :: left: Int total: Int -> Int
134{
135 if left == 0
136 {
137 total
138 }
139 else
140 {
141 let total = total + one_shots 64 0 + cancel_once () + cancel_multi ()
142 let total = total + list.sum (choose_both ())
143 let total = total + list.sum (saved_with_handlers_between ())
144 work (left - 1) total
145 }
146}
147
148fn spawn_holders ::
149 left: Int
150 offers: Subject<Subject<()>>
151 done: Subject<Int>
152 -> () / <proc>
153{
154 if left > 0
155 {
156 let _ = process.spawn fn { process.send done (hold_mapping offers) }
157 spawn_holders (left - 1) offers done
158 }
159}
160
161fn release_holders :: releases: List<Subject<()>> -> () / <proc>
162{
163 match releases
164 { [release, ..rest] ->
165 {
166 process.send release ()
167 release_holders rest
168 }
169 , [] -> ()
170 }
171}
172
173fn collect_releases ::
174 offers: Subject<Subject<()>>
175 left: Int
176 releases: List<Subject<()>>
177 -> List<Subject<()>> / <proc>
178{
179 if left == 0
180 {
181 releases
182 }
183 else
184 {
185 let release = process.receive_forever offers
186 collect_releases offers (left - 1) [release, ..releases]
187 }
188}
189
190fn spawn_workers :: left: Int results: Subject<Int> -> () / <proc>
191{
192 if left > 0
193 {
194 let _ = process.spawn fn { process.send results (work 128 0) }
195 spawn_workers (left - 1) results
196 }
197}
198
199fn collect :: results: Subject<Int> left: Int total: Int -> Int / <proc>
200{
201 if left == 0
202 {
203 total
204 }
205 else
206 {
207 collect results (left - 1) (total + process.receive_forever results)
208 }
209}
210
211pub fn main :: -> () / <io, proc>
212{
213 let holders = 80
214 let offers = process.subject ()
215 let held = process.subject ()
216 spawn_holders holders offers held
217 // Every holder has suspended its handler before the first release can let
218 // one finish; the done messages also put every return before the churn.
219 let ready = collect_releases offers holders []
220 io.println ("rendezvous " <> show (list.length ready))
221 release_holders ready
222 io.println ("held total " <> show (collect held holders 0))
223 let workers = 8
224 let results = process.subject ()
225 spawn_workers workers results
226 io.println ("checksum " <> show (collect results workers 0))
227}
228//> rendezvous 80
229//> held total 3280
230//> checksum 2846720