Add pthread attributes and other libc functions

This commit is contained in:
Justine Tunney 2022-09-07 05:23:44 -07:00
parent d5c9308a43
commit 4339d9f15e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
81 changed files with 1111 additions and 428 deletions

View file

@ -1,5 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_
#define COSMOPOLITAN_LIBC_THREAD_POSIXTHREAD_INTERNAL_H_
#include "libc/intrin/pthread.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/spawn.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
@ -10,7 +11,7 @@ COSMOPOLITAN_C_START_
*/
enum PosixThreadStatus {
kPosixThreadStarted,
kPosixThreadJoinable,
kPosixThreadDetached,
kPosixThreadTerminated,
kPosixThreadZombie,
@ -19,16 +20,19 @@ enum PosixThreadStatus {
struct PosixThread {
struct spawn spawn;
void *(*start_routine)(void *);
void *arg;
void *rc;
int tid;
void *arg; // start_routine's parameter
void *rc; // start_routine's return value
_Atomic(enum PosixThreadStatus) status;
jmp_buf exiter;
size_t stacksize;
pthread_attr_t attr;
};
void pthread_zombies_add(struct PosixThread *);
void pthread_zombies_decimate(void);
void pthread_zombies_harvest(void);
void pthread_free(struct PosixThread *) hidden;
void pthread_wait(struct PosixThread *) hidden;
void pthread_zombies_add(struct PosixThread *) hidden;
void pthread_zombies_decimate(void) hidden;
void pthread_zombies_harvest(void) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -0,0 +1,38 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/pthread.h"
#include "libc/runtime/stack.h"
/**
* Returns configuration for thread stack.
*
* This is a getter for a configuration attribute. By default, zeros are
* returned. If pthread_attr_setstack() was called earlier, then this'll
* return those earlier supplied values.
*
* @param stackaddr will be set to stack address in bytes
* @return 0 on success, or errno on error
* @see pthread_attr_setstacksize()
*/
int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr,
size_t *stacksize) {
*stackaddr = attr->stackaddr;
*stacksize = attr->stacksize;
return 0;
}

View file

@ -0,0 +1,106 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
#include "libc/runtime/stack.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#define MAP_ANON_OPENBSD 0x1000
#define MAP_STACK_OPENBSD 0x4000
/**
* Configures custom allocated stack for thread, e.g.
*
* pthread_t id;
* pthread_attr_t attr;
* pthread_attr_init(&attr);
* pthread_attr_setstack(&attr, gc(malloc(GetStackSize())),
* GetStackSize());
* pthread_create(&id, &attr, func, 0);
* pthread_attr_destroy(&attr);
* pthread_join(id, 0);
*
* Your stack must have at least `PTHREAD_STACK_MIN` bytes, which
* Cosmpolitan Libc defines as `GetStackSize()`. It's a link-time
* constant used by Actually Portable Executable that's 128 kb by
* default. See libc/runtime/stack.h for docs on your stack limit
* since the APE ELF phdrs are the one true source of truth here.
*
* Cosmpolitan Libc runtime magic (e.g. ftrace) and memory safety
* (e.g. kprintf) assumes that stack sizes are two-powers and are
* aligned to that two-power. Conformance isn't required since we
* say caveat emptor to those who don't maintain these invariants
*
* Unlike pthread_attr_setstacksize(), this function permits just
* about any parameters and will change the values and allocation
* as needed to conform to the mandatory requirements of the host
* operating system.
*
* @param stackaddr is address of stack allocated by caller, and
* may be NULL in which case default behavior is restored
* @param stacksize is size of caller allocated stack
* @return 0 on success, or errno on error
* @raise EINVAL if parameters were unacceptable
* @see pthread_attr_setstacksize()
*/
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr,
size_t stacksize) {
if (!stackaddr) {
attr->stackaddr = 0;
attr->stacksize = 0;
return 0;
}
if (stacksize < PTHREAD_STACK_MIN ||
(IsAsan() && !__asan_is_valid(stackaddr, stacksize))) {
return EINVAL;
}
if (IsOpenbsd()) {
// OpenBSD: Only permits RSP to occupy memory that's been explicitly
// defined as stack memory. We need to squeeze the provided interval
// in order to successfully call mmap(), which will return EINVAL if
// these calculations should overflow.
size_t n;
int e, rc;
uintptr_t x, y;
n = stacksize;
x = (uintptr_t)stackaddr;
y = ROUNDUP(x, PAGESIZE);
n -= y - x;
n = ROUNDDOWN(n, PAGESIZE);
stackaddr = (void *)y;
stacksize = n;
e = errno;
if (__sys_mmap(stackaddr, stacksize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON_OPENBSD | MAP_STACK_OPENBSD, -1, 0,
0) == MAP_FAILED) {
rc = errno;
errno = e;
return rc;
}
}
attr->stackaddr = stackaddr;
attr->stacksize = stacksize;
return 0;
}

