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.

78 lignes · 2544 octetsBrut · Historique
1// Every actor gets the runtime's `clock` and `random` handlers; a handler
2// around `spawn` doesn't follow the new actor (ADR 0036).
3
4import std/io
5import std/process.{type Subject, type proc}
6import std/random.{type random}
7import std/string
8import std/time.{type clock}
9
10/// Uses each of an actor's capabilities, and reports to `to`.
11fn report :: to: Subject<String> how: String -> () / <proc, clock, random>
12{
13 let start = time.monotonic ()
14 process.sleep 2
15 let slept = time.elapsed (time.monotonic ()) start
16 process.send
17 to
18 "{how}: {slept} {string.length (random.token 3)} {time.unix_ms (time.now ()) > 0}"
19}
20
21pub fn main :: -> () / <io, proc, clock, random>
22{
23 let clocks = process.subject ()
24 time.with_fixed_clock
25 (time.from_unix_ms 0)
26 fn
27 {
28 io.println "caller: {time.now ()}"
29 let _ =
30 process.spawn
31 fn { process.send clocks (time.unix_ms (time.now ()) > 0) }
32 ()
33 }
34 io.println "actor reads the system clock: {process.receive clocks 1000}"
35
36 let tokens = process.subject ()
37 let seeded =
38 random.with_seed
39 1
40 fn
41 {
42 let _ = process.spawn fn { process.send tokens (random.token 16) }
43 let _ = process.spawn fn { process.send tokens (random.token 16) }
44 random.token 4
45 }
46 let a = process.receive tokens 1000
47 let b = process.receive tokens 1000
48 io.println "caller: {seeded}; actors read the system's randomness: {a != b}"
49
50 // The scheduler's clock, in an actor: virtual, so exactly what it slept.
51 let durations = process.subject ()
52 let _ =
53 process.spawn
54 fn
55 {
56 let start = time.monotonic ()
57 process.sleep 7
58 process.send durations (time.elapsed (time.monotonic ()) start)
59 }
60 io.println "actor slept {process.receive durations 1000}"
61
62 // The three ways to spawn give a body the same capabilities.
63 let reports = process.subject ()
64 let _ = process.spawn fn { report reports "spawn" }
65 io.println "{process.receive reports 1000}"
66 let _ = process.spawn_link fn { report reports "spawn_link" }
67 io.println "{process.receive reports 1000}"
68 let (_, down) = process.spawn_monitor fn { report reports "spawn_monitor" }
69 io.println "{process.receive reports 1000}"
70 let _ = process.receive down 1000
71}
72//> caller: 1970-01-01T00:00:00Z
73//> actor reads the system clock: Some(True)
74//> caller: c15c0289; actors read the system's randomness: True
75//> actor slept Some(7ms)
76//> Some(spawn: 2ms 6 True)
77//> Some(spawn_link: 2ms 6 True)
78//> Some(spawn_monitor: 2ms 6 True)