Concurrency.pool.worker: def of bend-kit-concurrency@0.2.0.0 Import: import bend-kit-concurrency@0.2.0.0/concurrency.bend as Concurrency Lines 150-177 of bend-kit-concurrency@0.2.0.0/concurrency.bend: https://jend.leeeet.dev/read.txt?path=bend-kit-concurrency@0.2.0.0/concurrency.bend&start_line=150&end_line=177 import Base # Takes jobs until the queue is closed and empty, then answers its state. # Each job takes three steps, so 3 * (jobs + 1) fuel is enough. def pool.worker( ~S: Type, ~A: Type, ~B: Type, ~work: S -> A -> IO(S & B), fuel: Nat, +q: Chan(A & Chan(B)), w: Worker ) -> IO(S): match fuel: case 0n: IO.die(S, 1, "Conc.pool: a worker ran out of fuel") case 1n+p: match w: case WIdle{s}: do IO: got : Maybe<&1, A & Chan(B)> <- Chan.recv(A & Chan(B), q) pool.worker(~S, ~A, ~B, ~work, p, q, WGot{s, got}) case WGot{s, got}: match got: case None{}: IO.pure(S, s) case Some{(a, reply)}: do IO: sb : S & B <- work(s, a) pool.worker(~S, ~A, ~B, ~work, p, q, WDone{sb, reply}) case WDone{sb, reply}: (s, b) = sb do IO: sent : Result<&1, &1, B, Unit> <- Chan.send(B, reply, b) pool.worker(~S, ~A, ~B, ~work, p, q, WIdle{s}) Uses (14): https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Chan https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Chan.recv https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Chan.send https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=IO https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=IO.bind https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=IO.die https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=IO.pure https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Maybe https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Nat https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Pair https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Result https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Sigma https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=Unit https://jend.leeeet.dev/definition.txt?path=bend-kit-concurrency@0.2.0.0/concurrency.bend&name=Worker