View file

@ -78,11 +78,7 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
tsp = &rel;
}
if (IsLinux() || IsOpenbsd()) {
if (cond->attr == PTHREAD_PROCESS_SHARED) {
_futex_wait_public(&cond->seq, seq, tsp);
} else {
_futex_wait_private(&cond->seq, seq, tsp);
}
_futex_wait(&cond->seq, seq, cond->pshared, tsp);
} else {
sched_yield();
}

View file

@ -16,19 +16,37 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/pthread.h"
#include "libc/intrin/wait0.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/gettls.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/clone.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/spawn.h"
#include "libc/thread/thread.h"
void pthread_wait(struct PosixThread *pt) {
_wait0(pt->spawn.ctid);
}
void pthread_free(struct PosixThread *pt) {
free(pt->spawn.tls);
if (pt->stacksize) {
munmap(&pt->spawn.stk, pt->stacksize);
}
free(pt);
}
static int PosixThread(void *arg, int tid) {
struct PosixThread *pt = arg;
enum PosixThreadStatus status;
pt->tid = tid;
if (!setjmp(pt->exiter)) {
((cthread_t)__get_tls())->pthread = pt;
pt->rc = pt->start_routine(pt->arg);
@ -66,27 +84,128 @@ static int PosixThread(void *arg, int tid) {
* bsdthread_create thr_new
*
*
* @param thread if non-null is used to output the thread id
* upon successful completion
* @param attr points to launch configuration, or may be null
* to use sensible defaults; it must be initialized using
* pthread_attr_init()
* @param start_routine is your thread's callback function
* @param arg is an arbitrary value passed to `start_routine`
* @return 0 on success, or errno on error
* @raise EAGAIN if resources to create thread weren't available
* @raise EINVAL if `attr` was supplied and had unnaceptable data
* @raise EPERM if scheduling policy was requested and user account
* isn't authorized to use it
*/
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg) {
int e, rc;
int rc, e = errno;
struct PosixThread *pt;
e = errno;
pthread_attr_t default_attr;
pthread_zombies_decimate();
if ((pt = calloc(1, sizeof(struct PosixThread)))) {
pt->start_routine = start_routine;
pt->arg = arg;
if (!_spawn(PosixThread, pt, &pt->spawn)) {
*thread = pt;
rc = 0;
} else {
free(pt);
rc = errno;
}
} else {
rc = errno;
// default attributes
if (!attr) {
pthread_attr_init(&default_attr);
attr = &default_attr;
}
errno = e;
return rc;
// create posix thread object
if (!(pt = calloc(1, sizeof(struct PosixThread)))) {
errno = e;
return EAGAIN;
}
pt->start_routine = start_routine;
pt->arg = arg;
// create thread local storage memory
if (!(pt->spawn.tls = _mktls(&pt->spawn.tib))) {
free(pt);
errno = e;
return EAGAIN;
}
// child thread id is also a condition variable
pt->spawn.ctid = (int *)(pt->spawn.tib + 0x38);
// create stack
if (attr && attr->stackaddr) {
// caller is responsible for creating stacks
pt->spawn.stk = attr->stackaddr;
} else {
// cosmo posix threads is managing the stack
pt->spawn.stk = mmap(0, attr->stacksize, PROT_READ | PROT_WRITE,
MAP_STACK | MAP_ANONYMOUS, -1, 0);
if (pt->spawn.stk != MAP_FAILED) {
pt->stacksize = attr->stacksize;
} else {
rc = errno;
pthread_free(pt);
errno = e;
if (rc == EINVAL || rc == EOVERFLOW) {
return EINVAL;
} else {
return EAGAIN;
}
}
// mmap(MAP_STACK) creates a 4096 guard by default
if (attr->guardsize != PAGESIZE) {
// user requested special guard size
if (attr->guardsize) {
rc = mprotect(pt->spawn.stk, attr->guardsize, PROT_NONE);
} else {
rc = mprotect(pt->spawn.stk, PAGESIZE, PROT_READ | PROT_WRITE);
}
if (rc) {
notpossible;
}
}
if (IsAsan()) {
if (attr->guardsize) {
__asan_poison(pt->spawn.stk, attr->guardsize, kAsanStackOverflow);
}
__asan_poison(
pt->spawn.stk + attr->stacksize - 16 /* openbsd:stackbound */, 16,
kAsanStackOverflow);
}
}
// save the attributes for descriptive purposes
pt->attr = *attr;
// set initial status
switch (attr->detachstate) {
case PTHREAD_CREATE_JOINABLE:
pt->status = kPosixThreadJoinable;
break;
case PTHREAD_CREATE_DETACHED:
pt->status = kPosixThreadDetached;
pthread_zombies_add(pt);
break;
default:
pthread_free(pt);
return EINVAL;
}
// launch PosixThread(pt) in new thread
if (clone(PosixThread, pt->spawn.stk,
attr->stacksize - 16 /* openbsd:stackbound */,
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_SETTID |
CLONE_CHILD_CLEARTID,
pt, &pt->spawn.ptid, pt->spawn.tib, pt->spawn.ctid) == -1) {
rc = errno;
pthread_free(pt);
errno = e;
if (rc == EINVAL) {
return EINVAL;
} else {
return EAGAIN;
}
}
if (thread) {
*thread = pt;
}
return 0;
}

View file

@ -36,10 +36,10 @@ int pthread_detach(pthread_t thread) {
if (status == kPosixThreadDetached || status == kPosixThreadZombie) {
break;
} else if (status == kPosixThreadTerminated) {
_join(&pt->spawn);
free(pt);
pthread_wait(pt);
pthread_free(pt);
break;
} else if (status == kPosixThreadStarted &&
} else if (status == kPosixThreadJoinable &&
atomic_compare_exchange_weak_explicit(
&pt->status, &status, kPosixThreadDetached,
memory_order_acquire, memory_order_relaxed)) {

View file

@ -27,5 +27,5 @@
int pthread_equal(pthread_t t1, pthread_t t2) {
struct PosixThread *a = t1;
struct PosixThread *b = t2;
return a->tid == b->tid;
return a->spawn.ptid == b->spawn.ptid;
}

View file

@ -0,0 +1,28 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/pthread.h"
#include "libc/str/str.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr) {
struct PosixThread *pt = thread;
memcpy(attr, &pt->attr, sizeof(pt->attr));
return 0;
}

View file

@ -24,5 +24,5 @@
*/
int64_t pthread_getunique_np(pthread_t thread) {
struct PosixThread *pt = thread;
return pt->tid;
return pt->spawn.ptid;
}

View file

@ -36,10 +36,10 @@ int pthread_join(pthread_t thread, void **value_ptr) {
assert(!"badjoin");
return EDEADLK;
}
_join(&pt->spawn);
pthread_wait(pt);
if (value_ptr) {
*value_ptr = pt->rc;
}
free(pt);
pthread_free(pt);
return 0;
}

View file

@ -22,12 +22,13 @@
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/intrin/pthread.h"
#include "libc/thread/thread.h"
int cthread_memory_wait32(int* addr, int val, const struct timespec* timeout) {
size_t size;
if (IsLinux() || IsOpenbsd()) {
return _futex_wait_public(addr, val, timeout);
return _futex_wait(addr, val, PTHREAD_PROCESS_SHARED, timeout);
} else {
return sched_yield();
}
@ -35,7 +36,7 @@ int cthread_memory_wait32(int* addr, int val, const struct timespec* timeout) {
int cthread_memory_wake32(int* addr, int n) {
if (IsLinux() || IsOpenbsd()) {
return _futex_wake_public(addr, n);
return _futex_wake(addr, n, PTHREAD_PROCESS_SHARED);
} else {
return 0;
}

View file

@ -44,9 +44,9 @@ void pthread_zombies_add(struct PosixThread *pt) {
}
}
void pthread_zombies_destroy(struct Zombie *z) {
_join(&z->pt->spawn);
free(z->pt);
static void pthread_zombies_collect(struct Zombie *z) {
pthread_wait(z->pt);
pthread_free(z->pt);
free(z);
}
@ -55,7 +55,7 @@ void pthread_zombies_decimate(void) {
while ((z = atomic_load(&pthread_zombies)) &&
atomic_load(&z->pt->status) == kPosixThreadZombie) {
if (atomic_compare_exchange_strong(&pthread_zombies, &z, z->next)) {
pthread_zombies_destroy(z);
pthread_zombies_collect(z);
}
}
}
@ -64,7 +64,7 @@ void pthread_zombies_harvest(void) {
struct Zombie *z;
while ((z = atomic_load(&pthread_zombies))) {
if (atomic_compare_exchange_weak(&pthread_zombies, &z, z->next)) {
pthread_zombies_destroy(z);
pthread_zombies_collect(z);
}
}
}