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.

71 lignes · 1554 octetsBrut · Historique
1// 100k actors: each is spawned linked to `main`, which traps exits; every
2// tenth one crashes. The output does not depend on the order in which
3// actors run, so it is checked with several scheduler threads.
4
5import std/io
6import std/process.
7{type ExitSignal, type Subject, type proc, ExitSignal, Normal}
8
9fn worker :: n: Int results: Subject<Int> -> () / <proc>
10{
11 if n % 10 == 0
12 {
13 panic "on purpose"
14 }
15 process.send results n
16}
17
18fn spawn_all :: n: Int results: Subject<Int> -> () / <proc>
19{
20 if n > 0
21 {
22 let _ = process.spawn_link fn { worker n results }
23 spawn_all (n - 1) results
24 }
25}
26
27fn sum :: results: Subject<Int> left: Int total: Int -> Int / <proc>
28{
29 if left == 0
30 {
31 total
32 }
33 else
34 {
35 sum results (left - 1) (total + process.receive_forever results)
36 }
37}
38
39fn count ::
40 exits: Subject<ExitSignal>
41 left: Int
42 normal: Int
43 crashed: Int
44 -> (Int, Int) / <proc>
45{
46 if left == 0
47 {
48 (normal, crashed)
49 }
50 else
51 {
52 match process.receive_forever exits
53 { ExitSignal(_, Normal) -> count exits (left - 1) (normal + 1) crashed
54 , ExitSignal(_, _) -> count exits (left - 1) normal (crashed + 1)
55 }
56 }
57}
58
59pub fn main :: -> () / <io, proc>
60{
61 let n = 100000
62 process.trap_exits True
63 let exits = process.exits ()
64 let results = process.subject ()
65 spawn_all n results
66 io.println ("sum " <> show (sum results (n - n / 10) 0))
67 let counts = count exits n 0 0
68 io.println ("normal " <> show counts.0 <> ", crashed " <> show counts.1)
69}
70//> sum 4500000000
71//> normal 90000, crashed 10000