; Two tasks increment a shared counter through a mutex lent as ref>; ; 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>) -> 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): %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