- lock ref m as x { … } cancelled { … }: mutex.lock on a ref<mutex<T>>
(a mutex is borrowed for the lock, a ref binding used as is); ok edge:
mutex.borrow bound as x; at every normal exit borrow.end x then
mutex.unlock; the guard is never a source value
- mutex.make/mutex.take; mutex<T> type; ref mr = m lends a mutex
- shield { … }: task.shield.begin/end at every exit (nesting by construction)
- task.check_cancel and task.join_cancel as outcome matches
- select: one borrow per endpoint, send values evaluated first and
deduplicated, every edge binds its own value then the unsent values;
bind count E7005, missing outcome E7004
- linear values never get frame storage (borrowed in place)
- liveness: select handled in the read and after walks
- Quick Reference: select edges bind `job` back (ok(x, job), closed(job));
every Quick Reference example now passes (no PENDING)
- tests: 6 run tests with AIR equivalence (mutex counter, select draw,
select same value, shield, join_cancel forwarding, cancelled recv), 10
rejects (E4304 x3, E3001, E2101, E7005 x2, E7004, E3101 x2, E4101)
- spec 15 §7/§9, quick reference, bundle notes, plan
42 lines
925 B
Rust
42 lines
925 B
Rust
// Two tasks increment a shared counter under a mutex; the lock block unlocks at
|
|
// every exit; mutex.take dismantles the mutex once no borrow is live.
|
|
// expect: run
|
|
// schedules: 30
|
|
// quantum: 1
|
|
// result: 10
|
|
// equivalent: phase3/run/sync_mutex_counter.air
|
|
// phase: 6
|
|
// spec: docs/spec/15-source-language.md
|
|
module demo.mutexcounter
|
|
|
|
fn bump(mr: ref<mutex<u64>>) -> u8
|
|
effects [task.wait]
|
|
{
|
|
var i: u64 = 0;
|
|
while i != 5 {
|
|
lock ref mr as count {
|
|
*count = *count + 1;
|
|
} cancelled {
|
|
return 0;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
0
|
|
}
|
|
|
|
fn main() -> u64
|
|
effects [task.spawn, task.wait]
|
|
{
|
|
let m = mutex.make(0u64);
|
|
{
|
|
ref mr = m;
|
|
scope sc {
|
|
let a = task.spawn(sc, bump, mr);
|
|
let b = task.spawn(sc, bump, mr);
|
|
let x = task.join(move a);
|
|
let y = task.join(move b);
|
|
}
|
|
}
|
|
mutex.take(move m)
|
|
}
|