// Calls with more than six arguments: those after the sixth go through // `ctx->xargs`, the running scheduler thread's slots (ABI 3, ADR 0028). // 32 actors make such calls at once, are preempted at function entries // and may move between threads: each callee must read its own arguments. // Covers a recursive function, a closure with captures (its entry passes // the arguments on), and a `ctl` operation (the runtime calls its clause). import std/io import std/process.{type Subject, type proc} effect mix { ctl mix7 :: a: Int b: Int c: Int d: Int e: Int f: Int g: Int -> Int } fn weigh :: a: Int b: Int c: Int d: Int e: Int f: Int g: Int h: Int i: Int -> Int { a + 2 * b + 3 * c + 5 * d + 7 * e + 11 * f + 13 * g + 17 * h + 19 * i } // Nine parameters, recursing: a function entry (and a preemption check) // on each step. fn walk :: n: Int a: Int b: Int c: Int d: Int e: Int f: Int g: Int total: Int -> Int { if n == 0 { total } else { walk (n - 1) b c d e f g a ((total + weigh n a b c d e f g total) % 1000003) } } fn mixing :: k: Int n: Int total: Int -> Int / { if n == 0 { total } else { mixing k (n - 1) ((total + mix7 n k (n + 1) (k + 1) (n + 2) (k + 2) (n + 3)) % 1000003) } } fn worker :: k: Int results: Subject -> () / { let scale = k * 7 let shift = k + 3 let add = fn a: Int b: Int c: Int d: Int e: Int f: Int g: Int { a + b + c + d + e + f + g + scale * shift } let walked = walk 20000 k 1 2 3 4 5 6 0 let mixed = handle { mixing k 5000 0 } with { mix7 a b c d e f g -> resume (add a b c d e f g) } process.send results (walked + mixed) } fn spawn_all :: k: Int results: Subject -> () / { if k > 0 { let _ = process.spawn fn { worker k results } spawn_all (k - 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 results = process.subject () spawn_all 32 results io.println (show (collect results 32 0)) } //> 33740715