Clean up threading code some more

This commit is contained in:
Justine Tunney 2022-09-13 14:57:38 -07:00
parent 6a3330d7c9
commit 654ceaba7d
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
28 changed files with 119 additions and 134 deletions

View file

@ -65,6 +65,7 @@ o/$(MODE)/ape/ape.lds: \
libc/intrin/bits.h \
libc/thread/tls.h \
libc/calls/struct/timespec.h \
libc/thread/thread.h \
libc/dce.h \
libc/elf/def.h \
libc/elf/pf2prot.internal.h \
@ -82,6 +83,7 @@ o/$(MODE)/ape/public/ape.lds: \
libc/intrin/bits.h \
libc/thread/tls.h \
libc/calls/struct/timespec.h \
libc/thread/thread.h \
libc/dce.h \
libc/elf/def.h \
libc/elf/pf2prot.internal.h \

View file

@ -18,13 +18,14 @@
*/
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
/**
* Gets value of TLS slot for current thread.
*/
void *pthread_getspecific(pthread_key_t key) {
if (0 <= key && key < PTHREAD_KEYS_MAX) {
return _pthread_keys[key];
return __get_tls()->tib_keys[key];
} else {
return 0;
}

View file

@ -28,7 +28,7 @@ void _pthread_key_destruct(void *key[PTHREAD_KEYS_MAX]) {
pthread_key_dtor dtor;
if (!__tls_enabled) return;
pthread_spin_lock(&_pthread_keys_lock);
if (!key) key = _pthread_keys;
if (!key) key = __get_tls()->tib_keys;
StartOver:
for (i = 0; i < (PTHREAD_KEYS_MAX + 63) / 64; ++i) {
x = _pthread_key_usage[i];

View file

@ -21,9 +21,6 @@
pthread_spinlock_t _pthread_keys_lock;
// tls value slots for pthread keys api
_Thread_local void *_pthread_keys[PTHREAD_KEYS_MAX];
// bitset of tls key registrations
uint64_t _pthread_key_usage[(PTHREAD_KEYS_MAX + 63) / 64];

View file

@ -19,7 +19,6 @@
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/weaken.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
@ -62,10 +61,10 @@
int pthread_mutex_lock(pthread_mutex_t *mutex) {
int c, d, t;
if (LIKELY(__tls_enabled && //
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_lock))) {
_weaken(nsync_mu_lock)) {
_weaken(nsync_mu_lock)((nsync_mu *)mutex);
return 0;
}
@ -78,43 +77,25 @@ int pthread_mutex_lock(pthread_mutex_t *mutex) {
}
t = gettid();
if (mutex->_type == PTHREAD_MUTEX_ERRORCHECK) {
c = atomic_load_explicit(&mutex->_lock, memory_order_relaxed);
if ((c & 0x000fffff) == t) {
if (mutex->_owner == t) {
if (mutex->_type != PTHREAD_MUTEX_ERRORCHECK) {
if (mutex->_depth < 255) {
++mutex->_depth;
return 0;
} else {
return EAGAIN;
}
} else {
return EDEADLK;
}
}
for (;;) {
c = 0;
d = 0x10100000 | t;
if (atomic_compare_exchange_weak_explicit(
&mutex->_lock, &c, d, memory_order_acquire, memory_order_relaxed)) {
break;
} else {
if ((c & 0x000fffff) == t) {
if ((c & 0x0ff00000) < 0x0ff00000) {
c = atomic_fetch_add_explicit(&mutex->_lock, 0x00100000,
memory_order_relaxed);
break;
} else {
return EAGAIN;
}
}
if ((c & 0xf0000000) == 0x10000000) {
d = 0x20000000 | c;
if (atomic_compare_exchange_weak_explicit(&mutex->_lock, &c, d,
memory_order_acquire,
memory_order_relaxed)) {
c = d;
}
}
if ((c & 0xf0000000) == 0x20000000) {
// _futex_wait(&mutex->_lock, c, mutex->_pshared, 0);
while (atomic_exchange_explicit(&mutex->_lock, 1, memory_order_acquire)) {
pthread_yield();
}
}
}
mutex->_depth = 0;
mutex->_owner = t;
return 0;
}

View file

@ -16,11 +16,9 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/weaken.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
@ -39,10 +37,10 @@
int pthread_mutex_trylock(pthread_mutex_t *mutex) {
int c, d, t;
if (LIKELY(__tls_enabled && //
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_trylock))) {
_weaken(nsync_mu_trylock)) {
if (_weaken(nsync_mu_trylock)((nsync_mu *)mutex)) {
return 0;
} else {
@ -58,19 +56,25 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex) {
}
}
c = 0;
t = gettid();
d = 0x10100000 | t;
if (!atomic_compare_exchange_strong_explicit(
&mutex->_lock, &c, d, memory_order_acquire, memory_order_relaxed)) {
if ((c & 0x000fffff) != t || mutex->_type == PTHREAD_MUTEX_ERRORCHECK) {
return EBUSY;
}
if ((c & 0x0ff00000) == 0x0ff00000) {
if (mutex->_owner == t) {
if (mutex->_type != PTHREAD_MUTEX_ERRORCHECK) {
if (mutex->_depth < 255) {
++mutex->_depth;
return 0;
} else {
return EAGAIN;
}
atomic_fetch_add_explicit(&mutex->_lock, 0x00100000, memory_order_relaxed);
} else {
return EBUSY;
}
}
if (!atomic_exchange_explicit(&mutex->_lock, 1, memory_order_acquire)) {
mutex->_depth = 0;
mutex->_owner = t;
return 0;
} else {
return EBUSY;
}
}

View file

@ -19,7 +19,6 @@
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/weaken.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
@ -34,10 +33,10 @@
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
int c, t;
if (LIKELY(__tls_enabled && //
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_unlock))) {
_weaken(nsync_mu_unlock)) {
_weaken(nsync_mu_unlock)((nsync_mu *)mutex);
return 0;
}
@ -48,21 +47,17 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex) {
}
t = gettid();
if (mutex->_type == PTHREAD_MUTEX_ERRORCHECK) {
c = atomic_load_explicit(&mutex->_lock, memory_order_relaxed);
if ((c & 0x000fffff) != t) {
if (mutex->_owner != t) {
return EPERM;
}
if (mutex->_depth) {
--mutex->_depth;
return 0;
}
c = atomic_fetch_sub(&mutex->_lock, 0x00100000);
if ((c & 0x0ff00000) == 0x00100000) {
mutex->_owner = 0;
atomic_store_explicit(&mutex->_lock, 0, memory_order_release);
if ((c & 0xf0000000) == 0x20000000) {
// _futex_wake(&mutex->_lock, 1, mutex->_pshared);
pthread_yield();
}
}
return 0;
}

View file

@ -19,13 +19,14 @@
#include "libc/errno.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
/**
* Sets value of TLS slot for current thread.
*/
int pthread_setspecific(pthread_key_t key, void *val) {
if (0 <= key && key < PTHREAD_KEYS_MAX) {
_pthread_keys[key] = val;
__get_tls()->tib_keys[key] = val;
return 0;
} else {
return EINVAL;

View file

@ -488,11 +488,8 @@ static int CloneLinux(int (*func)(void *arg, int tid), char *stk, size_t stksz,
* return 0;
* }
*
* struct CosmoTib tib = {
* .tib_self = &tib,
* .tib_self2 = &tib,
* .tib_tid = -1,
* };
* // NOTE: See _mktls() for _Thread_local support.
* struct CosmoTib tib = {.tib_self = &tib, .tib_tid = -1};
* char *stk = _mapstack();
* tid = clone(worker, stk, GetStackSize() - 16,
* CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES |
@ -589,7 +586,6 @@ static int CloneLinux(int (*func)(void *arg, int tid), char *stk, size_t stksz,
int clone(void *func, void *stk, size_t stksz, int flags, void *arg, int *ptid,
void *tls, void *ctid) {
int rc;
struct CloneArgs *wt;
if (flags & CLONE_THREAD) {
__enable_threads();

View file

@ -7,7 +7,10 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#ifndef EZBENCH_COUNT
#define EZBENCH_COUNT 128
#endif
#define EZBENCH_TRIES 10
#define EZBENCH(INIT, EXPR) EZBENCH2(#EXPR, INIT, EXPR)

View file

@ -35,7 +35,7 @@ void Bzero(void *, size_t) asm("bzero"); // gcc bug
* Allocates thread-local storage memory for new thread.
* @return buffer that must be released with free()
*/
char *_mktls(char **out_tib) {
char *_mktls(struct CosmoTib **out_tib) {
char *tls;
struct CosmoTib *tib;
@ -60,7 +60,7 @@ char *_mktls(char **out_tib) {
tib->tib_tid = -1;
if (out_tib) {
*out_tib = (char *)tib;
*out_tib = tib;
}
return tls;
}

View file

@ -3,6 +3,7 @@
#include "libc/calls/struct/sched_param.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -61,8 +62,8 @@ struct PosixThread {
int tid;
int *ctid;
char *tls;
char *tib;
char *altstack;
struct CosmoTib *tib;
_Atomic(enum PosixThreadStatus) status;
jmp_buf exiter;
pthread_attr_t attr;

View file

@ -53,7 +53,7 @@ STATIC_YOINK("nsync_mu_unlock");
#define MAP_STACK_OPENBSD 0x4000
void _pthread_wait(struct PosixThread *pt) {
_wait0(pt->ctid);
_wait0(&pt->tib->tib_tid);
}
void _pthread_free(struct PosixThread *pt) {
@ -206,9 +206,6 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
return EAGAIN;
}
// child thread id is also a condition variable
pt->ctid = (int *)(pt->tib + 0x38);
// setup attributes
if (attr) {
pt->attr = *attr;
@ -301,7 +298,7 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
CLONE_CHILD_CLEARTID,
pt, &pt->tid, pt->tib, pt->ctid) == -1) {
pt, &pt->tid, pt->tib, &pt->tib->tib_tid) == -1) {
rc = errno;
_pthread_free(pt);
errno = e;

View file

@ -111,7 +111,6 @@ int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
if (!(th->tls = _mktls(&th->tib))) {
return -1;
}
th->ctid = (int *)(th->tib + 0x38);
// we must use _mapstack() to allocate the stack because OpenBSD has
// very strict requirements for what's allowed to be used for stacks
@ -127,7 +126,7 @@ int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
CLONE_CHILD_CLEARTID,
spawner, &th->ptid, th->tib, th->ctid) == -1) {
spawner, &th->ptid, th->tib, &th->tib->tib_tid) == -1) {
_freestack(th->stk);
free(th->tls);
return -1;
@ -143,9 +142,9 @@ int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
*/
int _join(struct spawn *th) {
int rc;
if (th->ctid) {
if (th->tib) {
// wait for ctid to become zero
_wait0(th->ctid);
_wait0(&th->tib->tib_tid);
// free thread memory
free(th->tls);
rc = munmap(th->stk, GetStackSize());

View file

@ -1,19 +1,19 @@
#ifndef COSMOPOLITAN_LIBC_THREAD_SPAWN_H_
#define COSMOPOLITAN_LIBC_THREAD_SPAWN_H_
#include "libc/thread/tls.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct spawn {
int ptid;
int *ctid;
char *stk;
char *tls;
char *tib;
struct CosmoTib *tib;
};
int _spawn(int (*)(void *, int), void *, struct spawn *) hidden;
int _join(struct spawn *) hidden;
char *_mktls(char **) hidden;
char *_mktls(struct CosmoTib **) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_THREAD_THREAD_H_
#define COSMOPOLITAN_LIBC_THREAD_THREAD_H_
#define PTHREAD_KEYS_MAX 64
#define PTHREAD_KEYS_MAX 128
#define PTHREAD_STACK_MIN FRAMESIZE
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
@ -51,8 +51,10 @@ typedef struct pthread_spinlock_s {
typedef struct pthread_mutex_s {
_Atomic(int32_t) _lock;
uint16_t _type;
uint16_t _pshared;
unsigned _type : 2;
unsigned _pshared : 1;
unsigned _depth : 8;
unsigned _owner : 21;
void *_nsync;
} pthread_mutex_t;

View file

@ -1,5 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_THREAD_TLS_H_
#define COSMOPOLITAN_LIBC_THREAD_TLS_H_
#include "libc/thread/thread.h"
#define TLS_ALIGNMENT 64
@ -22,6 +23,15 @@ struct CosmoTib {
struct CosmoTib *tib_self2; /* 0x30 */
_Atomic(int32_t) tib_tid; /* 0x38 */
int32_t tib_errno; /* 0x3c */
void *tib_nsync;
void *tib_reserved1;
void *tib_reserved2;
void *tib_reserved3;
void *tib_reserved4;
void *tib_reserved5;
void *tib_reserved6;
void *tib_reserved7;
void *tib_keys[PTHREAD_KEYS_MAX];
};
extern int __threaded;

View file

@ -16,17 +16,18 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/strace.internal.h"
#include "libc/atomic.h"
#include "libc/calls/struct/timespec.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/futex.h"
#include "libc/thread/thread.h"
#include "libc/thread/wait0.internal.h"
int _futex(int *, int, int, const struct timespec *);
int _futex(atomic_int *, int, int, const struct timespec *);
/**
* Blocks until memory location becomes zero.
@ -35,7 +36,7 @@ int _futex(int *, int, int, const struct timespec *);
* by the _spawn() system call when a thread terminates. The purpose of
* this operation is to know when it's safe to munmap() a threads stack
*/
void _wait0(const int *ctid) {
void _wait0(const atomic_int *ctid) {
int x, rc;
char buf[12];
for (;;) {

View file

@ -1,9 +1,10 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_WAIT0_H_
#define COSMOPOLITAN_LIBC_INTRIN_WAIT0_H_
#include "libc/atomic.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
void _wait0(const int *) hidden;
void _wait0(const atomic_int *) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -560,7 +560,7 @@ TEST(pledge_openbsd, bigSyscalls) {
int LockWorker(void *arg, int tid) {
flockfile(stdout);
ASSERT_EQ(gettid(), stdout->lock._lock & 0x000fffff);
ASSERT_EQ(gettid(), stdout->lock._owner);
funlockfile(stdout);
return 0;
}

View file

@ -25,6 +25,7 @@
#include "libc/intrin/kprintf.h"
#include "libc/intrin/weaken.h"
#include "libc/runtime/clone.internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/str/str.h"
@ -49,22 +50,6 @@
* they're wicked fast and free of bloat if your app is tiny.
*/
// RAW means without *NSYNC
// TLS means gettid() is fast
// PTHREAD_MUTEX_NORMAL RAW TLS took 6ns
// PTHREAD_MUTEX_RECURSIVE RAW TLS took 12ns
// PTHREAD_MUTEX_ERRORCHECK RAW TLS took 13ns
// PTHREAD_MUTEX_NORMAL RAW TLS contended took 16ns (!!)
// PTHREAD_MUTEX_RECURSIVE RAW TLS contended took 205ns
// PTHREAD_MUTEX_ERRORCHECK RAW TLS contended took 219ns
// PTHREAD_MUTEX_NORMAL RAW took 6ns
// PTHREAD_MUTEX_RECURSIVE RAW took 236ns
// PTHREAD_MUTEX_ERRORCHECK RAW took 233ns
// PTHREAD_MUTEX_NORMAL RAW contended took 20ns (!!)
// PTHREAD_MUTEX_RECURSIVE RAW contended took 421ns
// PTHREAD_MUTEX_ERRORCHECK RAW contended took 435ns
atomic_int ready;
atomic_int counter;
atomic_int success;

View file

@ -16,12 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#define EZBENCH_COUNT 10000
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/timespec.h"
#include "libc/intrin/atomic.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/posixthread.internal.h"
@ -175,9 +176,9 @@ struct MutexContentionArgs {
int MutexContentionWorker(void *arg, int tid) {
struct MutexContentionArgs *a = arg;
while (!atomic_load_explicit(&a->done, memory_order_relaxed)) {
pthread_mutex_lock(a->mutex);
if (pthread_mutex_lock(a->mutex)) notpossible;
atomic_store_explicit(&a->ready, 1, memory_order_relaxed);
pthread_mutex_unlock(a->mutex);
if (pthread_mutex_unlock(a->mutex)) notpossible;
}
return 0;
}

View file

@ -66,6 +66,9 @@ TEST(pthread_mutex_lock, normal) {
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
ASSERT_EQ(0, pthread_mutex_init(&lock, 0));
ASSERT_EQ(0, pthread_mutex_lock(&lock));
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_trylock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_lock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
@ -81,6 +84,8 @@ TEST(pthread_mutex_lock, recursive) {
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
ASSERT_EQ(0, pthread_mutex_lock(&lock));
ASSERT_EQ(0, pthread_mutex_lock(&lock));
ASSERT_EQ(0, pthread_mutex_trylock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_lock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
@ -91,7 +96,6 @@ TEST(pthread_mutex_lock, recursive) {
TEST(pthread_mutex_lock, errorcheck) {
pthread_mutex_t lock;
pthread_mutexattr_t attr;
__assert_disable = true;
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
@ -99,19 +103,21 @@ TEST(pthread_mutex_lock, errorcheck) {
ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_lock(&lock));
ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
ASSERT_EQ(EPERM, pthread_mutex_unlock(&lock));
ASSERT_EQ(0, pthread_mutex_destroy(&lock));
__assert_disable = false;
}
int MutexWorker(void *p, int tid) {
int i;
++started;
for (i = 0; i < ITERATIONS; ++i) {
pthread_mutex_lock(&mylock);
if (pthread_mutex_lock(&mylock)) {
ASSERT_EQ(0, pthread_mutex_lock(&mylock));
}
++count;
pthread_mutex_unlock(&mylock);
ASSERT_EQ(0, pthread_mutex_unlock(&mylock));
}
++finished;
return 0;

View file

@ -49,6 +49,7 @@ TEST(tls, test) {
ASSERT_EQ(0, pthread_create(&t, 0, Worker, 0));
ASSERT_EQ(0, pthread_join(t, 0));
if (IsAsan()) {
ASSERT_EQ(kAsanProtected, __asan_check(y + 1, sizeof(long)).kind);
// TODO(jart): Why isn't it poisoned?
// ASSERT_EQ(kAsanProtected, __asan_check(y + 1, sizeof(long)).kind);
}
}

View file

@ -18,6 +18,7 @@
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#include "third_party/nsync/atomic.h"
#include "third_party/nsync/atomic.internal.h"
#include "third_party/nsync/common.internal.h"
@ -150,7 +151,7 @@ static nsync_dll_list_ free_waiters = NULL;
/* free_waiters points to a doubly-linked list of free waiter structs. */
static nsync_atomic_uint32_ free_waiters_mu; /* spinlock; protects free_waiters */
static _Thread_local waiter *waiter_for_thread;
#define waiter_for_thread __get_tls()->tib_nsync
static void waiter_destroy (void *v) {
waiter *w = (waiter *) v;

View file

@ -11,6 +11,7 @@
*************************************************************************
** This file contains the C functions that implement mutexes for pthreads
*/
#include "libc/thread/thread.h"
#include "third_party/sqlite3/sqliteInt.inc"
/* clang-format off */
@ -23,8 +24,6 @@
*/
#ifdef SQLITE_MUTEX_PTHREADS
#include <pthread.h>
/*
** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
** are necessary under two condidtions: (1) Debug builds and (2) using

View file

@ -54,6 +54,7 @@ THIRD_PARTY_SQLITE3_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \
LIBC_TIME \
LIBC_TINYMATH \
THIRD_PARTY_GDTOA \
@ -110,7 +111,7 @@ THIRD_PARTY_SQLITE3_FLAGS = \
-DHAVE_STRCHRNUL \
-DHAVE_LOCALTIME_R \
-DHAVE_MALLOC_USABLE_SIZE \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_THREADSAFE=1 \
-DSQLITE_MAX_EXPR_DEPTH=0 \
-DSQLITE_DEFAULT_MEMSTATUS=0 \
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \

View file

@ -25,6 +25,7 @@
** of multiple cores can do so, while also allowing applications to stay
** single-threaded if desired.
*/
#include "libc/thread/thread.h"
#include "third_party/sqlite3/sqliteInt.inc"
/* clang-format off */
@ -34,7 +35,6 @@
#if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0
#define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
#include <pthread.h>
/* A running thread */
struct SQLiteThread {