// An 80-actor rendezvous holds and then returns enough one-shot handlers to // fill the real pool; stack-pool churn then runs across eight actors. Both // one-shot and multi-shot handlers complete and cancel repeatedly, with copied // and last-use resumes; fresh one-shot handlers also run while a multi-shot // capture is saved. Scheduling/migration order cannot change the output. import std/io import std/list import std/process.{type Subject, type proc} effect ask { ctl ask :: -> Int } effect amb { multi ctl choose :: -> Bool } fn one_shots :: left: Int total: Int -> Int { if left == 0 { total } else { let answer = handle { ask () + 1 } with { ask -> resume 40 } one_shots (left - 1) (total + answer) } } fn cancel_once :: -> Int { handle { ask () } with { ask -> 5 } } fn cancel_multi :: -> Int { handle { if choose () { 1 } else { 2 } } with { choose -> 9 } } fn choose_both :: -> List { handle { if choose () { 10 } else { 20 } } with { choose -> list.append (resume True) (resume False), return n -> [n] } } fn first_of :: ks: List List / > value: Bool -> List / { match ks { [k, ..] -> k value, [] -> [] } } fn saved_with_handlers_between :: -> List { handle { if choose () { 10 } else { 20 } } with { choose -> { let kept = [resume] let first = first_of kept True let between = one_shots 2 0 let second = first_of kept False list.append (list.append first [between]) second } , return n -> [n] } } // Hold every mapping live at a real ctl suspension before releasing any // peer. Each child owns and receives from its own release subject. 80 one-shot // completions fill the 64-entry pool on one Shared even though the aging/churn // workload below still uses only eight actors. fn hold_mapping :: offers: Subject> -> Int / { handle { ask () + 1 } with { ask -> { let release = process.subject () process.send offers release process.receive_forever release resume 40 } } } fn work :: left: Int total: Int -> Int { if left == 0 { total } else { let total = total + one_shots 64 0 + cancel_once () + cancel_multi () let total = total + list.sum (choose_both ()) let total = total + list.sum (saved_with_handlers_between ()) work (left - 1) total } } fn spawn_holders :: left: Int offers: Subject> done: Subject -> () / { if left > 0 { let _ = process.spawn fn { process.send done (hold_mapping offers) } spawn_holders (left - 1) offers done } } fn release_holders :: releases: List> -> () / { match releases { [release, ..rest] -> { process.send release () release_holders rest } , [] -> () } } fn collect_releases :: offers: Subject> left: Int releases: List> -> List> / { if left == 0 { releases } else { let release = process.receive_forever offers collect_releases offers (left - 1) [release, ..releases] } } fn spawn_workers :: left: Int results: Subject -> () / { if left > 0 { let _ = process.spawn fn { process.send results (work 128 0) } spawn_workers (left - 1) results } } fn collect :: results: Subject left: Int total: Int -> Int / { if left == 0 { total } else { collect results (left - 1) (total + process.receive_forever results) } } pub fn main :: -> () / { let holders = 80 let offers = process.subject () let held = process.subject () spawn_holders holders offers held // Every holder has suspended its handler before the first release can let // one finish; the done messages also put every return before the churn. let ready = collect_releases offers holders [] io.println ("rendezvous " <> show (list.length ready)) release_holders ready io.println ("held total " <> show (collect held holders 0)) let workers = 8 let results = process.subject () spawn_workers workers results io.println ("checksum " <> show (collect results workers 0)) } //> rendezvous 80 //> held total 3280 //> checksum 2846720