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

@ -27,14 +27,8 @@
#include "third_party/nsync/futex.internal.h"
#include "third_party/nsync/mu.h"
static errno_t pthread_mutex_trylock_naive(pthread_mutex_t *mutex,
uint64_t word) {
uint64_t lock;
word = MUTEX_UNLOCK(word);
lock = MUTEX_LOCK(word);
if (atomic_compare_exchange_weak_explicit(&mutex->_word, &word, lock,
memory_order_acquire,
memory_order_relaxed))
static errno_t pthread_mutex_trylock_spin(atomic_int *word) {
if (!atomic_exchange_explicit(word, 1, memory_order_acquire))
return 0;
return EBUSY;
}
@ -116,7 +110,7 @@ errno_t pthread_mutex_trylock(pthread_mutex_t *mutex) {
if (_weaken(nsync_futex_wait_)) {
return pthread_mutex_trylock_drepper(&mutex->_futex);
} else {
return pthread_mutex_trylock_naive(mutex, word);
return pthread_mutex_trylock_spin(&mutex->_futex);
}
}