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

@ -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 && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_lock))) {
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_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);
pthread_yield();
}
}
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 && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_trylock))) {
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_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) {
if (mutex->_owner == t) {
if (mutex->_type != PTHREAD_MUTEX_ERRORCHECK) {
if (mutex->_depth < 255) {
++mutex->_depth;
return 0;
} else {
return EAGAIN;
}
} else {
return EBUSY;
}
if ((c & 0x0ff00000) == 0x0ff00000) {
return EAGAIN;
}
atomic_fetch_add_explicit(&mutex->_lock, 0x00100000, memory_order_relaxed);
}
return 0;
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 && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_unlock))) {
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_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) {
return EPERM;
}
if (mutex->_owner != t) {
return EPERM;
}
c = atomic_fetch_sub(&mutex->_lock, 0x00100000);
if ((c & 0x0ff00000) == 0x00100000) {
atomic_store_explicit(&mutex->_lock, 0, memory_order_release);
if ((c & 0xf0000000) == 0x20000000) {
// _futex_wake(&mutex->_lock, 1, mutex->_pshared);
pthread_yield();
}
if (mutex->_depth) {
--mutex->_depth;
return 0;
}
mutex->_owner = 0;
atomic_store_explicit(&mutex->_lock, 0, memory_order_release);
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) */