// Arc handles: the frozen graph becomes shared; each worker gets its own handle through // a channel and releases it; the last release frees the graph. // expect: run // grant: heap=mem.alloc:512 // schedules: 20 // result: 20 // phase: 6 // spec: docs/spec/03-memory-model.md module demo.arcshared fn reader(rx: receiver>) -> u64 effects [task.wait] { var sum: u64 = 0; loop { match channel.recv(rx) { ok(a) => { { ref ar = a; let r = arc.root(ar); ref v = arc.at(ar, r); sum = sum + *v; } arc.release(move a); } closed => { break; } cancelled => { break; } } } channel.close_receiver(move rx) or trap; sum } fn main(heap: cap) -> u64 effects [mem.alloc, task.spawn, task.wait] { let isl = island.create(10u64, heap) or trap; let a = arc.from_frozen(island.freeze(move isl)); match channel.make>(2, heap) { ok(tx, rx) => { var r: u64 = 0; scope sc { let t = task.spawn(sc, reader, move rx); let b = arc.clone(a); channel.send(tx, move b) or trap; channel.send(tx, move a) or trap; channel.close_sender(move tx); r = task.join(move t); } r } err(e) => { arc.release(move a); trap; } } }