| @@ -0,0 +1,87 @@ |
| 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 | +import std/io |
| 7 | +import std/process.{type proc, type Subject} |
| 8 | + |
| 9 | +fn server(inbox: Subject<(Int, Subject<Int>)>, n: Int) -> () / <proc> { |
| 10 | + if n > 0 { |
| 11 | + match process.receive(inbox, 60000) { |
| 12 | + Some((x, reply)) -> { |
| 13 | + process.send(reply, x + 1) |
| 14 | + server(inbox, n - 1) |
| 15 | + }, |
| 16 | + None -> panic("server: timed out"), |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +fn client(server: Subject<(Int, Subject<Int>)>, reply: Subject<Int>, n: Int, acc: Int) -> Int / <proc> { |
| 22 | + if n == 0 { |
| 23 | + acc |
| 24 | + } else { |
| 25 | + process.send(server, (n, reply)) |
| 26 | + match process.receive(reply, 60000) { |
| 27 | + Some(x) -> client(server, reply, n - 1, acc + x), |
| 28 | + None -> panic("client: timed out"), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +fn sleeper(n: Int) -> Int / <proc> { |
| 34 | + if n == 0 { |
| 35 | + 0 |
| 36 | + } else { |
| 37 | + process.sleep(1) |
| 38 | + 1 + sleeper(n - 1) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +fn waiter(quiet: Subject<Int>, n: Int) -> Int / <proc> { |
| 43 | + if n == 0 { |
| 44 | + 0 |
| 45 | + } else { |
| 46 | + match process.receive(quiet, 1) { |
| 47 | + Some(_) -> panic("waiter: a message"), |
| 48 | + None -> 1 + waiter(quiet, n - 1), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn spawn_pairs(n: Int, done: Subject<Int>) -> () / <proc> { |
| 54 | + if n > 0 { |
| 55 | + let _ = process.spawn(fn() { |
| 56 | + let ready = process.subject() |
| 57 | + let _ = process.spawn(fn() { |
| 58 | + let inbox = process.subject() |
| 59 | + process.send(ready, inbox) |
| 60 | + server(inbox, 100) |
| 61 | + }) |
| 62 | + let inbox = process.receive_forever(ready) |
| 63 | + process.send(done, client(inbox, process.subject(), 100, 0)) |
| 64 | + }) |
| 65 | + spawn_pairs(n - 1, done) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn spawn_sleepers(n: Int, done: Subject<Int>) -> () / <proc> { |
| 70 | + if n > 0 { |
| 71 | + let _ = process.spawn(fn() { process.send(done, sleeper(50)) }) |
| 72 | + let _ = process.spawn(fn() { process.send(done, waiter(process.subject(), 50)) }) |
| 73 | + spawn_sleepers(n - 1, done) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn sum(done: Subject<Int>, left: Int, total: Int) -> Int / <proc> { |
| 78 | + if left == 0 { total } else { sum(done, left - 1, total + process.receive_forever(done)) } |
| 79 | +} |
| 80 | + |
| 81 | +pub fn main() -> () / <io, proc> { |
| 82 | + let done = process.subject() |
| 83 | + spawn_pairs(1000, done) |
| 84 | + spawn_sleepers(1000, done) |
| 85 | + io.println(show(sum(done, 3000, 0))) |
| 86 | +} |
| 87 | +//> 5250000 |