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

@ -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) */