Get threads working well on MacOS Arm64

- Now using 10x better GCD semaphores
- We now generate Linux-like thread ids
- We now use fast system clock / sleep libraries
- The APE M1 loader now generates Linux-like stacks
This commit is contained in:
Justine Tunney 2023-06-04 01:57:10 -07:00
parent b5eab2b0b7
commit bcf9af94bf
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2037 changed files with 4664 additions and 4451 deletions

View file

@ -29,6 +29,7 @@
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
relegated void __assert_fail(const char *expr, const char *file, int line) {
int me, owner;
@ -37,7 +38,7 @@ relegated void __assert_fail(const char *expr, const char *file, int line) {
strace_enabled(-1);
ftrace_enabled(-1);
owner = 0;
me = sys_gettid();
me = __tls_enabled ? __get_tls()->tib_tid : sys_gettid();
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
kprintf("%s:%d: assert(%s) failed (tid %d) %m\n", file, line, expr, me);
if (__vforked ||

View file

@ -20,6 +20,7 @@
#include "libc/intrin/asmflag.h"
#include "libc/nt/thread.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/sysv/consts/nr.h"
#include "libc/thread/tls.h"
@ -74,13 +75,16 @@ privileged wontreturn void _Exit1(int rc) {
}
notpossible;
#elif defined(__aarch64__)
register long r0 asm("x0") = rc;
asm volatile("mov\tx8,%0\n\t"
"mov\tx16,%1\n\t"
"svc\t0"
: /* no outputs */
: "i"(93), "i"(0x169), "r"(r0)
: "x8", "memory");
if (IsLinux()) {
register long r0 asm("x0") = rc;
asm volatile("mov\tx8,%0\n\t"
"svc\t0"
: /* no outputs */
: "i"(93), "r"(r0)
: "x8", "memory");
} else if (IsXnu()) {
__syslib->pthread_exit(0);
}
notpossible;
#else
#error "arch unsupported"

View file

@ -227,8 +227,8 @@ privileged dontinline void klog(const char *b, size_t n) {
register long r0 asm("x0") = (long)2;
register long r1 asm("x1") = (long)b;
register long r2 asm("x2") = (long)n;
register long r8 asm("x8") = (long)__NR_write & 0x7ff;
register long r16 asm("x16") = (long)__NR_write & 0x7ff;
register long r8 asm("x8") = (long)__NR_write;
register long r16 asm("x16") = (long)__NR_write;
register long res_x0 asm("x0");
asm volatile("svc\t0"
: "=r"(res_x0)

View file

@ -90,13 +90,18 @@ sched_yield:
#elif defined(__aarch64__)
mov x0,#0
mov x1,#0
mov x2,#0
mov x3,#0
mov x8,#0x7c // sched_yield() for linux
mov x16,#0x85d // select(0,0,0,0) for xnu
stp x29,x30,[sp,-32]!
mov x29,sp
mov x3,0
mov x2,0
add x4,sp,16
mov x1,0
mov w0,0
stp xzr,xzr,[sp,16]
mov x8,#0x7c // sched_yield() for gnu/systemd
mov x16,#0x5d // select(0,0,0,0,&blah) for xnu
svc 0
ldp x29,x30,[sp],32
ret
#else

View file

@ -63,14 +63,15 @@ privileged int sys_gettid(void) {
}
return tid;
#elif defined(__aarch64__)
register long res_x0 asm("x0");
// this can't be used on xnu
if (!IsLinux()) notpossible;
register long res asm("x0");
asm volatile("mov\tx8,%1\n\t"
"mov\tx16,%2\n\t"
"svc\t0"
: "=r"(res_x0)
: "i"(178), "i"(186)
: "=r"(res)
: "i"(178)
: "x8", "memory");
return res_x0;
return res;
#else
#error "arch unsupported"
#endif