; Phase 4 exit program (hand-written AIR): island pipeline. A producer task builds a ; cyclic graph in an island and moves it through a channel; the consumer freezes it, ; lends it to two readers, turns it into an arc and sends a clone to a late reader. ; expect: run ; grant: heap=mem.alloc:1024 ; schedules: 30 ; quantum: 3 ; result: 36 ; phase: 4 ; spec: docs/spec/03-memory-model.md module test.island_pipeline air 0 lang 0.1 features [arc, cancellation, capabilities, channels, effects, heap, islands, ownership, resources, tasks] type @Node = struct { value: u64, next: @Link } type @Link = enum { End, To(iref<@Node>) } fn @main(%heap: cap) -> u64 effects [mem.alloc, task.spawn, task.wait] entry: %cap = const.usize 1 channel.make island<@Node>, %cap, %heap, ok -> @made, err -> @oom made(%tx: sender>, %rx: receiver>): %sc1 = task.scope.begin %prod = task.spawn %sc1, @producer(%tx, %heap) %rxb = borrow.mut %rx channel.recv %rxb, ok -> @got, closed -> @none, cancelled -> @none got(%i: island<@Node>): borrow.end %rxb ; the graph is complete and owned here: freeze it and share it with two readers %f = island.freeze %i %fb = borrow.ref %f %sc2 = task.scope.begin %six = const.u64 6 %w1 = task.spawn %sc2, @walk(%fb, %six) %w2 = task.spawn %sc2, @walk(%fb, %six) %s1 = task.join %w1 %s2 = task.join %w2 task.scope.end %sc2 borrow.end %fb %t1 = add.checked.u64 %s1, %s2 ; shared ownership from here on: one handle stays, a clone goes to the late reader %a = arc.from_frozen %f %cap2 = const.usize 1 channel.make arc<@Node>, %cap2, %heap, ok -> @made2, err -> @oom2 made2(%atx: sender>, %arx: receiver>): %sc3 = task.scope.begin %late = task.spawn %sc3, @late(%arx) %ab = borrow.ref %a %a2 = arc.clone %ab borrow.end %ab %atxb = borrow.mut %atx channel.send %atxb, %a2, ok -> @sent, closed -> @refused, cancelled -> @refused sent: br @after_send() refused(%back: arc<@Node>): arc.release %back br @after_send() after_send: borrow.end %atxb channel.close_sender %atx %s3 = task.join %late task.scope.end %sc3 %t2 = add.checked.u64 %t1, %s3 ; the last release frees the graph arc.release %a br @finish(%t2) none: borrow.end %rxb %z = const.u64 0 br @finish(%z) finish(%total: u64): channel.close_receiver %rx, ok -> @rx_closed, pending -> @rx_pending rx_pending(%rx2: receiver>): ; the producer sends one island and it was received: nothing can be buffered trap rx_closed: %pr = task.join %prod task.scope.end %sc1 ret %total oom(%e: alloc_error): trap oom2(%e2: alloc_error): trap fn @producer(%tx: sender>, %heap: cap) -> u8 effects [mem.alloc, task.wait] entry: %one = const.u64 1 %end = enum.make @Link.End() %root = struct.make @Node(%one, %end) island.create %root, %heap, ok -> @created, err -> @oom_create created(%i: island<@Node>): %ib = borrow.mut %i %r = island.root %ib %three = const.u64 3 %to_r = enum.make @Link.To(%r) %nc = struct.make @Node(%three, %to_r) island.alloc %ib, %nc, ok -> @have_c, err -> @oom_alloc_c have_c(%c: iref<@Node>): %two = const.u64 2 %to_c = enum.make @Link.To(%c) %nb = struct.make @Node(%two, %to_c) island.alloc %ib, %nb, ok -> @have_b, err -> @oom_alloc_b have_b(%b: iref<@Node>): ; close the ring: root.next = b, so 1 -> 2 -> 3 -> 1 %rp = island.borrow %ib, %r %nf = borrow.field @Node.next, %rp %to_b = enum.make @Link.To(%b) store %nf, %to_b borrow.end %nf borrow.end %rp borrow.end %ib %txb = borrow.mut %tx channel.send %txb, %i, ok -> @sent, closed -> @refused, cancelled -> @refused sent: ; %i is gone: the receiver owns the graph now borrow.end %txb br @finish() refused(%back: island<@Node>): borrow.end %txb island.destroy %back br @finish() finish: channel.close_sender %tx %z = const.u8 0 ret %z oom_create(%e: alloc_error): trap oom_alloc_c(%e2: alloc_error): trap oom_alloc_b(%e3: alloc_error): trap fn @walk(%f: ref>, %steps: u64) -> u64 effects [] entry: %r = frozen.root %f %z = const.u64 0 br @loop(%r, %z, %z) loop(%cur: iref<@Node>, %acc: u64, %k: u64): %done = eq.u64 %k, %steps cbr %done, @fin(), @step() step: %p = frozen.borrow %f, %cur %n = load %p borrow.end %p %v = struct.get @Node.value, %n %acc2 = add.checked.u64 %acc, %v %one = const.u64 1 %k2 = add.checked.u64 %k, %one %nx = struct.get @Node.next, %n enum.switch @Link %nx, End -> @stay, To -> @move stay: br @loop(%cur, %acc2, %k2) move(%to: iref<@Node>): br @loop(%to, %acc2, %k2) fin: ret %acc fn @late(%rx: receiver>) -> u64 effects [task.wait] entry: %rxb = borrow.mut %rx channel.recv %rxb, ok -> @got, closed -> @none, cancelled -> @none got(%a: arc<@Node>): borrow.end %rxb %ab = borrow.ref %a %r = arc.root %ab %z = const.u64 0 br @loop(%r, %z, %z) loop(%cur: iref<@Node>, %acc: u64, %k: u64): %six = const.u64 6 %done = eq.u64 %k, %six cbr %done, @walked(), @step() step: %p = arc.borrow %ab, %cur %n = load %p borrow.end %p %v = struct.get @Node.value, %n %acc2 = add.checked.u64 %acc, %v %one = const.u64 1 %k2 = add.checked.u64 %k, %one %nx = struct.get @Node.next, %n enum.switch @Link %nx, End -> @stay, To -> @move stay: br @loop(%cur, %acc2, %k2) move(%to: iref<@Node>): br @loop(%to, %acc2, %k2) walked: borrow.end %ab arc.release %a br @close(%acc) none: borrow.end %rxb %zero = const.u64 0 br @close(%zero) close(%sum: u64): channel.close_receiver %rx, ok -> @closed, pending -> @pending pending(%rx2: receiver>): trap closed: ret %sum