- program_parallel_map: four exclusive loans of disjoint quarters - program_channel_transfer: heap-owned messages moved through a bounded channel; the 64-byte budget fits exactly the bounded worst case - program_tcp_echo_tasks: server task, one echo task per connection, two client tasks sharing address and message by shared loans - runner --stress N: seeds 0..N-1 under quanta 1, 2, 7 and the test's own; failures print the replay command; CI runs --stress 200 - plan: P3-12, P3-13 done
82 lines
2.4 KiB
LLVM
82 lines
2.4 KiB
LLVM
; Phase 3 exit program: ownership transfer through a bounded channel. The
|
||
; producer allocates each message on the heap and moves the owner into the
|
||
; channel; the consumer takes it. The budget (64 bytes) only fits the channel
|
||
; (2 × 8) plus three live messages (3 × 16): bounded capacity bounds memory.
|
||
; Result: 10 + 20 + 30 = 60.
|
||
; expect: run
|
||
; grant: heap=mem.alloc:64
|
||
; schedules: 30
|
||
; quantum: 2
|
||
; result: 60
|
||
; phase: 3
|
||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||
module test.program_channel_transfer
|
||
air 0
|
||
lang 0.1
|
||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||
|
||
type @Message = struct { id: u64, size: u64 }
|
||
|
||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||
effects [mem.alloc, task.spawn, task.wait]
|
||
entry:
|
||
%cap = const.usize 2
|
||
channel.make own<@Message>, %cap, %heap, ok -> @made, err -> @oom
|
||
made(%tx: sender<own<@Message>>, %rx: receiver<own<@Message>>):
|
||
%sc = task.scope.begin
|
||
%p = task.spawn %sc, @producer(%tx, %heap)
|
||
%z = const.u64 0
|
||
br @recv_loop(%rx, %z)
|
||
recv_loop(%r: receiver<own<@Message>>, %acc: u64):
|
||
%rb = borrow.mut %r
|
||
channel.recv %rb, ok -> @got, closed -> @drained, cancelled -> @drained
|
||
got(%o: own<@Message>):
|
||
borrow.end %rb
|
||
%msg = own.take %o
|
||
%id = struct.get @Message.id, %msg
|
||
%acc2 = add.checked.u64 %acc, %id
|
||
br @recv_loop(%r, %acc2)
|
||
drained:
|
||
borrow.end %rb
|
||
channel.close_receiver %r, ok -> @closed, pending -> @pending
|
||
pending(%r2: receiver<own<@Message>>):
|
||
br @recv_loop(%r2, %acc)
|
||
closed:
|
||
%x = task.join %p
|
||
task.scope.end %sc
|
||
ret %acc
|
||
oom(%e: alloc_error):
|
||
trap
|
||
|
||
fn @producer(%tx: sender<own<@Message>>, %heap: cap<mem.alloc>) -> u8
|
||
effects [mem.alloc, task.wait]
|
||
entry:
|
||
%txb = borrow.mut %tx
|
||
%ten = const.u64 10
|
||
br @loop(%ten)
|
||
loop(%id: u64):
|
||
%end = const.u64 40
|
||
%done = eq.u64 %id, %end
|
||
cbr %done, @finish(), @build(%id)
|
||
build(%i: u64):
|
||
%size = const.u64 16
|
||
%msg = struct.make @Message(%i, %size)
|
||
own.new %msg, %heap, ok -> @owned, err -> @oom
|
||
owned(%o: own<@Message>):
|
||
channel.send %txb, %o, ok -> @sent, closed -> @refused, cancelled -> @refused
|
||
sent:
|
||
; %o is gone: the receiver owns the message now
|
||
%step = const.u64 10
|
||
%next = add.checked.u64 %i, %step
|
||
br @loop(%next)
|
||
refused(%back: own<@Message>):
|
||
own.free %back
|
||
br @finish()
|
||
finish:
|
||
borrow.end %txb
|
||
channel.close_sender %tx
|
||
%r = const.u8 0
|
||
ret %r
|
||
oom(%e: alloc_error):
|
||
trap
|