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.

116 lignes · 3301 octetsBrut · Historique
1// Messages that the sender keeps using: moved, copied or shared by the
2// native runtime (ADR 0002, Revision 1), invisible to the program.
3
4import std/io
5import std/list
6import std/process.{type Subject, type proc, Crashed, Down}
7import std/ref
8
9effect note
10{
11 fn note :: text: String -> ()
12}
13
14/// What `@extern` code can do: no type crosses it, so the compiler cannot
15/// check what is sent through it. The runtime still refuses it (ADR 0004,
16/// Revision 6: the backstop).
17@extern c "tl_coerce"
18fn unchecked :: x: a -> b
19
20type Job
21{ Sum(List<Int>, Subject<Int>), Both(List<Int>, List<Int>, Subject<Int>) }
22
23fn worker :: jobs: Subject<Subject<Job>> -> () / <proc, io>
24{
25 let inbox = process.subject ()
26 process.send jobs inbox
27 serve inbox
28}
29
30fn serve :: inbox: Subject<Job> -> () / <proc, io>
31{
32 match process.receive_forever inbox
33 { Sum(xs, reply) -> process.send reply (list.sum xs)
34 , Both(xs, ys, reply) -> process.send reply (list.sum xs + list.sum ys)
35 }
36 serve inbox
37}
38
39pub fn main :: -> () / <io, proc>
40{
41 let jobs = process.subject ()
42 let _ = process.spawn fn { worker jobs }
43 let inbox = process.receive_forever jobs
44 let reply = process.subject ()
45
46 // Small, kept by the sender: copied.
47 let small = [1, 2, 3]
48 process.send inbox Sum(small, reply)
49 io.println
50 ( "small " <> show (process.receive_forever reply) <> " "
51 <> show (list.sum small)
52 )
53
54 // Large, kept by the sender: shared.
55 let large = list.range 1 1000
56 process.send inbox Sum(large, reply)
57 io.println
58 ( "large " <> show (process.receive_forever reply) <> " "
59 <> show (list.length large)
60 )
61
62 // The same list twice in one message, and again in a later one.
63 process.send inbox Both(small, small, reply)
64 io.println ("twice " <> show (process.receive_forever reply))
65 process.send inbox Both(large, small, reply)
66 io.println ("mixed " <> show (process.receive_forever reply))
67
68 // Not kept: moved.
69 process.send inbox Sum(list.range 1 10, reply)
70 io.println ("moved " <> show (process.receive_forever reply))
71
72 // A spawned actor's closure is sent too.
73 let done = process.subject ()
74 let _ =
75 process.spawn fn { process.send done (list.sum large + list.sum small) }
76 io.println ("captured " <> show (process.receive_forever done))
77
78 // A closure capturing a mutable reference cannot leave its actor: the
79 // compiler refuses to send one (tests/ui/send_closures.lse), and when
80 // `@extern` code hides it, the sender crashes.
81 let (_, down) =
82 {
83 use <- process.spawn_monitor
84 region
85 {
86 let counter = ref.new 1
87 let mine = process.subject ()
88 process.send mine (unchecked fn { ref.get counter })
89 }
90 }
91 let Down(_, reason) = process.receive_forever down
92 io.println (show reason)
93
94 // Nor can a closure using a handler of this actor.
95 let (_, down) =
96 {
97 use <- process.spawn_monitor
98 handle
99 {
100 let mine = process.subject ()
101 process.send mine (unchecked fn { note "hello" })
102 }
103 with
104 { note text -> () }
105 }
106 let Down(_, reason) = process.receive_forever down
107 io.println (show reason)
108}
109//> small 6 6
110//> large 500500 1000
111//> twice 12
112//> mixed 500506
113//> moved 55
114//> captured 500506
115//> Crashed(cannot send a region or a handler)
116//> Crashed(cannot send a region or a handler)