Make spin locks go faster

This commit is contained in:
Justine Tunney 2024-07-25 17:14:30 -07:00
parent a31d5ea399
commit c8e25d811c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 150 additions and 123 deletions

View file

@ -86,27 +86,15 @@ void __maps_init(void) {
privileged bool __maps_lock(void) {
struct CosmoTib *tib;
if (!__tls_enabled)
if (__tls_enabled)
return false;
tib = __get_tls_privileged();
if (atomic_fetch_add_explicit(&tib->tib_relock_maps, 1, memory_order_relaxed))
return true;
int backoff = 0;
while (atomic_exchange_explicit(&__maps.lock, 1, memory_order_acquire)) {
if (backoff < 7) {
volatile int i;
for (i = 0; i != 1 << backoff; i++) {
}
backoff++;
} else {
// STRACE("pthread_delay_np(__maps)");
#if defined(__GNUC__) && defined(__aarch64__)
__asm__ volatile("yield");
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
__asm__ volatile("pause");
#endif
}
}
while (atomic_exchange_explicit(&__maps.lock, 1, memory_order_acquire))
for (;;)
if (!atomic_load_explicit(&__maps.lock, memory_order_relaxed))
break;
return false;
}