Phase 3: channels, mutex, select and cancellation points (RFC 0012)
- features cancellation, channels, sync supported - AIR model/parser/printer: shield, sender<T>, receiver<T>, mutex<T>, guard<T>; task.check_cancel, task.join.cancel, task.shield.*, channel.*, select (bracketed arms), mutex.* - verifier: arities (E5002), element rules (E4001/E1001, E0006 channel_transfer), guard_transfer gating, task.wait effect (gated for task.join by cancellation), per-edge consumption with values and handles returned as block parameters, loans moved to the handle on task.join.cancel's cancelled edge, shield order (E4305), guard children (E2102), critical-section rule (E4304) - interpreter: waiting-operation rule (ready, else cancelled if observable, else wait), shield depth, bounded and rendezvous channels with FIFO offers, drain-then-close receiver, budget release at the last close, select with seeded draw among ready arms, mutex wakeups; schedule record also after a select draw - report counts task.spawn, task.wait and channel allocations - spec erratum: mutex<T> is linear (format 0 has no droppable move-only class) - 30 new conformance tests (10 run with schedules, 4 accept, 16 reject); 6000-run schedule stress and 4350 mutants without failure
This commit is contained in:
1 parent
b82bc35c5f
commit
aaf32b6853
45 files changed
+2077
-43
No files matched your search
@@ -0,0 +1,17 @@
|
||||
; Properly nested shields: the inner one ends first.
|
||||
; expect: accept
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_shields_nested
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
%a = task.shield.begin
|
||||
%b = task.shield.begin
|
||||
task.shield.end %b
|
||||
task.shield.end %a
|
||||
ret
|
||||
@@ -0,0 +1,30 @@
|
||||
; A critical section may call functions that neither wait nor spawn, passing them the guarded mut<T>.
|
||||
; expect: accept
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_call_in_critical_section
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%a: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %a, ok -> @la, cancelled -> @out
|
||||
la(%g: guard<u64>):
|
||||
%p = mutex.borrow %g
|
||||
call @inc(%p)
|
||||
borrow.end %p
|
||||
mutex.unlock %g
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
|
||||
fn @inc(%p: mut<u64>) -> void
|
||||
effects []
|
||||
entry:
|
||||
%v = load %p
|
||||
%one = const.u64 1
|
||||
%w = add.wrap.u64 %v, %one
|
||||
store %p, %w
|
||||
ret
|
||||
@@ -0,0 +1,21 @@
|
||||
; Unlocking first, then waiting, is accepted.
|
||||
; expect: accept
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_unlock_then_wait
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%a: ref<mutex<u64>>, %b: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %a, ok -> @la, cancelled -> @out
|
||||
la(%ga: guard<u64>):
|
||||
mutex.unlock %ga
|
||||
mutex.lock %b, ok -> @lb, cancelled -> @out
|
||||
lb(%gb: guard<u64>):
|
||||
mutex.unlock %gb
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
@@ -0,0 +1,23 @@
|
||||
; A module that does not declare cancellation keeps the RFC 0011 effect rules: task.join needs no task.wait.
|
||||
; expect: accept
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.tasks_join_without_wait_effect
|
||||
air 0
|
||||
lang 0.1
|
||||
features [effects, ownership, tasks]
|
||||
|
||||
fn @main() -> u8
|
||||
effects [task.spawn]
|
||||
entry:
|
||||
%sc = task.scope.begin
|
||||
%t = task.spawn %sc, @w()
|
||||
%r = task.join %t
|
||||
task.scope.end %sc
|
||||
ret %r
|
||||
|
||||
fn @w() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%z = const.u8 0
|
||||
ret %z
|
||||
@@ -0,0 +1,33 @@
|
||||
; On the cancelled edge of task.join.cancel the task is not joined: its loans stay, so ending the lent borrow is rejected.
|
||||
; expect: reject E4302
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_join_keeps_loans
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @main() -> u8
|
||||
effects [task.spawn, task.wait]
|
||||
entry:
|
||||
%x = local.alloc u8
|
||||
local.zero %x
|
||||
%r = borrow.ref %x
|
||||
%sc = task.scope.begin
|
||||
%t = task.spawn %sc, @w(%r)
|
||||
task.join.cancel %t, ok -> @joined, cancelled -> @cancelled
|
||||
joined(%v: u8):
|
||||
task.scope.end %sc
|
||||
borrow.end %r
|
||||
ret %v
|
||||
cancelled(%t2: task<u8>):
|
||||
borrow.end %r
|
||||
%v2 = task.join %t2
|
||||
task.scope.end %sc
|
||||
ret %v2
|
||||
|
||||
fn @w(%p: ref<u8>) -> u8
|
||||
effects []
|
||||
entry:
|
||||
%v = load %p
|
||||
ret %v
|
||||
@@ -0,0 +1,23 @@
|
||||
; In a module declaring cancellation, task.join requires the effect task.wait.
|
||||
; expect: reject E3101
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_join_needs_wait
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @main() -> u8
|
||||
effects [task.spawn]
|
||||
entry:
|
||||
%sc = task.scope.begin
|
||||
%t = task.spawn %sc, @w()
|
||||
%r = task.join %t
|
||||
task.scope.end %sc
|
||||
ret %r
|
||||
|
||||
fn @w() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%z = const.u8 0
|
||||
ret %z
|
||||
@@ -0,0 +1,17 @@
|
||||
; Shields end innermost first (spec 04 §37.3).
|
||||
; expect: reject E4305
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_shield_order
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
%a = task.shield.begin
|
||||
%b = task.shield.begin
|
||||
task.shield.end %a
|
||||
task.shield.end %b
|
||||
ret
|
||||
@@ -0,0 +1,18 @@
|
||||
; A channel element contains no borrow (reported for each endpoint parameter).
|
||||
; expect: reject E1001 E1001
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_borrow_element
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @f(%heap: cap<mem.alloc>) -> void
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%n = const.usize 1
|
||||
channel.make ref<u64>, %n, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<ref<u64>>, %rx: receiver<ref<u64>>):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,18 @@
|
||||
; Endpoints inside channel elements belong to the reserved feature channel_transfer.
|
||||
; expect: reject E0006
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_endpoint_element
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @f(%heap: cap<mem.alloc>) -> void
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%n = const.usize 1
|
||||
channel.make sender<u64>, %n, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<sender<u64>>, %rx: receiver<sender<u64>>):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,19 @@
|
||||
; Endpoints are linear: a receiver that is never closed leaks.
|
||||
; expect: reject E3002
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_receiver_leak
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @f(%heap: cap<mem.alloc>) -> void
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%n = const.usize 1
|
||||
channel.make u64, %n, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<u64>, %rx: receiver<u64>):
|
||||
channel.close_sender %tx
|
||||
ret
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,17 @@
|
||||
; channel.recv may suspend: it requires the effect task.wait.
|
||||
; expect: reject E3101
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_recv_without_effect
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @f(%rxb: mut<receiver<u64>>) -> u64
|
||||
effects []
|
||||
entry:
|
||||
channel.recv %rxb, ok -> @got, closed -> @end, cancelled -> @end
|
||||
got(%v: u64):
|
||||
ret %v
|
||||
end:
|
||||
trap
|
||||
@@ -0,0 +1,21 @@
|
||||
; channel.send consumes the value on every edge; on closed/cancelled it comes back as a block parameter, so the old value is dead.
|
||||
; expect: reject E2001
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_use_after_send
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @f(%txb: mut<sender<own<u64>>>, %o: own<u64>) -> u64
|
||||
effects [task.wait]
|
||||
entry:
|
||||
channel.send %txb, %o, ok -> @sent, closed -> @back, cancelled -> @back
|
||||
sent:
|
||||
%z = const.u64 0
|
||||
ret %z
|
||||
back(%r: own<u64>):
|
||||
%b = borrow.ref %o
|
||||
borrow.end %b
|
||||
%v = own.take %r
|
||||
ret %v
|
||||
@@ -0,0 +1,19 @@
|
||||
; A select closed target takes the send values (here one).
|
||||
; expect: reject E5002
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.select_target_arity
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @f(%txb: mut<sender<u64>>, %x: u64) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
select [send %txb, %x, ok -> @sent, closed -> @gone], cancelled -> @back
|
||||
sent:
|
||||
ret
|
||||
gone:
|
||||
ret
|
||||
back(%v: u64):
|
||||
ret
|
||||
@@ -0,0 +1,29 @@
|
||||
; A call of a function declaring task.wait inside a critical section is rejected.
|
||||
; expect: reject E4304
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_call_waiting_function
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%a: ref<mutex<u64>>, %b: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %a, ok -> @la, cancelled -> @out
|
||||
la(%ga: guard<u64>):
|
||||
call @g(%b)
|
||||
mutex.unlock %ga
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
|
||||
fn @g(%b: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %b, ok -> @lb, cancelled -> @out
|
||||
lb(%gb: guard<u64>):
|
||||
mutex.unlock %gb
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
@@ -0,0 +1,17 @@
|
||||
; A guard is never unlocked implicitly: leaving it live is a leak.
|
||||
; expect: reject E3002
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_guard_leak
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%a: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %a, ok -> @la, cancelled -> @out
|
||||
la(%g: guard<u64>):
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
@@ -0,0 +1,14 @@
|
||||
; guard<T> is function-bound; as a parameter it belongs to the reserved feature guard_transfer.
|
||||
; expect: reject E0006
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_guard_parameter
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%g: guard<u64>) -> void
|
||||
effects []
|
||||
entry:
|
||||
mutex.unlock %g
|
||||
ret
|
||||
@@ -0,0 +1,24 @@
|
||||
; mutex.lock may wait, so a second lock while holding a guard is rejected: lock-order deadlock is not expressible.
|
||||
; expect: reject E4304
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_lock_twice
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%a: ref<mutex<u64>>, %b: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %a, ok -> @la, cancelled -> @out
|
||||
la(%ga: guard<u64>):
|
||||
mutex.lock %b, ok -> @lb, cancelled -> @ub
|
||||
lb(%gb: guard<u64>):
|
||||
mutex.unlock %gb
|
||||
mutex.unlock %ga
|
||||
ret
|
||||
ub:
|
||||
mutex.unlock %ga
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
@@ -0,0 +1,20 @@
|
||||
; The mut<T> from mutex.borrow must end before mutex.unlock.
|
||||
; expect: reject E2102
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_unlock_live_borrow
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @f(%a: ref<mutex<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %a, ok -> @la, cancelled -> @out
|
||||
la(%g: guard<u64>):
|
||||
%p = mutex.borrow %g
|
||||
mutex.unlock %g
|
||||
borrow.end %p
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
@@ -0,0 +1,23 @@
|
||||
; A task may not wait while it holds a lock guard (spec 04 §41).
|
||||
; expect: reject E4304
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_wait_while_locked
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, sync, tasks]
|
||||
|
||||
fn @f(%mr: ref<mutex<u64>>, %rxb: mut<receiver<u64>>) -> void
|
||||
effects [task.wait]
|
||||
entry:
|
||||
mutex.lock %mr, ok -> @locked, cancelled -> @out
|
||||
locked(%g: guard<u64>):
|
||||
channel.recv %rxb, ok -> @got, closed -> @end, cancelled -> @end
|
||||
got(%v: u64):
|
||||
mutex.unlock %g
|
||||
ret
|
||||
end:
|
||||
mutex.unlock %g
|
||||
ret
|
||||
out:
|
||||
ret
|
||||
@@ -0,0 +1,48 @@
|
||||
; task.join.cancel: a cancelled parent gets its child's handle back (not joined,
|
||||
; loans kept), forwards the request and joins normally.
|
||||
; expect: run
|
||||
; schedules: 20
|
||||
; result: 13
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_join_forward
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [task.spawn, task.wait]
|
||||
entry:
|
||||
%sc = task.scope.begin
|
||||
%p = task.spawn %sc, @parent()
|
||||
task.cancel %p
|
||||
%r = task.join %p
|
||||
task.scope.end %sc
|
||||
ret %r
|
||||
|
||||
fn @parent() -> u64
|
||||
effects [task.spawn, task.wait]
|
||||
entry:
|
||||
%sc = task.scope.begin
|
||||
%c = task.spawn %sc, @child()
|
||||
task.join.cancel %c, ok -> @joined, cancelled -> @cancelled
|
||||
joined(%v: u64):
|
||||
task.scope.end %sc
|
||||
ret %v
|
||||
cancelled(%c2: task<u64>):
|
||||
task.cancel %c2
|
||||
%v2 = task.join %c2
|
||||
task.scope.end %sc
|
||||
%ten = const.u64 10
|
||||
%r = add.checked.u64 %v2, %ten
|
||||
ret %r
|
||||
|
||||
fn @child() -> u64
|
||||
effects []
|
||||
entry:
|
||||
br @spin()
|
||||
spin:
|
||||
task.check_cancel, ok -> @spin, cancelled -> @stop
|
||||
stop:
|
||||
%three = const.u64 3
|
||||
ret %three
|
||||
@@ -0,0 +1,58 @@
|
||||
; Inside a shield cancellation is not observed: the shielded receive waits for
|
||||
; the value; after the shield ends, task.check_cancel sees the pending request.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; schedules: 20
|
||||
; result: 105
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_shield
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> 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<u64>, %rx: receiver<u64>):
|
||||
%sc = task.scope.begin
|
||||
%t = task.spawn %sc, @waiter(%rx)
|
||||
task.cancel %t
|
||||
%txb = borrow.mut %tx
|
||||
%five = const.u64 5
|
||||
channel.send %txb, %five, ok -> @sent, closed -> @back, cancelled -> @back
|
||||
back(%w: u64):
|
||||
br @sent()
|
||||
sent:
|
||||
borrow.end %txb
|
||||
%r = task.join %t
|
||||
task.scope.end %sc
|
||||
channel.close_sender %tx
|
||||
ret %r
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
|
||||
fn @waiter(%rx: receiver<u64>) -> u64
|
||||
effects [task.wait]
|
||||
entry:
|
||||
%sh = task.shield.begin
|
||||
%rb = borrow.mut %rx
|
||||
channel.recv %rb, ok -> @got, closed -> @bad, cancelled -> @bad
|
||||
got(%v: u64):
|
||||
borrow.end %rb
|
||||
task.shield.end %sh
|
||||
channel.close_receiver %rx, ok -> @check, pending -> @pending
|
||||
check:
|
||||
task.check_cancel, ok -> @plain, cancelled -> @seen
|
||||
plain:
|
||||
ret %v
|
||||
seen:
|
||||
%hundred = const.u64 100
|
||||
%r = add.checked.u64 %v, %hundred
|
||||
ret %r
|
||||
bad:
|
||||
trap
|
||||
pending(%r2: receiver<u64>):
|
||||
trap
|
||||
@@ -0,0 +1,53 @@
|
||||
; A task waiting in channel.recv observes cancellation on its cancelled edge;
|
||||
; cancellation is a request, so the parent still joins it.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; schedules: 20
|
||||
; result: 7
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.cancel_wait_recv
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u8
|
||||
effects [mem.alloc, task.spawn, task.wait]
|
||||
entry:
|
||||
%one = const.usize 1
|
||||
channel.make u64, %one, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<u64>, %rx: receiver<u64>):
|
||||
%sc = task.scope.begin
|
||||
%t = task.spawn %sc, @waiter(%rx)
|
||||
task.cancel %t
|
||||
%r = task.join %t
|
||||
task.scope.end %sc
|
||||
channel.close_sender %tx
|
||||
ret %r
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
|
||||
fn @waiter(%rx: receiver<u64>) -> u8
|
||||
effects [task.wait]
|
||||
entry:
|
||||
%rb = borrow.mut %rx
|
||||
channel.recv %rb, ok -> @got, closed -> @other, cancelled -> @cancelled
|
||||
got(%v: u64):
|
||||
borrow.end %rb
|
||||
br @close(%rx)
|
||||
other:
|
||||
borrow.end %rb
|
||||
br @close(%rx)
|
||||
close(%r: receiver<u64>):
|
||||
channel.close_receiver %r, ok -> @wrong, pending -> @pending
|
||||
pending(%r2: receiver<u64>):
|
||||
trap
|
||||
wrong:
|
||||
%z = const.u8 0
|
||||
ret %z
|
||||
cancelled:
|
||||
borrow.end %rb
|
||||
channel.close_receiver %rx, ok -> @done, pending -> @pending
|
||||
done:
|
||||
%seven = const.u8 7
|
||||
ret %seven
|
||||
@@ -0,0 +1,32 @@
|
||||
; The only task waits on a channel whose sender it still holds: no future
|
||||
; event can wake it, so the run ends blocked (not an error, not a trap).
|
||||
; expect: run
|
||||
; blocked: no_future_event
|
||||
; grant: heap=mem.alloc:64
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_blocked
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.wait]
|
||||
entry:
|
||||
%one = const.usize 1
|
||||
channel.make u64, %one, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<u64>, %rx: receiver<u64>):
|
||||
%rb = borrow.mut %rx
|
||||
channel.recv %rb, ok -> @got, closed -> @end, cancelled -> @end
|
||||
got(%v: u64):
|
||||
borrow.end %rb
|
||||
channel.close_sender %tx
|
||||
channel.close_receiver %rx, ok -> @fin, pending -> @stuck
|
||||
end:
|
||||
trap
|
||||
fin:
|
||||
ret %v
|
||||
stuck(%p: receiver<u64>):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,71 @@
|
||||
; Closing a receiver with buffered owned values hands it back on pending; a
|
||||
; send after that ends closed and returns the value; the receiver drains
|
||||
; everything before the close succeeds. No owned value is dropped.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; result: 13
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_drain_linear
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.wait]
|
||||
entry:
|
||||
%four = const.usize 4
|
||||
channel.make own<u64>, %four, %heap, ok -> @made, err -> @oom0
|
||||
made(%tx: sender<own<u64>>, %rx: receiver<own<u64>>):
|
||||
%txb = borrow.mut %tx
|
||||
%one = const.u64 1
|
||||
own.new %one, %heap, ok -> @o1, err -> @oom
|
||||
o1(%a: own<u64>):
|
||||
channel.send %txb, %a, ok -> @s1, closed -> @lost, cancelled -> @lost
|
||||
s1:
|
||||
%two = const.u64 2
|
||||
own.new %two, %heap, ok -> @o2, err -> @oom
|
||||
o2(%b: own<u64>):
|
||||
channel.send %txb, %b, ok -> @s2, closed -> @lost, cancelled -> @lost
|
||||
s2:
|
||||
channel.close_receiver %rx, ok -> @unexpected, pending -> @closing
|
||||
closing(%r: receiver<own<u64>>):
|
||||
%ten = const.u64 10
|
||||
own.new %ten, %heap, ok -> @o3, err -> @oom2
|
||||
o3(%c: own<u64>):
|
||||
channel.send %txb, %c, ok -> @unexpected2, closed -> @back, cancelled -> @back
|
||||
back(%returned: own<u64>):
|
||||
%v0 = own.take %returned
|
||||
br @drain(%r, %v0)
|
||||
drain(%rr: receiver<own<u64>>, %acc: u64):
|
||||
%rb = borrow.mut %rr
|
||||
channel.recv %rb, ok -> @got, closed -> @empty, cancelled -> @retry
|
||||
got(%o: own<u64>):
|
||||
borrow.end %rb
|
||||
%v = own.take %o
|
||||
%acc2 = add.checked.u64 %acc, %v
|
||||
br @drain(%rr, %acc2)
|
||||
retry:
|
||||
borrow.end %rb
|
||||
br @drain(%rr, %acc)
|
||||
empty:
|
||||
borrow.end %rb
|
||||
channel.close_receiver %rr, ok -> @done, pending -> @again
|
||||
again(%r4: receiver<own<u64>>):
|
||||
br @drain(%r4, %acc)
|
||||
done:
|
||||
borrow.end %txb
|
||||
channel.close_sender %tx
|
||||
ret %acc
|
||||
lost(%x: own<u64>):
|
||||
trap
|
||||
unexpected:
|
||||
trap
|
||||
unexpected2:
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
oom2(%e2: alloc_error):
|
||||
trap
|
||||
oom0(%e0: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,72 @@
|
||||
; 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<mem.alloc>) -> 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<u64>, %rx: receiver<u64>):
|
||||
%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<u64>, %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<u64>):
|
||||
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<u64>) -> 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
|
||||
@@ -0,0 +1,66 @@
|
||||
; A producer task sends 0..4 on a bounded channel and closes its sender; the
|
||||
; consumer receives until closed (buffered values first), then closes the receiver.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:1024
|
||||
; schedules: 20
|
||||
; quantum: 3
|
||||
; result: 10
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_producer_consumer
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.spawn, task.wait]
|
||||
entry:
|
||||
%two = const.usize 2
|
||||
channel.make u64, %two, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<u64>, %rx: receiver<u64>):
|
||||
%sc = task.scope.begin
|
||||
%five = const.u64 5
|
||||
%t = task.spawn %sc, @produce(%tx, %five)
|
||||
%z = const.u64 0
|
||||
br @recv_loop(%rx, %z)
|
||||
recv_loop(%r: receiver<u64>, %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<u64>):
|
||||
br @recv_loop(%r2, %acc)
|
||||
closed:
|
||||
%x = task.join %t
|
||||
task.scope.end %sc
|
||||
ret %acc
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
|
||||
fn @produce(%tx: sender<u64>, %n: u64) -> u8
|
||||
effects [task.wait]
|
||||
entry:
|
||||
%txb = borrow.mut %tx
|
||||
%zero = const.u64 0
|
||||
br @loop(%zero)
|
||||
loop(%i: u64):
|
||||
%done = eq.u64 %i, %n
|
||||
cbr %done, @finish(), @send(%i)
|
||||
send(%j: u64):
|
||||
channel.send %txb, %j, ok -> @sent, closed -> @gone, cancelled -> @gone
|
||||
sent:
|
||||
%one = const.u64 1
|
||||
%k = add.checked.u64 %j, %one
|
||||
br @loop(%k)
|
||||
gone(%w: u64):
|
||||
br @finish()
|
||||
finish:
|
||||
borrow.end %txb
|
||||
channel.close_sender %tx
|
||||
%r = const.u8 0
|
||||
ret %r
|
||||
@@ -0,0 +1,61 @@
|
||||
; Capacity 0: every send completes only when the receiver takes the value.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; schedules: 20
|
||||
; quantum: 2
|
||||
; result: 6
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.channels_rendezvous
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.spawn, task.wait]
|
||||
entry:
|
||||
%zero = const.usize 0
|
||||
channel.make u64, %zero, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<u64>, %rx: receiver<u64>):
|
||||
%sc = task.scope.begin
|
||||
%t = task.spawn %sc, @produce(%tx)
|
||||
%z = const.u64 0
|
||||
br @recv_loop(%rx, %z)
|
||||
recv_loop(%r: receiver<u64>, %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<u64>):
|
||||
br @recv_loop(%r2, %acc)
|
||||
closed:
|
||||
%x = task.join %t
|
||||
task.scope.end %sc
|
||||
ret %acc
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
|
||||
fn @produce(%tx: sender<u64>) -> u8
|
||||
effects [task.wait]
|
||||
entry:
|
||||
%txb = borrow.mut %tx
|
||||
%one = const.u64 1
|
||||
channel.send %txb, %one, ok -> @s2, closed -> @gone, cancelled -> @gone
|
||||
s2:
|
||||
%two = const.u64 2
|
||||
channel.send %txb, %two, ok -> @s3, closed -> @gone, cancelled -> @gone
|
||||
s3:
|
||||
%three = const.u64 3
|
||||
channel.send %txb, %three, ok -> @finish, closed -> @gone, cancelled -> @gone
|
||||
gone(%w: u64):
|
||||
br @finish()
|
||||
finish:
|
||||
borrow.end %txb
|
||||
channel.close_sender %tx
|
||||
%r = const.u8 0
|
||||
ret %r
|
||||
@@ -0,0 +1,68 @@
|
||||
; Both arms are ready: the schedule generator picks one (a recorded choice even
|
||||
; in a single task); the other value is received afterwards.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; schedules: 20
|
||||
; result: 3
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.select_ready_draw
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.wait]
|
||||
entry:
|
||||
%one = const.usize 1
|
||||
channel.make u64, %one, %heap, ok -> @a_made, err -> @oom
|
||||
a_made(%ta: sender<u64>, %ra: receiver<u64>):
|
||||
channel.make u64, %one, %heap, ok -> @b_made, err -> @oom2
|
||||
b_made(%tb: sender<u64>, %rb: receiver<u64>):
|
||||
%tab = borrow.mut %ta
|
||||
%v1 = const.u64 1
|
||||
channel.send %tab, %v1, ok -> @s1, closed -> @lost, cancelled -> @lost
|
||||
s1:
|
||||
%tbb = borrow.mut %tb
|
||||
%v2 = const.u64 2
|
||||
channel.send %tbb, %v2, ok -> @s2, closed -> @lost2, cancelled -> @lost2
|
||||
s2:
|
||||
borrow.end %tab
|
||||
borrow.end %tbb
|
||||
channel.close_sender %ta
|
||||
channel.close_sender %tb
|
||||
%rab = borrow.mut %ra
|
||||
%rbb = borrow.mut %rb
|
||||
select [recv %rab, ok -> @from_a, closed -> @gone], [recv %rbb, ok -> @from_b, closed -> @gone], cancelled -> @gone
|
||||
from_a(%x: u64):
|
||||
channel.recv %rbb, ok -> @second, closed -> @gone, cancelled -> @gone
|
||||
from_b(%y: u64):
|
||||
channel.recv %rab, ok -> @second_b, closed -> @gone, cancelled -> @gone
|
||||
second(%z: u64):
|
||||
%s = add.checked.u64 %x, %z
|
||||
br @finish(%s)
|
||||
second_b(%w: u64):
|
||||
%s2 = add.checked.u64 %y, %w
|
||||
br @finish(%s2)
|
||||
finish(%sum: u64):
|
||||
borrow.end %rab
|
||||
borrow.end %rbb
|
||||
channel.close_receiver %ra, ok -> @ca, pending -> @stuck
|
||||
ca:
|
||||
channel.close_receiver %rb, ok -> @cb, pending -> @stuck2
|
||||
cb:
|
||||
ret %sum
|
||||
gone:
|
||||
trap
|
||||
stuck(%p: receiver<u64>):
|
||||
trap
|
||||
stuck2(%p2: receiver<u64>):
|
||||
trap
|
||||
lost(%l: u64):
|
||||
trap
|
||||
lost2(%l2: u64):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
oom2(%e2: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,60 @@
|
||||
; The same owned value appears in two send arms: it is consumed only on the
|
||||
; edge of the arm that runs; every other edge hands it back as a parameter.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; schedules: 20
|
||||
; result: 42
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.select_same_value
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, capabilities, channels, effects, heap, ownership, resources, tasks]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.wait]
|
||||
entry:
|
||||
%one = const.usize 1
|
||||
channel.make own<u64>, %one, %heap, ok -> @a_made, err -> @oom
|
||||
a_made(%ta: sender<own<u64>>, %ra: receiver<own<u64>>):
|
||||
channel.make own<u64>, %one, %heap, ok -> @b_made, err -> @oom2
|
||||
b_made(%tb: sender<own<u64>>, %rb: receiver<own<u64>>):
|
||||
%v = const.u64 42
|
||||
own.new %v, %heap, ok -> @owned, err -> @oom3
|
||||
owned(%x: own<u64>):
|
||||
%tab = borrow.mut %ta
|
||||
%tbb = borrow.mut %tb
|
||||
select [send %tab, %x, ok -> @sent, closed -> @back], [send %tbb, %x, ok -> @sent, closed -> @back], cancelled -> @back
|
||||
back(%returned: own<u64>):
|
||||
trap
|
||||
sent:
|
||||
borrow.end %tab
|
||||
borrow.end %tbb
|
||||
%rab = borrow.mut %ra
|
||||
%rbb = borrow.mut %rb
|
||||
select [recv %rab, ok -> @got, closed -> @none], [recv %rbb, ok -> @got, closed -> @none], cancelled -> @none
|
||||
got(%o: own<u64>):
|
||||
borrow.end %rab
|
||||
borrow.end %rbb
|
||||
%r = own.take %o
|
||||
; senders stay open until now: an empty channel whose senders are closed
|
||||
; would make its receive arm ready with `closed`
|
||||
channel.close_sender %ta
|
||||
channel.close_sender %tb
|
||||
channel.close_receiver %ra, ok -> @ca, pending -> @stuck
|
||||
ca:
|
||||
channel.close_receiver %rb, ok -> @cb, pending -> @stuck2
|
||||
cb:
|
||||
ret %r
|
||||
none:
|
||||
trap
|
||||
stuck(%p: receiver<own<u64>>):
|
||||
trap
|
||||
stuck2(%p2: receiver<own<u64>>):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
oom2(%e2: alloc_error):
|
||||
trap
|
||||
oom3(%e3: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,53 @@
|
||||
; Two tasks increment a shared counter through a mutex lent as ref<mutex<u64>>;
|
||||
; the critical sections never interleave, whatever the schedule.
|
||||
; expect: run
|
||||
; schedules: 30
|
||||
; quantum: 1
|
||||
; result: 10
|
||||
; phase: 3
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.sync_mutex_counter
|
||||
air 0
|
||||
lang 0.1
|
||||
features [cancellation, effects, ownership, sync, tasks]
|
||||
|
||||
fn @main() -> u64
|
||||
effects [task.spawn, task.wait]
|
||||
entry:
|
||||
%z = const.u64 0
|
||||
%m = mutex.make %z
|
||||
%mr = borrow.ref %m
|
||||
%sc = task.scope.begin
|
||||
%a = task.spawn %sc, @bump(%mr)
|
||||
%b = task.spawn %sc, @bump(%mr)
|
||||
%x = task.join %a
|
||||
%y = task.join %b
|
||||
task.scope.end %sc
|
||||
borrow.end %mr
|
||||
%v = mutex.take %m
|
||||
ret %v
|
||||
|
||||
fn @bump(%mr: ref<mutex<u64>>) -> u8
|
||||
effects [task.wait]
|
||||
entry:
|
||||
%zero = const.u64 0
|
||||
br @loop(%zero)
|
||||
loop(%i: u64):
|
||||
%n = const.u64 5
|
||||
%done = eq.u64 %i, %n
|
||||
cbr %done, @finish(), @lock(%i)
|
||||
lock(%j: u64):
|
||||
mutex.lock %mr, ok -> @locked, cancelled -> @finish
|
||||
locked(%g: guard<u64>):
|
||||
%p = mutex.borrow %g
|
||||
%x = load %p
|
||||
%one = const.u64 1
|
||||
%y = add.checked.u64 %x, %one
|
||||
store %p, %y
|
||||
borrow.end %p
|
||||
mutex.unlock %g
|
||||
%k = add.checked.u64 %j, %one
|
||||
br @loop(%k)
|
||||
finish:
|
||||
%r = const.u8 0
|
||||
ret %r
|
||||
Reference in new issue
Block a user