Make spinlocks faster (take two)

This change is green on x86 and arm test fleet.
This commit is contained in:
Justine Tunney 2024-07-26 00:44:45 -07:00
parent 02e1cbcd00
commit 59692b0882
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
14 changed files with 122 additions and 79 deletions

View file

@ -31,17 +31,16 @@
#include "third_party/nsync/futex.internal.h"
#include "third_party/nsync/mu.h"
static void pthread_mutex_lock_naive(pthread_mutex_t *mutex, uint64_t word) {
static void pthread_mutex_lock_spin(atomic_int *word) {
int backoff = 0;
uint64_t lock;
for (;;) {
word = MUTEX_UNLOCK(word);
lock = MUTEX_LOCK(word);
if (atomic_compare_exchange_weak_explicit(&mutex->_word, &word, lock,
memory_order_acquire,
memory_order_relaxed))
return;
backoff = pthread_delay_np(mutex, backoff);
if (!atomic_exchange_explicit(word, 1, memory_order_acquire))
break;
for (;;) {
if (!atomic_load_explicit(word, memory_order_relaxed))
break;
backoff = pthread_delay_np(word, backoff);
}
}
}
@ -96,7 +95,14 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
mutex->_pid = __pid;
return 0;
}
backoff = pthread_delay_np(mutex, backoff);
for (;;) {
word = atomic_load_explicit(&mutex->_word, memory_order_relaxed);
if (MUTEX_OWNER(word) == me)
break;
if (word == MUTEX_UNLOCK(word))
break;
backoff = pthread_delay_np(mutex, backoff);
}
}
}
@ -121,7 +127,7 @@ static errno_t pthread_mutex_lock_impl(pthread_mutex_t *mutex) {
if (_weaken(nsync_futex_wait_)) {
pthread_mutex_lock_drepper(&mutex->_futex, MUTEX_PSHARED(word));
} else {
pthread_mutex_lock_naive(mutex, word);
pthread_mutex_lock_spin(&mutex->_futex);
}
return 0;
}