Make recursive mutexes slightly faster

This commit is contained in:
Justine Tunney 2024-12-30 01:37:14 -08:00
parent 9ba5b227d9
commit c7e3d9f7ff
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 12 additions and 8 deletions

View file

@ -18,6 +18,7 @@
*/ */
#include "libc/assert.h" #include "libc/assert.h"
#include "libc/atomic.h" #include "libc/atomic.h"
#include "libc/calls/cp.internal.h"
#include "libc/calls/internal.h" #include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h" #include "libc/calls/sig.internal.h"
#include "libc/calls/struct/sigset.h" #include "libc/calls/struct/sigset.h"
@ -232,6 +233,7 @@ static int cosmo_futex_fix_timeout (struct timespec *memory, int clock,
* @raise EAGAIN if `*w` wasn't `expect` * @raise EAGAIN if `*w` wasn't `expect`
* @raise EINTR if a signal handler was called while waiting * @raise EINTR if a signal handler was called while waiting
* @raise ECANCELED if calling thread was canceled while waiting * @raise ECANCELED if calling thread was canceled while waiting
* @cancelationpoint
*/ */
int cosmo_futex_wait (atomic_int *w, int expect, char pshare, int cosmo_futex_wait (atomic_int *w, int expect, char pshare,
int clock, const struct timespec *abstime) { int clock, const struct timespec *abstime) {
@ -240,6 +242,7 @@ int cosmo_futex_wait (atomic_int *w, int expect, char pshare,
struct PosixThread *pt; struct PosixThread *pt;
struct timespec tsmem; struct timespec tsmem;
struct timespec *timeout = 0; struct timespec *timeout = 0;
BEGIN_CANCELATION_POINT;
cosmo_once (&g_cosmo_futex.once, cosmo_futex_init); cosmo_once (&g_cosmo_futex.once, cosmo_futex_init);
@ -351,6 +354,7 @@ Finished:
DescribeTimespec (0, abstime), DescribeTimespec (0, abstime),
DescribeErrno (rc)); DescribeErrno (rc));
END_CANCELATION_POINT;
return rc; return rc;
} }

View file

@ -39,7 +39,7 @@
int gettid(void) { int gettid(void) {
int tid; int tid;
if (VERY_LIKELY(__tls_enabled && !__vforked)) { if (VERY_LIKELY(__tls_enabled && !__vforked)) {
tid = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_acquire); tid = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
if (VERY_LIKELY(tid > 0)) if (VERY_LIKELY(tid > 0))
return tid; return tid;
} }

View file

@ -192,7 +192,7 @@ ABI void __maps_unlock(void) {
return; return;
if (tib->tib_flags & TIB_FLAG_VFORKED) if (tib->tib_flags & TIB_FLAG_VFORKED)
return; return;
me = atomic_load_explicit(&tib->tib_tid, memory_order_acquire); me = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
if (me <= 0) if (me <= 0)
return; return;
word = atomic_load_explicit(&__maps.lock.word, memory_order_relaxed); word = atomic_load_explicit(&__maps.lock.word, memory_order_relaxed);

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/blockcancel.internal.h" #include "libc/calls/blockcancel.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/state.internal.h" #include "libc/calls/state.internal.h"
#include "libc/cosmo.h" #include "libc/cosmo.h"
#include "libc/dce.h" #include "libc/dce.h"
@ -70,7 +69,7 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
uint64_t word, bool is_trylock) { uint64_t word, bool is_trylock) {
uint64_t lock; uint64_t lock;
int backoff = 0; int backoff = 0;
int me = gettid(); int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
bool once = false; bool once = false;
for (;;) { for (;;) {
if (MUTEX_OWNER(word) == me) { if (MUTEX_OWNER(word) == me) {
@ -120,7 +119,7 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
static errno_t pthread_mutex_lock_recursive_nsync(pthread_mutex_t *mutex, static errno_t pthread_mutex_lock_recursive_nsync(pthread_mutex_t *mutex,
uint64_t word, uint64_t word,
bool is_trylock) { bool is_trylock) {
int me = gettid(); int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
for (;;) { for (;;) {
if (MUTEX_OWNER(word) == me) { if (MUTEX_OWNER(word) == me) {
if (MUTEX_DEPTH(word) < MUTEX_DEPTH_MAX) { if (MUTEX_DEPTH(word) < MUTEX_DEPTH_MAX) {

View file

@ -30,6 +30,7 @@
#include "libc/thread/lock.h" #include "libc/thread/lock.h"
#include "libc/thread/posixthread.internal.h" #include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "third_party/nsync/mu.h" #include "third_party/nsync/mu.h"
// see "take 3" algorithm in "futexes are tricky" by ulrich drepper // see "take 3" algorithm in "futexes are tricky" by ulrich drepper
@ -43,7 +44,7 @@ static void pthread_mutex_unlock_drepper(atomic_int *futex, char pshare) {
static errno_t pthread_mutex_unlock_recursive(pthread_mutex_t *mutex, static errno_t pthread_mutex_unlock_recursive(pthread_mutex_t *mutex,
uint64_t word) { uint64_t word) {
int me = gettid(); int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
for (;;) { for (;;) {
// we allow unlocking an initialized lock that wasn't locked, but we // we allow unlocking an initialized lock that wasn't locked, but we
@ -75,7 +76,7 @@ static errno_t pthread_mutex_unlock_recursive(pthread_mutex_t *mutex,
#if PTHREAD_USE_NSYNC #if PTHREAD_USE_NSYNC
static errno_t pthread_mutex_unlock_recursive_nsync(pthread_mutex_t *mutex, static errno_t pthread_mutex_unlock_recursive_nsync(pthread_mutex_t *mutex,
uint64_t word) { uint64_t word) {
int me = gettid(); int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
for (;;) { for (;;) {
// we allow unlocking an initialized lock that wasn't locked, but we // we allow unlocking an initialized lock that wasn't locked, but we

View file

@ -81,7 +81,7 @@ errno_t pthread_setcancelstate(int state, int *oldstate) {
} }
err = 0; err = 0;
} }
#if IsModeDbg() #if IsModeDbg() && 0
STRACE("pthread_setcancelstate(%s, [%s]) → %s", STRACE("pthread_setcancelstate(%s, [%s]) → %s",
DescribeCancelState(0, &state), DescribeCancelState(err, oldstate), DescribeCancelState(0, &state), DescribeCancelState(err, oldstate),
DescribeErrno(err)); DescribeErrno(err));