authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-07-27 12:03:05+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-07-29 09:41:47+02:00
log9d23e711ef98a4c941165457c1fdcdc736b264dc
tree6849e3e3ad809c9bceb03d7391f9e20528a29ef5
parent5b68595255d844764d7a4c1869992c8d666d6960
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.atomic: Implement specialized spinLoopHint() for more architectures.


1 files changed, 32 insertions(+), 4 deletions(-)

lib/std/atomic.zig+32-4
......@@ -378,21 +378,33 @@ pub inline fn spinLoopHint() void {
378378 // No-op instruction that can hint to save (or share with a hardware-thread)
379379 // pipelining/power resources
380380 // https://software.intel.com/content/www/us/en/develop/articles/benefitting-power-and-performance-sleep-loops.html
381 .x86, .x86_64 => asm volatile ("pause" ::: "memory"),
381 .x86,
382 .x86_64,
383 => asm volatile ("pause" ::: "memory"),
382384
383385 // No-op instruction that serves as a hardware-thread resource yield hint.
384386 // https://stackoverflow.com/a/7588941
385 .powerpc64, .powerpc64le => asm volatile ("or 27, 27, 27" ::: "memory"),
387 .powerpc,
388 .powerpcle,
389 .powerpc64,
390 .powerpc64le,
391 => asm volatile ("or 27, 27, 27" ::: "memory"),
386392
387393 // `isb` appears more reliable for releasing execution resources than `yield`
388394 // on common aarch64 CPUs.
389395 // https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8258604
390396 // https://bugs.mysql.com/bug.php?id=100664
391 .aarch64, .aarch64_be => asm volatile ("isb" ::: "memory"),
397 .aarch64,
398 .aarch64_be,
399 => asm volatile ("isb" ::: "memory"),
392400
393401 // `yield` was introduced in v6k but is also available on v6m.
394402 // https://www.keil.com/support/man/docs/armasm/armasm_dom1361289926796.htm
395 .arm, .armeb, .thumb, .thumbeb => {
403 .arm,
404 .armeb,
405 .thumb,
406 .thumbeb,
407 => {
396408 const can_yield = comptime std.Target.arm.featureSetHasAny(builtin.target.cpu.features, .{
397409 .has_v6k, .has_v6m,
398410 });
......@@ -402,6 +414,22 @@ pub inline fn spinLoopHint() void {
402414 asm volatile ("" ::: "memory");
403415 }
404416 },
417
418 // The 8-bit immediate specifies the amount of cycles to pause for. We can't really be too
419 // opinionated here.
420 .hexagon,
421 => asm volatile ("pause(#1)" ::: "memory"),
422
423 .riscv32,
424 .riscv64,
425 => {
426 if (comptime std.Target.riscv.featureSetHas(builtin.target.cpu.features, .zihintpause)) {
427 asm volatile ("pause" ::: "memory");
428 } else {
429 asm volatile ("" ::: "memory");
430 }
431 },
432
405433 // Memory barrier to prevent the compiler from optimizing away the spin-loop
406434 // even if no hint_instruction was provided.
407435 else => asm volatile ("" ::: "memory"),