mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 19:28:29 +00:00
Improve locks and signals
- Introduce fast spinlock API - Double rand64() perf w/ spinlock - Improve raise() on New Technology - Support gettid() across platforms - Implement SA_NODEFER on New Technology - Move the lock intrinsics into LIBC_INTRIN - Make SIGTRAP recoverable on New Technology - Block SIGCHLD in wait4() on New Technology - Add threading prototypes for XNU and FreeBSD - Rewrite abort() fixing its minor bugs on XNU/NT - Shave down a lot of the content in libc/bits/bits.h - Let signal handlers modify CPU registers on New Technology
This commit is contained in:
parent
f68f1789bd
commit
046c7ebd4a
110 changed files with 1514 additions and 876 deletions
112
libc/thread/freebsd.internal.h
Normal file
112
libc/thread/freebsd.internal.h
Normal file
|
@ -0,0 +1,112 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_THREAD_FREEBSD_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_THREAD_FREEBSD_INTERNAL_H_
|
||||
#include "libc/bits/asmflag.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/errno.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
/**
|
||||
* @fileoverview FreeBSD Threading
|
||||
*
|
||||
* @note even though FreeBSD uses a 64-bit type for thread IDs the
|
||||
* maximum legal range is PID_MAX + 2 (100001) through INT_MAX
|
||||
*/
|
||||
|
||||
struct rtprio {
|
||||
uint16_t type; /* scheduling class */
|
||||
uint16_t prio;
|
||||
};
|
||||
|
||||
struct thr_param {
|
||||
void (*start_func)(void *);
|
||||
void *arg;
|
||||
char *stack_base;
|
||||
uint64_t stack_size;
|
||||
char *tls_base;
|
||||
uint64_t tls_size;
|
||||
int64_t *child_tid;
|
||||
int64_t *parent_tid;
|
||||
int32_t flags;
|
||||
struct rtprio *rtp;
|
||||
};
|
||||
|
||||
static inline wontreturn void thr_exit(int64_t *opt_out_state) {
|
||||
long ax, di;
|
||||
asm volatile("syscall"
|
||||
: "=a"(ax)
|
||||
: "0"(431), "D"(opt_out_state)
|
||||
: "memory", "cc");
|
||||
unreachable;
|
||||
}
|
||||
|
||||
static inline int thr_new(struct thr_param *param, int param_size) {
|
||||
bool failed;
|
||||
long ax, di, si;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=D"(di), "=S"(si)
|
||||
: "1"(455), "2"(param), "3"(param_size)
|
||||
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (failed) ax = -ax;
|
||||
return ax;
|
||||
}
|
||||
|
||||
static inline int thr_kill(int64_t id, int sig) {
|
||||
bool failed;
|
||||
long ax, di, si;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=D"(di), "=S"(si)
|
||||
: "1"(433), "2"(id), "3"(sig)
|
||||
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (failed) ax = -ax;
|
||||
return ax;
|
||||
}
|
||||
|
||||
static inline int thr_suspend(const struct timespec *opt_timeout) {
|
||||
bool failed;
|
||||
long ax, di;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=D"(di)
|
||||
: "1"(442), "2"(opt_timeout)
|
||||
: "rcx", "rdx", "rsi", "r8", "r9", "r10", "r11", "memory");
|
||||
if (failed) ax = -ax;
|
||||
return ax;
|
||||
}
|
||||
|
||||
static inline int thr_wake(int64_t id) {
|
||||
bool failed;
|
||||
long ax, di;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=D"(di)
|
||||
: "1"(443), "2"(id)
|
||||
: "rcx", "rdx", "rsi", "r8", "r9", "r10", "r11", "memory");
|
||||
if (failed) ax = -ax;
|
||||
return ax;
|
||||
}
|
||||
|
||||
static inline int thr_set_name(int64_t id, const char *name) {
|
||||
bool failed;
|
||||
long ax, di, si;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=D"(di), "=S"(si)
|
||||
: "1"(464), "2"(id), "3"(name)
|
||||
: "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (failed) ax = -ax;
|
||||
return ax;
|
||||
}
|
||||
|
||||
static inline int thr_kill2(int pid, int64_t id, int sig) {
|
||||
bool failed;
|
||||
long ax, di, si, dx;
|
||||
asm volatile(CFLAG_ASM("syscall")
|
||||
: CFLAG_CONSTRAINT(failed), "=a"(ax), "=D"(di), "=S"(si),
|
||||
"=d"(dx)
|
||||
: "1"(481), "2"(pid), "3"(id), "4"(sig)
|
||||
: "rcx", "r8", "r9", "r10", "r11", "memory");
|
||||
if (failed) ax = -ax;
|
||||
return ax;
|
||||
}
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_THREAD_FREEBSD_INTERNAL_H_ */
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/atomic.h"
|
||||
#include "libc/intrin/atomic_load.h"
|
||||
#include "libc/thread/sem.h"
|
||||
#include "libc/thread/wait.h"
|
||||
#include "libc/thread/yield.h"
|
||||
|
@ -37,6 +38,7 @@ int cthread_sem_init(cthread_sem_t* sem, int count) {
|
|||
sem->linux.count = count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cthread_sem_destroy(cthread_sem_t* sem) {
|
||||
(void)sem;
|
||||
return 0;
|
||||
|
@ -44,7 +46,7 @@ int cthread_sem_destroy(cthread_sem_t* sem) {
|
|||
|
||||
int cthread_sem_signal(cthread_sem_t* sem) {
|
||||
uint64_t count;
|
||||
asm volatile("lock xadd\t%1, %0"
|
||||
asm volatile("lock xadd\t%1,%0"
|
||||
: "+m"(sem->linux.count), "=r"(count)
|
||||
: "1"(1)
|
||||
: "cc");
|
||||
|
@ -62,7 +64,7 @@ int cthread_sem_wait_futex(cthread_sem_t* sem, const struct timespec* timeout) {
|
|||
uint64_t count;
|
||||
|
||||
// record current thread as waiter
|
||||
asm volatile("lock xadd\t%1, %0"
|
||||
asm volatile("lock xadd\t%1,%0"
|
||||
: "+m"(sem->linux.count), "=r"(count)
|
||||
: "1"((uint64_t)1 << CTHREAD_THREAD_VAL_BITS)
|
||||
: "cc");
|
||||
|
@ -77,7 +79,7 @@ int cthread_sem_wait_futex(cthread_sem_t* sem, const struct timespec* timeout) {
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// WARNING: an offset of 4 bytes would be required on little-endian archs
|
||||
void* wait_address = &sem->linux.count;
|
||||
cthread_memory_wait32(wait_address, count, timeout);
|
||||
|
@ -91,16 +93,17 @@ int cthread_sem_wait_spin(cthread_sem_t* sem, uint64_t count, int spin,
|
|||
const struct timespec* timeout) {
|
||||
// spin on pause
|
||||
for (int attempt = 0; attempt < spin; ++attempt) {
|
||||
//if ((count >> CTHREAD_THREAD_VAL_BITS) != 0) break;
|
||||
// if ((count >> CTHREAD_THREAD_VAL_BITS) != 0) break;
|
||||
while ((uint32_t)count > 0) {
|
||||
// spin is useful if multiple waiters can acquire the semaphore at the same time
|
||||
// spin is useful if multiple waiters can acquire the semaphore at the
|
||||
// same time
|
||||
if (atomic_compare_exchange_weak(&sem->linux.count, count, count - 1)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
pause(attempt);
|
||||
}
|
||||
|
||||
|
||||
return cthread_sem_wait_futex(sem, timeout);
|
||||
}
|
||||
|
||||
|
@ -110,11 +113,12 @@ int cthread_sem_wait(cthread_sem_t* sem, int spin,
|
|||
|
||||
// uncontended
|
||||
while ((uint32_t)count > 0) {
|
||||
// spin is useful if multiple waiters can acquire the semaphore at the same time
|
||||
// spin is useful if multiple waiters can acquire the semaphore at the same
|
||||
// time
|
||||
if (atomic_compare_exchange_weak(&sem->linux.count, count, count - 1)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return cthread_sem_wait_spin(sem, count, spin, timeout);
|
||||
}
|
||||
|
|
25
libc/thread/xnu.internal.h
Normal file
25
libc/thread/xnu.internal.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef COSMOPOLITAN_LIBC_THREAD_XNU_INTERNAL_H_
|
||||
#define COSMOPOLITAN_LIBC_THREAD_XNU_INTERNAL_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
/**
|
||||
* XNU thread system calls.
|
||||
* @see darwin-libpthread/kern/kern_support.c
|
||||
*/
|
||||
|
||||
void *bsdthread_create(void *func, void *func_arg, void *stack, void *pthread,
|
||||
uint32_t flags);
|
||||
int bsdthread_terminate(void *stackaddr, size_t freesize, uint32_t port,
|
||||
uint32_t sem);
|
||||
int bsdthread_register(void *threadstart, void *wqthread, uint32_t flags,
|
||||
void *stack_addr_hint, void *targetconc_ptr,
|
||||
uint32_t dispatchqueue_offset, uint32_t tsd_offset);
|
||||
int bsdthread_ctl(void *cmd, void *arg1, void *arg2, void *arg3);
|
||||
uint64_t thread_selfid(void);
|
||||
uint64_t thread_selfusage(void);
|
||||
int thread_selfcounts(int type, void *buf, uint64_t nbytes);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_THREAD_XNU_INTERNAL_H_ */
|
Loading…
Add table
Add a link
Reference in a new issue