Skip to content

Adding 'sb' instruction to fastlock_lock() for ARM v8.5 onward #911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/fastlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
#include "config.h"
#include "serverassert.h"

#if defined(__aarch64__) && defined(__linux__)
#include <sys/auxv.h>
#ifndef HWCAP_SB
#define HWCAP_SB (1 << 29)
#endif // HWCAP_SB
#endif // __aarch64__ __linux__

#ifdef __APPLE__
#include <TargetConditionals.h>
#ifdef TARGET_OS_MAC
Expand Down Expand Up @@ -379,7 +386,24 @@ extern "C" void fastlock_lock(struct fastlock *lock, spin_worker worker)
#if defined(__i386__) || defined(__amd64__)
__asm__ __volatile__ ("pause");
#elif defined(__aarch64__)
__asm__ __volatile__ ("yield");
#ifdef __linux__
static int use_spin_delay_sb = -1;

// Use SB instruction if available otherwise ISB
if (__builtin_expect(use_spin_delay_sb == 1, 1)) {
__asm__ __volatile__(".inst 0xd50330ff"); // SB instruction encoding
} else if (use_spin_delay_sb == 0) {
__asm__ __volatile__("isb");
} else {
// Initialize variable and use getauxval fuction as delay
if (getauxval(AT_HWCAP) & HWCAP_SB)
use_spin_delay_sb = 1;
else
use_spin_delay_sb = 0;
}
#else
__asm__ __volatile__ ("isb");
#endif // __linux__
#endif

if ((++cloops % loopLimit) == 0)
Expand Down