Created
February 27, 2022 14:37
-
-
Save ikskuh/bc7767636838bd1f0af96895fd0698e0 to your computer and use it in GitHub Desktop.
Examples of why atomics are necessary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
var global: u32 = 0; | |
export fn normalStore(arg: u32) void { | |
global = arg; | |
} | |
export fn normalLoad() u32 { | |
return global; | |
} | |
export fn volatileStore(arg: u32) void { | |
@ptrCast(*volatile u32, &global).* = arg; | |
} | |
export fn volatileLoad() u32 { | |
return @ptrCast(*volatile u32, &global).*; | |
} | |
export fn atomicStoreMonotonic(val: u32) void { | |
@atomicStore(u32, &global, val, .Monotonic); | |
} | |
export fn atomicStoreRelease(val: u32) void { | |
@atomicStore(u32, &global, val, .Release); | |
} | |
export fn atomicStoreSeqCst(val: u32) void { | |
@atomicStore(u32, &global, val, .SeqCst); | |
} | |
export fn atomicLoadMonotonic() u32 { | |
return @atomicLoad(u32, &global, .Monotonic); | |
} | |
export fn atomicLoadAcquire() u32 { | |
return @atomicLoad(u32, &global, .Acquire); | |
} | |
export fn atomicLoadSeqCst() u32 { | |
return @atomicLoad(u32, &global, .SeqCst); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment