; Two producers share one channel through channel.sender.dup; the receiver ; sees closed only after both senders are closed. ; expect: run ; grant: heap=mem.alloc:64 ; schedules: 20 ; quantum: 2 ; result: 20 ; phase: 3 ; spec: docs/spec/04-concurrency-and-atomics.md module test.channels_mpsc_dup air 0 lang 0.1 features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks] fn @main(%heap: cap) -> u64 effects [mem.alloc, task.spawn, task.wait] entry: %one = const.usize 1 channel.make u64, %one, %heap, ok -> @made, err -> @oom made(%tx: sender, %rx: receiver): %txr = borrow.ref %tx %tx2 = channel.sender.dup %txr borrow.end %txr %sc = task.scope.begin %a = task.spawn %sc, @produce(%tx) %b = task.spawn %sc, @produce(%tx2) %z = const.u64 0 br @recv_loop(%rx, %z) recv_loop(%r: receiver, %acc: u64): %rb = borrow.mut %r channel.recv %rb, ok -> @got, closed -> @drained, cancelled -> @drained got(%v: u64): borrow.end %rb %acc2 = add.checked.u64 %acc, %v br @recv_loop(%r, %acc2) drained: borrow.end %rb channel.close_receiver %r, ok -> @closed, pending -> @pending pending(%r2: receiver): br @recv_loop(%r2, %acc) closed: %x = task.join %a %y = task.join %b task.scope.end %sc ret %acc oom(%e: alloc_error): trap ; sends 1, 2, 3, 4 fn @produce(%tx: sender) -> u8 effects [task.wait] entry: %txb = borrow.mut %tx %one = const.u64 1 br @loop(%one) loop(%i: u64): %five = const.u64 5 %done = eq.u64 %i, %five cbr %done, @finish(), @send(%i) send(%j: u64): channel.send %txb, %j, ok -> @sent, closed -> @gone, cancelled -> @gone sent: %step = const.u64 1 %k = add.checked.u64 %j, %step br @loop(%k) gone(%w: u64): br @finish() finish: borrow.end %txb channel.close_sender %tx %r = const.u8 0 ret %r