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.

126 lignes · 2517 octetsBrut · Historique
1// Timers: actors that wait with timeouts, on all threads.
2// - 1,000 client/server pairs make 100 calls each, both sides receiving
3// with a timeout that never expires (a `call` with a timeout);
4// - 1,000 actors sleep 1 ms, 50 times each;
5// - 1,000 actors wait 1 ms for a message that never comes, 50 times each.
6
7import std/io
8import std/process.{type Subject, type proc}
9
10fn server :: inbox: Subject<(Int, Subject<Int>)> n: Int -> () / <proc>
11{
12 if n > 0
13 {
14 match process.receive inbox 60000
15 { Some((x, reply)) ->
16 {
17 process.send reply (x + 1)
18 server inbox (n - 1)
19 }
20 , None -> panic "server: timed out"
21 }
22 }
23}
24
25fn client ::
26 server: Subject<(Int, Subject<Int>)>
27 reply: Subject<Int>
28 n: Int
29 acc: Int
30 -> Int / <proc>
31{
32 if n == 0
33 {
34 acc
35 }
36 else
37 {
38 process.send server (n, reply)
39 match process.receive reply 60000
40 { Some(x) -> client server reply (n - 1) (acc + x)
41 , None -> panic "client: timed out"
42 }
43 }
44}
45
46fn sleeper :: n: Int -> Int / <proc>
47{
48 if n == 0
49 {
50 0
51 }
52 else
53 {
54 process.sleep 1
55 1 + sleeper (n - 1)
56 }
57}
58
59fn waiter :: quiet: Subject<Int> n: Int -> Int / <proc>
60{
61 if n == 0
62 {
63 0
64 }
65 else
66 {
67 match process.receive quiet 1
68 { Some(_) -> panic "waiter: a message", None -> 1 + waiter quiet (n - 1) }
69 }
70}
71
72fn spawn_pairs :: n: Int done: Subject<Int> -> () / <proc>
73{
74 if n > 0
75 {
76 let _ =
77 process.spawn
78 fn
79 {
80 let ready = process.subject ()
81 let _ =
82 process.spawn
83 fn
84 {
85 let inbox = process.subject ()
86 process.send ready inbox
87 server inbox 100
88 }
89 let inbox = process.receive_forever ready
90 process.send done (client inbox (process.subject ()) 100 0)
91 }
92 spawn_pairs (n - 1) done
93 }
94}
95
96fn spawn_sleepers :: n: Int done: Subject<Int> -> () / <proc>
97{
98 if n > 0
99 {
100 let _ = process.spawn fn { process.send done (sleeper 50) }
101 let _ =
102 process.spawn fn { process.send done (waiter (process.subject ()) 50) }
103 spawn_sleepers (n - 1) done
104 }
105}
106
107fn sum :: done: Subject<Int> left: Int total: Int -> Int / <proc>
108{
109 if left == 0
110 {
111 total
112 }
113 else
114 {
115 sum done (left - 1) (total + process.receive_forever done)
116 }
117}
118
119pub fn main :: -> () / <io, proc>
120{
121 let done = process.subject ()
122 spawn_pairs 1000 done
123 spawn_sleepers 1000 done
124 io.println (show (sum done 3000 0))
125}
126//> 5250000