124_atomic_word_ops.toy (871B)
1 // Word-sized (4-byte) atomics. 4-byte atomics are lock-free on every target 2 // (including rv32, whose lr.w/sc.w/amo*.w are native), so this case runs on all 3 // lanes and guards the universal word-atomic lowering. Exercises store, rmw add, 4 // rmw xchg, cmpxchg, and load; returns the sentinel 42. 5 fn __user_main(): i32 { 6 var x: i32 = 0 as i32; 7 @atomic_store<i32>(&x, 40 as i32, .seq_cst); 8 let old: i32 = @atomic_rmw<i32>(.add, &x, 2 as i32, .seq_cst); // old=40, x=42 9 let prev: i32 = @atomic_rmw<i32>(.xchg, &x, 100 as i32, .seq_cst); // prev=42, x=100 10 let r = @atomic_cmpxchg<i32>(&x, 100 as i32, 42 as i32, .seq_cst, .seq_cst, .strong); // x=42 11 if old != 40 as i32 { return 1 as i32; } 12 if prev != 42 as i32 { return 2 as i32; } 13 if !r.ok { return 3 as i32; } 14 return @atomic_load<i32>(&x, .seq_cst); // 42 15 } 16 17 fn main(): i32 { return __user_main(); }