Skip to content

Commit

Permalink
std.atomic: Don't lie to the compiler about memory clobbers in spinLo…
Browse files Browse the repository at this point in the history
…opHint().
  • Loading branch information
alexrp committed Jul 29, 2024
1 parent c37fe4b commit a074236
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions lib/std/atomic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -380,23 +380,23 @@ pub inline fn spinLoopHint() void {
// https://software.intel.com/content/www/us/en/develop/articles/benefitting-power-and-performance-sleep-loops.html
.x86,
.x86_64,
=> asm volatile ("pause" ::: "memory"),
=> asm volatile ("pause"),

// No-op instruction that serves as a hardware-thread resource yield hint.
// https://stackoverflow.com/a/7588941
.powerpc,
.powerpcle,
.powerpc64,
.powerpc64le,
=> asm volatile ("or 27, 27, 27" ::: "memory"),
=> asm volatile ("or 27, 27, 27"),

// `isb` appears more reliable for releasing execution resources than `yield`
// on common aarch64 CPUs.
// https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8258604
// https://bugs.mysql.com/bug.php?id=100664
.aarch64,
.aarch64_be,
=> asm volatile ("isb" ::: "memory"),
=> asm volatile ("isb"),

// `yield` was introduced in v6k but is also available on v6m.
// https://www.keil.com/support/man/docs/armasm/armasm_dom1361289926796.htm
Expand All @@ -409,30 +409,26 @@ pub inline fn spinLoopHint() void {
.has_v6k, .has_v6m,
});
if (can_yield) {
asm volatile ("yield" ::: "memory");
asm volatile ("yield");
} else {
asm volatile ("" ::: "memory");
asm volatile ("");
}
},

// The 8-bit immediate specifies the amount of cycles to pause for. We can't really be too
// opinionated here.
.hexagon,
=> asm volatile ("pause(#1)" ::: "memory"),
=> asm volatile ("pause(#1)"),

.riscv32,
.riscv64,
=> {
if (comptime std.Target.riscv.featureSetHas(builtin.target.cpu.features, .zihintpause)) {
asm volatile ("pause" ::: "memory");
} else {
asm volatile ("" ::: "memory");
asm volatile ("pause");
}
},

// Memory barrier to prevent the compiler from optimizing away the spin-loop
// even if no hint_instruction was provided.
else => asm volatile ("" ::: "memory"),
else => {},
}
}

Expand Down

0 comments on commit a074236

Please sign in to comment.