Skip to content

Commit c7e3d9f

Browse files
committed
Make recursive mutexes slightly faster
1 parent 9ba5b22 commit c7e3d9f

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

libc/intrin/cosmo_futex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/assert.h"
2020
#include "libc/atomic.h"
21+
#include "libc/calls/cp.internal.h"
2122
#include "libc/calls/internal.h"
2223
#include "libc/calls/sig.internal.h"
2324
#include "libc/calls/struct/sigset.h"
@@ -232,6 +233,7 @@ static int cosmo_futex_fix_timeout (struct timespec *memory, int clock,
232233
* @raise EAGAIN if `*w` wasn't `expect`
233234
* @raise EINTR if a signal handler was called while waiting
234235
* @raise ECANCELED if calling thread was canceled while waiting
236+
* @cancelationpoint
235237
*/
236238
int cosmo_futex_wait (atomic_int *w, int expect, char pshare,
237239
int clock, const struct timespec *abstime) {
@@ -240,6 +242,7 @@ int cosmo_futex_wait (atomic_int *w, int expect, char pshare,
240242
struct PosixThread *pt;
241243
struct timespec tsmem;
242244
struct timespec *timeout = 0;
245+
BEGIN_CANCELATION_POINT;
243246

244247
cosmo_once (&g_cosmo_futex.once, cosmo_futex_init);
245248

@@ -351,6 +354,7 @@ int cosmo_futex_wait (atomic_int *w, int expect, char pshare,
351354
DescribeTimespec (0, abstime),
352355
DescribeErrno (rc));
353356

357+
END_CANCELATION_POINT;
354358
return rc;
355359
}
356360

libc/intrin/gettid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
int gettid(void) {
4040
int tid;
4141
if (VERY_LIKELY(__tls_enabled && !__vforked)) {
42-
tid = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_acquire);
42+
tid = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
4343
if (VERY_LIKELY(tid > 0))
4444
return tid;
4545
}

libc/intrin/maps.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ ABI void __maps_unlock(void) {
192192
return;
193193
if (tib->tib_flags & TIB_FLAG_VFORKED)
194194
return;
195-
me = atomic_load_explicit(&tib->tib_tid, memory_order_acquire);
195+
me = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
196196
if (me <= 0)
197197
return;
198198
word = atomic_load_explicit(&__maps.lock.word, memory_order_relaxed);

libc/intrin/pthread_mutex_lock.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/blockcancel.internal.h"
20-
#include "libc/calls/calls.h"
2120
#include "libc/calls/state.internal.h"
2221
#include "libc/cosmo.h"
2322
#include "libc/dce.h"
@@ -70,7 +69,7 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
7069
uint64_t word, bool is_trylock) {
7170
uint64_t lock;
7271
int backoff = 0;
73-
int me = gettid();
72+
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
7473
bool once = false;
7574
for (;;) {
7675
if (MUTEX_OWNER(word) == me) {
@@ -120,7 +119,7 @@ static errno_t pthread_mutex_lock_recursive(pthread_mutex_t *mutex,
120119
static errno_t pthread_mutex_lock_recursive_nsync(pthread_mutex_t *mutex,
121120
uint64_t word,
122121
bool is_trylock) {
123-
int me = gettid();
122+
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
124123
for (;;) {
125124
if (MUTEX_OWNER(word) == me) {
126125
if (MUTEX_DEPTH(word) < MUTEX_DEPTH_MAX) {

libc/intrin/pthread_mutex_unlock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "libc/thread/lock.h"
3131
#include "libc/thread/posixthread.internal.h"
3232
#include "libc/thread/thread.h"
33+
#include "libc/thread/tls.h"
3334
#include "third_party/nsync/mu.h"
3435

3536
// 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) {
4344

4445
static errno_t pthread_mutex_unlock_recursive(pthread_mutex_t *mutex,
4546
uint64_t word) {
46-
int me = gettid();
47+
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
4748
for (;;) {
4849

4950
// 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,
7576
#if PTHREAD_USE_NSYNC
7677
static errno_t pthread_mutex_unlock_recursive_nsync(pthread_mutex_t *mutex,
7778
uint64_t word) {
78-
int me = gettid();
79+
int me = atomic_load_explicit(&__get_tls()->tib_tid, memory_order_relaxed);
7980
for (;;) {
8081

8182
// we allow unlocking an initialized lock that wasn't locked, but we

libc/intrin/pthread_setcancelstate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ errno_t pthread_setcancelstate(int state, int *oldstate) {
8181
}
8282
err = 0;
8383
}
84-
#if IsModeDbg()
84+
#if IsModeDbg() && 0
8585
STRACE("pthread_setcancelstate(%s, [%s]) → %s",
8686
DescribeCancelState(0, &state), DescribeCancelState(err, oldstate),
8787
DescribeErrno(err));

0 commit comments

Comments
 (0)