mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 19:28:29 +00:00
Make fixes and improvements
- clock_nanosleep() is now much faster on OpenBSD and NetBSD - Thread joining is now much faster on NetBSD - FreeBSD timestamps are now more accurate - Thread spawning now goes faster on XNU - Clean up the clone() code
This commit is contained in:
parent
aee50b1327
commit
b407327972
47 changed files with 645 additions and 306 deletions
|
@ -66,7 +66,7 @@ struct PosixThread {
|
|||
int flags; // 0x00: see PT_* constants
|
||||
_Atomic(int) cancelled; // 0x04: thread has bad beliefs
|
||||
_Atomic(enum PosixThreadStatus) status;
|
||||
int tid; // clone parent tid
|
||||
_Atomic(int) ptid; // transitions 0 → tid
|
||||
void *(*start)(void *); // creation callback
|
||||
void *arg; // start's parameter
|
||||
void *rc; // start's return value
|
||||
|
|
|
@ -73,7 +73,6 @@ void _pthread_onfork_child(void) {
|
|||
tib = __get_tls();
|
||||
pt = (struct PosixThread *)tib->tib_pthread;
|
||||
atomic_store_explicit(&pt->cancelled, false, memory_order_relaxed);
|
||||
pt->tid = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&__mmi_lock_obj, &attr);
|
||||
|
|
|
@ -87,7 +87,7 @@ static void ListenForSigThr(void) {
|
|||
*
|
||||
* By default, pthread_cancel() can only take effect when a thread
|
||||
* reaches a cancellation point. Such functions are documented with
|
||||
* @cancellationpoint. They check the cancellation state before the
|
||||
* `@cancellationpoint`. They check the cancellation state before the
|
||||
* underlying system call is issued. If the system call is issued and
|
||||
* blocks, then pthread_cancel() will interrupt the operation in which
|
||||
* case the syscall wrapper will check the cancelled state a second
|
||||
|
@ -277,18 +277,20 @@ errno_t pthread_cancel(pthread_t thread) {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
if (IsWindows()) return 0; // no true cancellations on Windows yet
|
||||
tid = atomic_load_explicit(&pt->tib->tib_tid, memory_order_acquire);
|
||||
if (tid <= 0) return 0; // -1 means still starting, 0 means exited
|
||||
e = errno;
|
||||
if (!__tkill(tid, SIGTHR, pt->tib)) {
|
||||
return 0;
|
||||
} else {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
return rc;
|
||||
if (!(rc = pthread_getunique_np(thread, &tid))) {
|
||||
if (!IsWindows()) {
|
||||
e = errno;
|
||||
if (!__tkill(tid, SIGTHR, pt->tib)) {
|
||||
rc = 0;
|
||||
} else {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
}
|
||||
} else {
|
||||
rc = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,38 +19,27 @@
|
|||
#include "libc/assert.h"
|
||||
#include "libc/calls/blocksigs.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sched-sysv.internal.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/sigaltstack.h"
|
||||
#include "libc/calls/struct/sigset.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/atomic.h"
|
||||
#include "libc/intrin/bits.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/weaken.h"
|
||||
#include "libc/log/internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/gc.internal.h"
|
||||
#include "libc/runtime/clone.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "libc/thread/wait0.internal.h"
|
||||
#include "third_party/dlmalloc/dlmalloc.h"
|
||||
|
||||
STATIC_YOINK("nsync_mu_lock");
|
||||
STATIC_YOINK("nsync_mu_unlock");
|
||||
|
@ -254,15 +243,13 @@ static errno_t pthread_create_impl(pthread_t *thread,
|
|||
|
||||
// launch PosixThread(pt) in new thread
|
||||
pt->sigmask = oldsigs;
|
||||
if (clone(PosixThread, pt->attr.__stackaddr,
|
||||
pt->attr.__stacksize - (IsOpenbsd() ? 16 : 0),
|
||||
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->tib->tib_tid) == -1) {
|
||||
rc = errno;
|
||||
if ((rc = clone(PosixThread, pt->attr.__stackaddr,
|
||||
pt->attr.__stacksize - (IsOpenbsd() ? 16 : 0),
|
||||
CLONE_VM | CLONE_THREAD | CLONE_FS | CLONE_FILES |
|
||||
CLONE_SIGHAND | CLONE_SETTLS | CLONE_PARENT_SETTID |
|
||||
CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID,
|
||||
pt, &pt->ptid, pt->tib, &pt->tib->tib_tid))) {
|
||||
_pthread_free(pt);
|
||||
errno = e;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/calls/struct/cpuset.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/str/str.h"
|
||||
|
@ -33,37 +34,40 @@
|
|||
* @return 0 on success, or errno on error
|
||||
* @raise EINVAL if `size` or `bitset` is invalid
|
||||
* @raise ENOSYS if not Linux, FreeBSD, or NetBSD
|
||||
* @raise ESRCH if thread isn't alive
|
||||
*/
|
||||
errno_t pthread_getaffinity_np(pthread_t thread, size_t size,
|
||||
cpu_set_t *bitset) {
|
||||
int tid, rc, e = errno;
|
||||
tid = ((struct PosixThread *)thread)->tid;
|
||||
int e, rc, tid;
|
||||
|
||||
if (size != sizeof(cpu_set_t)) {
|
||||
rc = einval();
|
||||
} else if (IsWindows() || IsMetal() || IsOpenbsd()) {
|
||||
rc = enosys();
|
||||
} else if (IsFreebsd()) {
|
||||
if (!sys_sched_getaffinity_freebsd(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid, 32,
|
||||
bitset)) {
|
||||
rc = 32;
|
||||
if (!(rc = pthread_getunique_np(thread, &tid))) {
|
||||
e = errno;
|
||||
if (size != sizeof(cpu_set_t)) {
|
||||
rc = einval();
|
||||
} else if (IsWindows() || IsMetal() || IsOpenbsd()) {
|
||||
rc = enosys();
|
||||
} else if (IsFreebsd()) {
|
||||
if (!sys_sched_getaffinity_freebsd(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid,
|
||||
32, bitset)) {
|
||||
rc = 32;
|
||||
} else {
|
||||
rc = -1;
|
||||
}
|
||||
} else if (IsNetbsd()) {
|
||||
if (!sys_sched_getaffinity_netbsd(tid, 0, 32, bitset)) {
|
||||
rc = 32;
|
||||
} else {
|
||||
rc = -1;
|
||||
}
|
||||
} else {
|
||||
rc = -1;
|
||||
rc = sys_sched_getaffinity(tid, size, bitset);
|
||||
}
|
||||
} else if (IsNetbsd()) {
|
||||
if (!sys_sched_getaffinity_netbsd(tid, 0, 32, bitset)) {
|
||||
rc = 32;
|
||||
} else {
|
||||
rc = -1;
|
||||
if (rc > 0) {
|
||||
if (rc < size) {
|
||||
bzero((char *)bitset + rc, size - rc);
|
||||
}
|
||||
rc = 0;
|
||||
}
|
||||
} else {
|
||||
rc = sys_sched_getaffinity(tid, size, bitset);
|
||||
}
|
||||
if (rc > 0) {
|
||||
if (rc < size) {
|
||||
bzero((char *)bitset + rc, size - rc);
|
||||
}
|
||||
rc = 0;
|
||||
}
|
||||
|
||||
STRACE("pthread_getaffinity_np(%d, %'zu, %p) → %s", tid, size, bitset,
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/asmflag.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
@ -30,11 +31,12 @@
|
|||
#include "libc/thread/posixthread.internal.h"
|
||||
|
||||
static errno_t pthread_getname_impl(pthread_t thread, char *name, size_t size) {
|
||||
int fd, rc, tid, len, e = errno;
|
||||
int e, fd, rc, tid, len;
|
||||
|
||||
if ((rc = pthread_getunique_np(thread, &tid))) return rc;
|
||||
if (!size) return 0;
|
||||
bzero(name, size);
|
||||
tid = ((struct PosixThread *)thread)->tid;
|
||||
e = errno;
|
||||
|
||||
if (IsLinux()) {
|
||||
// TASK_COMM_LEN is 16 on Linux so we're just being paranoid.
|
||||
|
|
|
@ -16,12 +16,26 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
/**
|
||||
* Returns thread id of POSIX thread.
|
||||
* Returns system thread id of POSIX thread.
|
||||
*
|
||||
* @return 0 on success, or errno on error
|
||||
*/
|
||||
int64_t pthread_getunique_np(pthread_t thread) {
|
||||
struct PosixThread *pt = (struct PosixThread *)thread;
|
||||
return pt->tid;
|
||||
errno_t pthread_getunique_np(pthread_t thread, pthread_id_np_t *out_tid) {
|
||||
int tid;
|
||||
struct PosixThread *pt;
|
||||
for (pt = (struct PosixThread *)thread;;) {
|
||||
tid = atomic_load_explicit(&pt->ptid, memory_order_acquire);
|
||||
if (!tid) {
|
||||
pthread_yield();
|
||||
} else {
|
||||
*out_tid = tid;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
|
@ -33,13 +34,17 @@
|
|||
* @asyncsignalsafe
|
||||
*/
|
||||
errno_t pthread_kill(pthread_t thread, int sig) {
|
||||
int rc, e = errno;
|
||||
struct PosixThread *pt = (struct PosixThread *)thread;
|
||||
if (!__tkill(pt->tid, sig, pt->tib)) {
|
||||
rc = 0;
|
||||
} else {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
int e, rc, tid;
|
||||
struct PosixThread *p;
|
||||
if (!(rc = pthread_getunique_np(thread, &tid))) {
|
||||
e = errno;
|
||||
p = (struct PosixThread *)thread;
|
||||
if (!__tkill(tid, sig, p->tib)) {
|
||||
rc = 0;
|
||||
} else {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -20,23 +20,29 @@
|
|||
#include "libc/calls/struct/sched_param.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/posixthread.internal.h"
|
||||
|
||||
errno_t _pthread_reschedule(struct PosixThread *pt) {
|
||||
int rc, e = errno;
|
||||
int e, rc, tid;
|
||||
int policy = pt->attr.__schedpolicy;
|
||||
struct sched_param param = {pt->attr.__schedparam};
|
||||
if (IsNetbsd()) {
|
||||
rc = sys_sched_setparam_netbsd(0, pt->tid, policy, ¶m);
|
||||
} else if (IsLinux()) {
|
||||
rc = sys_sched_setscheduler(pt->tid, policy, ¶m);
|
||||
} else if (IsFreebsd()) {
|
||||
rc = _pthread_setschedparam_freebsd(pt->tid, policy, ¶m);
|
||||
} else {
|
||||
rc = enosys();
|
||||
if (!(rc = pthread_getunique_np((pthread_t)pt, &tid))) {
|
||||
e = errno;
|
||||
if (IsNetbsd()) {
|
||||
rc = sys_sched_setparam_netbsd(0, tid, policy, ¶m);
|
||||
} else if (IsLinux()) {
|
||||
rc = sys_sched_setscheduler(tid, policy, ¶m);
|
||||
} else if (IsFreebsd()) {
|
||||
rc = _pthread_setschedparam_freebsd(tid, policy, ¶m);
|
||||
} else {
|
||||
rc = enosys();
|
||||
}
|
||||
if (rc == -1) {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
}
|
||||
}
|
||||
rc = rc != -1 ? 0 : errno;
|
||||
errno = e;
|
||||
return rc;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include "libc/calls/syscall_support-nt.internal.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/nt/enum/threadaccess.h"
|
||||
|
@ -52,23 +53,25 @@ static dontinline textwindows int sys_pthread_setaffinity_nt(
|
|||
*/
|
||||
errno_t pthread_setaffinity_np(pthread_t thread, size_t size,
|
||||
const cpu_set_t *bitset) {
|
||||
int tid, rc, e = errno;
|
||||
tid = ((struct PosixThread *)thread)->tid;
|
||||
if (size != sizeof(cpu_set_t)) {
|
||||
rc = einval();
|
||||
} else if (IsWindows()) {
|
||||
rc = sys_pthread_setaffinity_nt(tid, size, bitset);
|
||||
} else if (IsFreebsd()) {
|
||||
rc = sys_sched_setaffinity_freebsd(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid, 32,
|
||||
bitset);
|
||||
} else if (IsNetbsd()) {
|
||||
rc = sys_sched_setaffinity_netbsd(tid, 0, 32, bitset);
|
||||
} else {
|
||||
rc = sys_sched_setaffinity(tid, size, bitset);
|
||||
}
|
||||
if (rc == -1) {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
int e, rc, tid;
|
||||
if (!(rc = pthread_getunique_np(thread, &tid))) {
|
||||
e = errno;
|
||||
if (size != sizeof(cpu_set_t)) {
|
||||
rc = einval();
|
||||
} else if (IsWindows()) {
|
||||
rc = sys_pthread_setaffinity_nt(tid, size, bitset);
|
||||
} else if (IsFreebsd()) {
|
||||
rc = sys_sched_setaffinity_freebsd(CPU_LEVEL_WHICH, CPU_WHICH_TID, tid,
|
||||
32, bitset);
|
||||
} else if (IsNetbsd()) {
|
||||
rc = sys_sched_setaffinity_netbsd(tid, 0, 32, bitset);
|
||||
} else {
|
||||
rc = sys_sched_setaffinity(tid, size, bitset);
|
||||
}
|
||||
if (rc == -1) {
|
||||
rc = errno;
|
||||
errno = e;
|
||||
}
|
||||
}
|
||||
STRACE("pthread_setaffinity_np(%d, %'zu, %p) → %s", tid, size, bitset,
|
||||
DescribeErrnoResult(rc));
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/asan.internal.h"
|
||||
#include "libc/intrin/asmflag.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/pr.h"
|
||||
|
@ -31,12 +32,13 @@
|
|||
|
||||
static errno_t pthread_setname_impl(pthread_t thread, const char *name) {
|
||||
char path[128], *p;
|
||||
int fd, rc, tid, len, e = errno;
|
||||
int e, fd, rc, tid, len;
|
||||
|
||||
tid = ((struct PosixThread *)thread)->tid;
|
||||
if ((rc = pthread_getunique_np(thread, &tid))) return rc;
|
||||
len = strlen(name);
|
||||
|
||||
if (IsLinux()) {
|
||||
e = errno;
|
||||
if (tid == gettid()) {
|
||||
if (prctl(PR_SET_NAME, name) == -1) {
|
||||
rc = errno;
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/clone.internal.h"
|
||||
|
@ -92,6 +93,7 @@ static int Spawner(void *arg, int tid) {
|
|||
* @return 0 on success, or -1 w/ errno
|
||||
*/
|
||||
int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
||||
errno_t rc;
|
||||
struct spawn *th, ths;
|
||||
struct spawner *spawner;
|
||||
__require_tls();
|
||||
|
@ -122,11 +124,13 @@ int _spawn(int fun(void *, int), void *arg, struct spawn *opt_out_thread) {
|
|||
spawner = malloc(sizeof(struct spawner));
|
||||
spawner->fun = fun;
|
||||
spawner->arg = arg;
|
||||
if (clone(Spawner, th->stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
||||
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->tib->tib_tid) == -1) {
|
||||
rc = clone(Spawner, th->stk, GetStackSize() - 16 /* openbsd:stackbound */,
|
||||
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->tib->tib_tid);
|
||||
if (rc) {
|
||||
errno = rc;
|
||||
_freestack(th->stk);
|
||||
free(th->tls);
|
||||
return -1;
|
||||
|
|
|
@ -115,7 +115,7 @@ int pthread_testcancel_np(void);
|
|||
void pthread_exit(void *) wontreturn;
|
||||
pthread_t pthread_self(void) pureconst;
|
||||
pthread_id_np_t pthread_getthreadid_np(void);
|
||||
int64_t pthread_getunique_np(pthread_t);
|
||||
int pthread_getunique_np(pthread_t, pthread_id_np_t *);
|
||||
int pthread_setname_np(pthread_t, const char *);
|
||||
int pthread_getname_np(pthread_t, char *, size_t);
|
||||
int pthread_getattr_np(pthread_t, pthread_attr_t *);
|
||||
|
|
|
@ -24,7 +24,7 @@ struct CosmoTib {
|
|||
intptr_t tib_locale; /* 0x20 */
|
||||
intptr_t tib_pthread; /* 0x28 */
|
||||
struct CosmoTib *tib_self2; /* 0x30 */
|
||||
_Atomic(int32_t) tib_tid; /* 0x38 */
|
||||
_Atomic(int32_t) tib_tid; /* 0x38 transitions -1 → tid → 0 */
|
||||
int32_t tib_errno; /* 0x3c */
|
||||
uint64_t tib_flags; /* 0x40 */
|
||||
void *tib_nsync;
|
||||
|
|
|
@ -8,8 +8,8 @@ COSMOPOLITAN_C_START_
|
|||
* See darwin-libpthread/kern/kern_support.c
|
||||
*/
|
||||
|
||||
int sys_bsdthread_create(void *func, void *func_arg, void *stack, void *pthread,
|
||||
uint32_t flags);
|
||||
errno_t sys_clone_xnu(void *func, void *func_arg, void *stack, void *pthread,
|
||||
uint32_t flags);
|
||||
int sys_bsdthread_register(
|
||||
void (*threadstart)(void *pthread, int machport, void *(*func)(void *),
|
||||
void *arg, intptr_t *, unsigned),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue