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.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Message passing: a ring of actors passing a token, on all threads. |
| 2 | |
| 3 | import std/io |
| 4 | import std/process.{type Subject, type proc} |
| 5 | |
| 6 | fn relay :: inbox: Subject<Int> next: Subject<Int> -> () / <proc> |
| 7 | { |
| 8 | let token = process.receive_forever inbox |
| 9 | process.send next (token + 1) |
| 10 | relay inbox next |
| 11 | } |
| 12 | |
| 13 | fn laps :: |
| 14 | start: Subject<Int> |
| 15 | back: Subject<Int> |
| 16 | n: Int |
| 17 | token: Int |
| 18 | -> Int / <proc> |
| 19 | { |
| 20 | if n == 0 |
| 21 | { |
| 22 | token |
| 23 | } |
| 24 | else |
| 25 | { |
| 26 | process.send start token |
| 27 | laps start back (n - 1) (process.receive_forever back) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | pub fn main :: -> () / <io, proc> |
| 32 | { |
| 33 | let back = process.subject () |
| 34 | let ready = process.subject () |
| 35 | let _ = |
| 36 | process.spawn |
| 37 | fn |
| 38 | { |
| 39 | let inbox = process.subject () |
| 40 | process.send ready inbox |
| 41 | relay inbox back |
| 42 | } |
| 43 | let last = process.receive_forever ready |
| 44 | // The ring is built backwards: each new actor forwards to the previous |
| 45 | // one, and the first to `back`. Relays run until `main` stops. |
| 46 | let start = ring_from 999 last |
| 47 | io.println (show (laps start back 200 0)) |
| 48 | } |
| 49 | |
| 50 | fn ring_from :: size: Int next: Subject<Int> -> Subject<Int> / <proc> |
| 51 | { |
| 52 | if size == 0 |
| 53 | { |
| 54 | next |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | let ready = process.subject () |
| 59 | let _ = |
| 60 | process.spawn |
| 61 | fn |
| 62 | { |
| 63 | let inbox = process.subject () |
| 64 | process.send ready inbox |
| 65 | relay inbox next |
| 66 | } |
| 67 | ring_from (size - 1) (process.receive_forever ready) |
| 68 | } |
| 69 | } |
| 70 | //> 200000 |