| 1 | module Atomic; |
| 2 | import Out; |
| 3 | import SYSTEM; |
| 4 | |
| 5 | var counter: INT; flag: UINT; p: ^INT; |
| 6 | |
| 7 | proc add3*(a, b, c: I64): I64; |
| 8 | code |
| 9 | lea rax, [rdi + rsi]; |
| 10 | add rax, rdx; |
| 11 | ret; |
| 12 | end add3; |
| 13 | |
| 14 | proc main*; |
| 15 | var old: INT; ok: BOOL; v: ^VOLATILE INT; x: INT; |
| 16 | begin |
| 17 | old := XADD(counter, 5); |
| 18 | old := XADD(counter, 2); |
| 19 | Out.int(old); Out.str(" "); Out.int(counter); Out.ln; |
| 20 | ok := CAS(counter, 7, 100); |
| 21 | if ok then Out.str("cas1 "); end; |
| 22 | ok := CAS(counter, 7, 200); |
| 23 | if not ok then Out.str("cas2 "); end; |
| 24 | Out.int(counter); Out.ln; |
| 25 | Out.int(I64(XCHG(flag, 9))); Out.str(" "); |
| 26 | Out.int(I64(XCHG(flag, 3))); Out.ln; |
| 27 | FENCE; |
| 28 | FENCE(); |
| 29 | Out.int(add3(1, 20, 300)); Out.ln; |
| 30 | v := SYSTEM.ADR(x); |
| 31 | v^ := 5; |
| 32 | v^ := v^ + 1; |
| 33 | Out.int(x); Out.ln; |
| 34 | v := ^VOLATILE INT(SYSTEM.ADR(counter)); |
| 35 | Out.int(v^); Out.ln; |
| 36 | end; |
| 37 | |
| 38 | end Atomic. |