// Reference program (Phase 4 exit): island pipeline. A producer task builds a cyclic // graph in an island and moves it through a channel (O(1), no copy). The consumer // freezes it, lends the frozen graph to two readers at once, turns it into an arc, // sends a clone to a late reader, and releases every handle; the last release frees // the graph. Each reader walks the ring 6 steps: 2 × (1+2+3+1+2+3) + 12 = 36. // expect: run // grant: heap=mem.alloc:1024 // schedules: 30 // quantum: 3 // result: 36 // equivalent: phase4/run/program_island_pipeline.air // phase: 6 // spec: docs/spec/03-memory-model.md module demo.islandpipeline struct Node { value: u64, next: Link, } enum Link { End, To(iref), } // Builds the ring 1 → 2 → 3 → 1 and moves it into the channel. fn produce(tx: sender>, heap: cap) -> u8 effects [mem.alloc, task.wait] { let isl = island.create(Node { value: 1, next: Link::End }, heap) or trap; { mut g = isl; let a = island.root(g); let c = island.alloc(g, Node { value: 3, next: Link::To(a) }) or trap; let b = island.alloc(g, Node { value: 2, next: Link::To(c) }) or trap; mut first = island.at(g, a); first.next = Link::To(b); } match channel.send(tx, move isl) { ok => {} closed(back) => { island.destroy(move back); } cancelled(back) => { island.destroy(move back); } } channel.close_sender(move tx); 0 } // Sums `steps` nodes along the ring, through a shared borrow of the frozen graph. fn walk(f: ref>, steps: u64) -> u64 effects [] { var sum: u64 = 0; var at: iref = frozen.root(f); for i in 0..steps { ref node = frozen.at(f, at); sum = sum + node.value; at = match node.next { Link::End => at, Link::To(n) => n, }; } sum } // A late reader that owns its own arc handle and releases it. fn late(rx: receiver>) -> u64 effects [task.wait] { var sum: u64 = 0; match channel.recv(rx) { ok(a) => { { ref ar = a; var at: iref = arc.root(ar); for i in 0..6u64 { ref node = arc.at(ar, at); sum = sum + node.value * 2; at = match node.next { Link::End => at, Link::To(n) => n, }; } } arc.release(move a); } closed => {} cancelled => {} } channel.close_receiver(move rx) or trap; sum / 2 } fn main(heap: cap) -> u64 effects [mem.alloc, task.spawn, task.wait] { var total: u64 = 0; match channel.make>(1, heap) { ok(tx, rx) => { scope sc { let p = task.spawn(sc, produce, move tx, heap); match channel.recv(rx) { ok(isl) => { let f = island.freeze(move isl); { ref fr = f; scope readers { let r1 = task.spawn(readers, walk, fr, 6); let r2 = task.spawn(readers, walk, fr, 6); total = task.join(move r1) + task.join(move r2); } } let a = arc.from_frozen(move f); match channel.make>(1, heap) { ok(atx, arx) => { scope lates { let l = task.spawn(lates, late, move arx); let b = arc.clone(a); match channel.send(atx, move b) { ok => {} closed(back) => { arc.release(move back); } cancelled(back) => { arc.release(move back); } } channel.close_sender(move atx); total = total + task.join(move l); } } err(e) => {} } arc.release(move a); } closed => {} cancelled => {} } channel.close_receiver(move rx) or trap; task.join(move p); } } err(e) => { trap; } } total }