mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Make futexes 100x better on x86 MacOS
Thanks to @autumnjolitz (in #876) the Cosmopolitan codebase is now acquainted with Apple's outstanding ulock system calls which offer something much closer to futexes than Grand Central Dispatch which wasn't quite as good, since its wait function can't be interrupted by signals (therefore necessitating a busy loop) and it also needs semaphore objects to be created and freed. Even though ulock is an internal Apple API, strictly speaking, the benefits of futexes are so great that it's worth the risk for now especially since we have the GCD implementation still as a quick escape hatch if it changes Here's why this change is important for x86 XNU users. Cosmo has a suboptimal polyfill when the operating system doesn't offer an API that let's us implement futexes properly. Sadly we had to use that on X86 XNU until now. The polyfill works using clock_nanosleep, to poll the futex in a busy loop with exponential backoff. On XNU x86 clock_nanosleep suffers from us not being able to use a fast clock gettime implementation, which had a compounding effect that's made the polyfill function even more poorly. On X86 XNU we also need to polyfill sched_yield() using select(), which made things even more troublesome. Now that we have futexes we don't have any busy loops anymore for both condition variables and thread joining so optimal performance is attained. To demonstrate, consider these benchmarks Before: $ ./lockscale_test.com -b consumed 38.8377 seconds real time and 0.087131 seconds cpu time After: $ ./lockscale_test.com -b consumed 0.007955 seconds real time and 0.011515 seconds cpu time Fixes #876
This commit is contained in:
parent
ff250a0c10
commit
85f64f3851
21 changed files with 267 additions and 34 deletions
|
@ -10,6 +10,8 @@ long __syscall2(long, long, int);
|
||||||
int __syscall2i(long, long, int) asm("__syscall2");
|
int __syscall2i(long, long, int) asm("__syscall2");
|
||||||
long __syscall3(long, long, long, int);
|
long __syscall3(long, long, long, int);
|
||||||
int __syscall3i(long, long, long, int) asm("__syscall3");
|
int __syscall3i(long, long, long, int) asm("__syscall3");
|
||||||
|
long __syscall4(long, long, long, long, int);
|
||||||
|
int __syscall4i(long, long, long, long, int) asm("__syscall4");
|
||||||
|
|
||||||
bool __is_linux_2_6_23(void);
|
bool __is_linux_2_6_23(void);
|
||||||
bool32 sys_isatty_metal(int);
|
bool32 sys_isatty_metal(int);
|
||||||
|
|
|
@ -568,25 +568,37 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
||||||
goto FormatUnsigned;
|
goto FormatUnsigned;
|
||||||
|
|
||||||
case 'P':
|
case 'P':
|
||||||
|
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
|
||||||
|
x = __pid;
|
||||||
|
#ifdef __x86_64__
|
||||||
|
} else if (IsLinux()) {
|
||||||
|
asm volatile("syscall"
|
||||||
|
: "=a"(x)
|
||||||
|
: "0"(__NR_getpid)
|
||||||
|
: "rcx", "rdx", "r11", "memory");
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
x = 666;
|
||||||
|
}
|
||||||
|
if (!__nocolor && p + 7 <= e) {
|
||||||
|
*p++ = '\e';
|
||||||
|
*p++ = '[';
|
||||||
|
*p++ = '1';
|
||||||
|
*p++ = ';';
|
||||||
|
*p++ = '3';
|
||||||
|
*p++ = '0' + x % 8;
|
||||||
|
*p++ = 'm';
|
||||||
|
ansi = 1;
|
||||||
|
}
|
||||||
|
goto FormatDecimal;
|
||||||
|
|
||||||
|
case 'H':
|
||||||
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
|
if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) {
|
||||||
if (tib) {
|
if (tib) {
|
||||||
x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
|
x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed);
|
||||||
if (IsNetbsd() && x == 1) {
|
|
||||||
x = __pid;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
x = __pid;
|
x = __pid;
|
||||||
}
|
}
|
||||||
if (!__nocolor && p + 7 <= e) {
|
|
||||||
*p++ = '\e';
|
|
||||||
*p++ = '[';
|
|
||||||
*p++ = '1';
|
|
||||||
*p++ = ';';
|
|
||||||
*p++ = '3';
|
|
||||||
*p++ = '0' + x % 8;
|
|
||||||
*p++ = 'm';
|
|
||||||
ansi = 1;
|
|
||||||
}
|
|
||||||
#ifdef __x86_64__
|
#ifdef __x86_64__
|
||||||
} else if (IsLinux()) {
|
} else if (IsLinux()) {
|
||||||
asm volatile("syscall"
|
asm volatile("syscall"
|
||||||
|
@ -597,6 +609,17 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
|
||||||
} else {
|
} else {
|
||||||
x = 666;
|
x = 666;
|
||||||
}
|
}
|
||||||
|
if (!__nocolor && p + 7 <= e) {
|
||||||
|
// xnu thread ids are always divisible by 8
|
||||||
|
*p++ = '\e';
|
||||||
|
*p++ = '[';
|
||||||
|
*p++ = '1';
|
||||||
|
*p++ = ';';
|
||||||
|
*p++ = '3';
|
||||||
|
*p++ = '0' + x % 7;
|
||||||
|
*p++ = 'm';
|
||||||
|
ansi = 1;
|
||||||
|
}
|
||||||
goto FormatDecimal;
|
goto FormatDecimal;
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
|
@ -1080,7 +1103,8 @@ privileged void kvprintf(const char *fmt, va_list v) {
|
||||||
* - `X` uppercase
|
* - `X` uppercase
|
||||||
* - `T` timestamp
|
* - `T` timestamp
|
||||||
* - `x` hexadecimal
|
* - `x` hexadecimal
|
||||||
* - `P` PID (or TID if TLS is enabled)
|
* - `P` process id
|
||||||
|
* - `H` thread id
|
||||||
*
|
*
|
||||||
* Types:
|
* Types:
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "libc/intrin/likely.h"
|
#include "libc/intrin/likely.h"
|
||||||
#include "libc/runtime/runtime.h"
|
#include "libc/runtime/runtime.h"
|
||||||
|
|
||||||
#define _NTTRACE 1 /* not configurable w/ flag yet */
|
#define _NTTRACE 0 /* not configurable w/ flag yet */
|
||||||
#define _POLLTRACE 0 /* not configurable w/ flag yet */
|
#define _POLLTRACE 0 /* not configurable w/ flag yet */
|
||||||
#define _DATATRACE 1 /* not configurable w/ flag yet */
|
#define _DATATRACE 1 /* not configurable w/ flag yet */
|
||||||
#define _LOCKTRACE 0 /* not configurable w/ flag yet */
|
#define _LOCKTRACE 0 /* not configurable w/ flag yet */
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
#define _KERNTRACE 0 /* not configurable w/ flag yet */
|
#define _KERNTRACE 0 /* not configurable w/ flag yet */
|
||||||
#define _TIMETRACE 0 /* not configurable w/ flag yet */
|
#define _TIMETRACE 0 /* not configurable w/ flag yet */
|
||||||
|
|
||||||
#define STRACE_PROLOGUE "%rSYS %6P %'18T "
|
#define STRACE_PROLOGUE "%rSYS %6P %6H %'18T "
|
||||||
|
|
||||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||||
COSMOPOLITAN_C_START_
|
COSMOPOLITAN_C_START_
|
||||||
|
|
54
libc/intrin/ulock.c
Normal file
54
libc/intrin/ulock.c
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/*-*- 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 2023 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/ulock.h"
|
||||||
|
#include "libc/assert.h"
|
||||||
|
#include "libc/calls/calls.h"
|
||||||
|
#include "libc/calls/syscall_support-sysv.internal.h"
|
||||||
|
#include "libc/dce.h"
|
||||||
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
|
#include "libc/intrin/strace.internal.h"
|
||||||
|
|
||||||
|
// XNU futexes
|
||||||
|
// https://opensource.apple.com/source/xnu/xnu-7195.50.7.100.1/bsd/sys/ulock.h.auto.html
|
||||||
|
// https://opensource.apple.com/source/xnu/xnu-3789.41.3/bsd/kern/sys_ulock.c.auto.html
|
||||||
|
|
||||||
|
int sys_ulock_wait(uint32_t operation, void *addr, uint64_t value,
|
||||||
|
uint32_t timeout_micros) asm("sys_futex_cp");
|
||||||
|
|
||||||
|
// returns -1 w/ errno
|
||||||
|
int ulock_wait(uint32_t operation, void *addr, uint64_t value,
|
||||||
|
uint32_t timeout_micros) {
|
||||||
|
int rc;
|
||||||
|
operation |= ULF_WAIT_CANCEL_POINT;
|
||||||
|
STRACE("ulock_wait(%#x, %p, %lx, %u) → ...", operation, addr, value,
|
||||||
|
timeout_micros);
|
||||||
|
rc = sys_ulock_wait(operation, addr, value, timeout_micros);
|
||||||
|
STRACE("ulock_wait(%#x, %p, %lx, %u) → %d% m", operation, addr, value,
|
||||||
|
timeout_micros, rc);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns -errno
|
||||||
|
int ulock_wake(uint32_t operation, void *addr, uint64_t wake_value) {
|
||||||
|
int rc;
|
||||||
|
rc = __syscall3i(operation, (long)addr, wake_value, 0x2000000 | 516);
|
||||||
|
STRACE("ulock_wake(%#x, %p, %lx) → %s", operation, addr, wake_value,
|
||||||
|
DescribeErrno(rc));
|
||||||
|
return rc;
|
||||||
|
}
|
27
libc/intrin/ulock.h
Normal file
27
libc/intrin/ulock.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef COSMOPOLITAN_ULOCK_H_
|
||||||
|
#define COSMOPOLITAN_ULOCK_H_
|
||||||
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||||
|
COSMOPOLITAN_C_START_
|
||||||
|
|
||||||
|
/* both wake and wait take one of these */
|
||||||
|
#define UL_COMPARE_AND_WAIT 1 /* multi-thread */
|
||||||
|
#define UL_UNFAIR_LOCK 2
|
||||||
|
#define UL_COMPARE_AND_WAIT_SHARED 3 /* multi-thread/process */
|
||||||
|
#define UL_UNFAIR_LOCK64_SHARED 4
|
||||||
|
#define UL_COMPARE_AND_WAIT64 5
|
||||||
|
#define UL_COMPARE_AND_WAIT64_SHARED 6
|
||||||
|
|
||||||
|
#define ULF_WAKE_ALL 0x00000100
|
||||||
|
#define ULF_WAKE_THREAD 0x00000200 /* takes wake_value */
|
||||||
|
#define ULF_WAKE_ALLOW_NON_OWNER 0x00000400
|
||||||
|
|
||||||
|
#define ULF_WAIT_WORKQ_DATA_CONTENTION 0x00010000
|
||||||
|
#define ULF_WAIT_CANCEL_POINT 0x00020000 /* raises eintr */
|
||||||
|
#define ULF_WAIT_ADAPTIVE_SPIN 0x00040000
|
||||||
|
|
||||||
|
int ulock_wake(uint32_t, void *, uint64_t);
|
||||||
|
int ulock_wait(uint32_t, void *, uint64_t, uint32_t);
|
||||||
|
|
||||||
|
COSMOPOLITAN_C_END_
|
||||||
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||||
|
#endif /* COSMOPOLITAN_ULOCK_H_ */
|
|
@ -230,6 +230,11 @@ static relegated void __oncrash_impl(int sig, struct siginfo *si,
|
||||||
}
|
}
|
||||||
Append(b, " %s %s %s %s\n", names.sysname, names.version, names.nodename,
|
Append(b, " %s %s %s %s\n", names.sysname, names.version, names.nodename,
|
||||||
names.release);
|
names.release);
|
||||||
|
Append(
|
||||||
|
b, " cosmoaddr2line %s%s %lx %s\n", __argv[0],
|
||||||
|
endswith(__argv[0], ".com") ? ".dbg" : "", ctx ? ctx->uc_mcontext.PC : 0,
|
||||||
|
DescribeBacktrace(ctx ? (struct StackFrame *)ctx->uc_mcontext.BP
|
||||||
|
: (struct StackFrame *)__builtin_frame_address(0)));
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
long pc;
|
long pc;
|
||||||
char *mem = 0;
|
char *mem = 0;
|
||||||
|
@ -240,6 +245,7 @@ static relegated void __oncrash_impl(int sig, struct siginfo *si,
|
||||||
struct StackFrame *fp;
|
struct StackFrame *fp;
|
||||||
struct SymbolTable *st;
|
struct SymbolTable *st;
|
||||||
struct fpsimd_context *vc;
|
struct fpsimd_context *vc;
|
||||||
|
|
||||||
st = GetSymbolTable();
|
st = GetSymbolTable();
|
||||||
debugbin = FindDebugBinary();
|
debugbin = FindDebugBinary();
|
||||||
addr2line = GetAddr2linePath();
|
addr2line = GetAddr2linePath();
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include "libc/intrin/atomic.h"
|
#include "libc/intrin/atomic.h"
|
||||||
#include "libc/intrin/describeflags.internal.h"
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
|
#include "libc/intrin/ulock.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
#include "libc/macros.internal.h"
|
#include "libc/macros.internal.h"
|
||||||
|
@ -197,12 +198,20 @@ XnuThreadMain(void *pthread, // rdi
|
||||||
// %rsi = size_t freesize,
|
// %rsi = size_t freesize,
|
||||||
// %rdx = uint32_t port,
|
// %rdx = uint32_t port,
|
||||||
// %r10 = uint32_t sem);
|
// %r10 = uint32_t sem);
|
||||||
asm volatile("movl\t$0,%0\n\t" // *wt->ztid = 0
|
asm volatile("movl\t$0,(%%rsi)\n\t" // *wt->ztid = 0
|
||||||
"xor\t%%r10d,%%r10d\n\t" // sem = 0
|
"mov\t$0x101,%%edi\n\t" // wake all
|
||||||
"syscall" // __bsdthread_terminate()
|
"xor\t%%edx,%%edx\n\t" // wake_value
|
||||||
: "=m"(*wt->ztid)
|
"mov\t$0x02000204,%%eax\n\t" // ulock_wake()
|
||||||
: "a"(0x2000000 | 361), "D"(0), "S"(0), "d"(0L)
|
"syscall\n\t" //
|
||||||
: "rcx", "r10", "r11", "memory");
|
"xor\t%%edi,%%edi\n\t" // freeaddr
|
||||||
|
"xor\t%%esi,%%esi\n\t" // freesize
|
||||||
|
"xor\t%%edx,%%edx\n\t" // kport
|
||||||
|
"xor\t%%r10d,%%r10d\n\t" // joinsem
|
||||||
|
"mov\t$0x02000169,%%eax\n\t" // bsdthread_terminate()
|
||||||
|
"syscall"
|
||||||
|
: /* no outputs */
|
||||||
|
: "S"(wt->ztid)
|
||||||
|
: "rax", "rcx", "r10", "r11", "memory");
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -443,6 +452,7 @@ static void *SiliconThreadMain(void *arg) {
|
||||||
*wt->ctid = wt->this;
|
*wt->ctid = wt->this;
|
||||||
__stack_call(wt->arg, wt->this, 0, 0, wt->func, wt);
|
__stack_call(wt->arg, wt->this, 0, 0, wt->func, wt);
|
||||||
*wt->ztid = 0;
|
*wt->ztid = 0;
|
||||||
|
ulock_wake(UL_COMPARE_AND_WAIT | ULF_WAKE_ALL, wt->ztid, 0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -206,6 +206,7 @@ textstartup void __enable_tls(void) {
|
||||||
tid = sys_gettid();
|
tid = sys_gettid();
|
||||||
}
|
}
|
||||||
atomic_store_explicit(&tib->tib_tid, tid, memory_order_relaxed);
|
atomic_store_explicit(&tib->tib_tid, tid, memory_order_relaxed);
|
||||||
|
// TODO(jart): set_tid_address?
|
||||||
|
|
||||||
// initialize posix threads
|
// initialize posix threads
|
||||||
_pthread_static.tib = tib;
|
_pthread_static.tib = tib;
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
#include "libc/sysv/macros.internal.h"
|
#include "libc/sysv/macros.internal.h"
|
||||||
.scall sys_futex,0x0a60531c6ffff0ca,98,4095,globl,hidden
|
.scall sys_futex,0x0a60531c622030ca,98,515,globl,hidden
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
#include "libc/sysv/macros.internal.h"
|
#include "libc/sysv/macros.internal.h"
|
||||||
.scall sys_futex_cp,0x8a68539c6ffff8ca,2146,4095,globl,hidden
|
.scall sys_futex_cp,0x8a68539c62a038ca,2146,2563,globl,hidden
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
//
|
//
|
||||||
// The returned value follows the Linux kernel convention ie
|
// The returned value follows the Linux kernel convention ie
|
||||||
// errors are returned as `-errno`, rather than -1 w/ errno.
|
// errors are returned as `-errno`, rather than -1 w/ errno.
|
||||||
|
//
|
||||||
|
// This helper should not be used to do cancelation points.
|
||||||
__syscall2:
|
__syscall2:
|
||||||
#ifdef __aarch64__
|
#ifdef __aarch64__
|
||||||
mov x8,x2 // syscall number (linux)
|
mov x8,x2 // syscall number (linux)
|
||||||
|
|
|
@ -26,6 +26,8 @@
|
||||||
//
|
//
|
||||||
// The return value follows the Linux Kernel (System V) ABI
|
// The return value follows the Linux Kernel (System V) ABI
|
||||||
// where -errno is returned, rather than doing -1 w/ errno.
|
// where -errno is returned, rather than doing -1 w/ errno.
|
||||||
|
//
|
||||||
|
// This helper should not be used to do cancelation points.
|
||||||
__syscall3:
|
__syscall3:
|
||||||
#ifdef __aarch64__
|
#ifdef __aarch64__
|
||||||
mov x8,x3 // syscall number (linux)
|
mov x8,x3 // syscall number (linux)
|
||||||
|
|
53
libc/sysv/syscall4.S
Normal file
53
libc/sysv/syscall4.S
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
||||||
|
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2023 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/macros.internal.h"
|
||||||
|
|
||||||
|
// Invokes system call w/ arity of four.
|
||||||
|
//
|
||||||
|
// This function takes four params. The first four, are for
|
||||||
|
// args passed along to the system call. The 5th is for the
|
||||||
|
// the magic number, indicating which system call is called
|
||||||
|
//
|
||||||
|
// The return value follows the Linux Kernel (System V) ABI
|
||||||
|
// where -errno is returned, rather than doing -1 w/ errno.
|
||||||
|
//
|
||||||
|
// This helper should not be used to do cancelation points.
|
||||||
|
__syscall4:
|
||||||
|
#ifdef __aarch64__
|
||||||
|
mov x8,x4 // syscall number (linux)
|
||||||
|
mov x16,x4 // syscall number (xnu)
|
||||||
|
mov x9,0 // clear carry flag
|
||||||
|
adds x9,x9,0 // clear carry flag
|
||||||
|
svc 0
|
||||||
|
bcs 1f
|
||||||
|
ret
|
||||||
|
1: neg x0,x0
|
||||||
|
ret
|
||||||
|
#elif defined(__x86_64__)
|
||||||
|
mov %rcx,%r10 // avoid intel cx clobber
|
||||||
|
mov %r8d,%eax // arg5 -> syscall number
|
||||||
|
clc // linux saves carry flag
|
||||||
|
syscall // bsds set carry on errs
|
||||||
|
jnc 1f
|
||||||
|
neg %rax // normalizes to system v
|
||||||
|
1: ret
|
||||||
|
#else
|
||||||
|
#error "unsupported architecture"
|
||||||
|
#endif
|
||||||
|
.endfn __syscall4,globl
|
|
@ -100,8 +100,8 @@ scall sys_killpg 0x092fff092fffffff 0xfff globl hidden
|
||||||
scall sys_clone 0x11fffffffffff038 0x0dc globl hidden
|
scall sys_clone 0x11fffffffffff038 0x0dc globl hidden
|
||||||
scall sys_tkill 0x13e0771b121480c8 0x082 globl hidden # thr_kill() on FreeBSD; _lwp_kill() on NetBSD; thrkill() on OpenBSD where arg3 should be 0 or tcb; __pthread_kill() on XNU
|
scall sys_tkill 0x13e0771b121480c8 0x082 globl hidden # thr_kill() on FreeBSD; _lwp_kill() on NetBSD; thrkill() on OpenBSD where arg3 should be 0 or tcb; __pthread_kill() on XNU
|
||||||
scall sys_tgkill 0xffffff1e1ffff0ea 0x083 globl hidden # thr_kill2() on FreeBSD
|
scall sys_tgkill 0xffffff1e1ffff0ea 0x083 globl hidden # thr_kill2() on FreeBSD
|
||||||
scall sys_futex 0x0a60531c6ffff0ca 0x062 globl hidden # raises SIGSYS on NetBSD; _umtx_op() on FreeBSD
|
scall sys_futex 0x0a60531c622030ca 0x062 globl hidden # raises SIGSYS on NetBSD; _umtx_op() on FreeBSD
|
||||||
scall sys_futex_cp 0x8a68539c6ffff8ca 0x862 globl hidden # intended for futex wait ops
|
scall sys_futex_cp 0x8a68539c62a038ca 0x862 globl hidden # intended for futex wait ops
|
||||||
scall sys_set_robust_list 0x0a7ffffffffff111 0x063 globl # no wrapper
|
scall sys_set_robust_list 0x0a7ffffffffff111 0x063 globl # no wrapper
|
||||||
scall sys_get_robust_list 0x0a8ffffffffff112 0x064 globl # no wrapper
|
scall sys_get_robust_list 0x0a8ffffffffff112 0x064 globl # no wrapper
|
||||||
scall sys_uname 0x0a4fff0a4ffff03f 0x0a0 globl hidden
|
scall sys_uname 0x0a4fff0a4ffff03f 0x0a0 globl hidden
|
||||||
|
|
|
@ -40,6 +40,7 @@ LIBC_SYSV_A_FILES := \
|
||||||
libc/sysv/restorert.S \
|
libc/sysv/restorert.S \
|
||||||
libc/sysv/syscall2.S \
|
libc/sysv/syscall2.S \
|
||||||
libc/sysv/syscall3.S \
|
libc/sysv/syscall3.S \
|
||||||
|
libc/sysv/syscall4.S \
|
||||||
libc/sysv/systemfive.S \
|
libc/sysv/systemfive.S \
|
||||||
libc/sysv/sysret.c \
|
libc/sysv/sysret.c \
|
||||||
libc/sysv/sysv.c \
|
libc/sysv/sysv.c \
|
||||||
|
@ -170,6 +171,8 @@ o/$(MODE)/libc/sysv/syscall2.o: libc/sysv/syscall2.S
|
||||||
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
||||||
o/$(MODE)/libc/sysv/syscall3.o: libc/sysv/syscall3.S
|
o/$(MODE)/libc/sysv/syscall3.o: libc/sysv/syscall3.S
|
||||||
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
||||||
|
o/$(MODE)/libc/sysv/syscall4.o: libc/sysv/syscall4.S
|
||||||
|
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
||||||
o/$(MODE)/libc/sysv/restorert.o: libc/sysv/restorert.S
|
o/$(MODE)/libc/sysv/restorert.o: libc/sysv/restorert.S
|
||||||
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
@$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) $<
|
||||||
o/$(MODE)/libc/sysv/calls/%.o: libc/sysv/calls/%.S
|
o/$(MODE)/libc/sysv/calls/%.o: libc/sysv/calls/%.S
|
||||||
|
|
|
@ -155,7 +155,7 @@ wontreturn void pthread_exit(void *rc) {
|
||||||
// note that the main thread is joinable by child threads
|
// note that the main thread is joinable by child threads
|
||||||
if (pt->pt_flags & PT_STATIC) {
|
if (pt->pt_flags & PT_STATIC) {
|
||||||
atomic_store_explicit(&tib->tib_tid, 0, memory_order_release);
|
atomic_store_explicit(&tib->tib_tid, 0, memory_order_release);
|
||||||
nsync_futex_wake_(&tib->tib_tid, INT_MAX, !IsWindows());
|
nsync_futex_wake_(&tib->tib_tid, INT_MAX, !IsWindows() && !IsXnu());
|
||||||
_Exit1(0);
|
_Exit1(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ static errno_t _pthread_wait(atomic_int *ctid, struct timespec *abstime) {
|
||||||
if (!(rc = pthread_testcancel_np())) {
|
if (!(rc = pthread_testcancel_np())) {
|
||||||
BEGIN_CANCELLATION_POINT;
|
BEGIN_CANCELLATION_POINT;
|
||||||
while ((x = atomic_load_explicit(ctid, memory_order_acquire))) {
|
while ((x = atomic_load_explicit(ctid, memory_order_acquire))) {
|
||||||
e = nsync_futex_wait_(ctid, x, !IsWindows(), abstime);
|
e = nsync_futex_wait_(ctid, x, !IsWindows() && !IsXnu(), abstime);
|
||||||
if (e == -ECANCELED) {
|
if (e == -ECANCELED) {
|
||||||
rc = ECANCELED;
|
rc = ECANCELED;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -100,7 +100,12 @@ TEST(sigbus, test) {
|
||||||
|
|
||||||
// verify the signal was raised
|
// verify the signal was raised
|
||||||
EXPECT_EQ(SIGBUS, gotsig);
|
EXPECT_EQ(SIGBUS, gotsig);
|
||||||
EXPECT_EQ(BUS_ADRERR, gotcode);
|
if (IsXnuSilicon()) {
|
||||||
|
// TODO: Why does it say it's an alignment error?
|
||||||
|
EXPECT_EQ(BUS_ADRALN, gotcode);
|
||||||
|
} else {
|
||||||
|
EXPECT_EQ(BUS_ADRERR, gotcode);
|
||||||
|
}
|
||||||
|
|
||||||
// clean up
|
// clean up
|
||||||
sigaction(SIGBUS, &oldsa, 0);
|
sigaction(SIGBUS, &oldsa, 0);
|
||||||
|
|
2
third_party/nsync/README.cosmo
vendored
2
third_party/nsync/README.cosmo
vendored
|
@ -19,6 +19,8 @@ LOCAL CHANGES
|
||||||
|
|
||||||
- Double linked list API was so good that it's now in libc
|
- Double linked list API was so good that it's now in libc
|
||||||
|
|
||||||
|
- Support Apple's ulock futexes which are internal but nicer than GCD
|
||||||
|
|
||||||
- Ensure resources such as POSIX semaphores are are released on fork.
|
- Ensure resources such as POSIX semaphores are are released on fork.
|
||||||
|
|
||||||
- Modified *NSYNC to allocate waiter objects on the stack. We need it
|
- Modified *NSYNC to allocate waiter objects on the stack. We need it
|
||||||
|
|
37
third_party/nsync/futex.c
vendored
37
third_party/nsync/futex.c
vendored
|
@ -32,6 +32,7 @@
|
||||||
#include "libc/intrin/atomic.h"
|
#include "libc/intrin/atomic.h"
|
||||||
#include "libc/intrin/describeflags.internal.h"
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
|
#include "libc/intrin/ulock.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
#include "libc/limits.h"
|
#include "libc/limits.h"
|
||||||
#include "libc/nexgen32e/vendor.internal.h"
|
#include "libc/nexgen32e/vendor.internal.h"
|
||||||
|
@ -75,6 +76,12 @@ static void nsync_futex_init_ (void) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (IsXnu ()) {
|
||||||
|
nsync_futex_.is_supported = true;
|
||||||
|
nsync_futex_.timeout_is_relative = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (IsFreebsd ()) {
|
if (IsFreebsd ()) {
|
||||||
nsync_futex_.is_supported = true;
|
nsync_futex_.is_supported = true;
|
||||||
nsync_futex_.FUTEX_PRIVATE_FLAG_ = FUTEX_PRIVATE_FLAG;
|
nsync_futex_.FUTEX_PRIVATE_FLAG_ = FUTEX_PRIVATE_FLAG;
|
||||||
|
@ -256,6 +263,20 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare, const struct time
|
||||||
// Windows 8 futexes don't support multiple processes :(
|
// Windows 8 futexes don't support multiple processes :(
|
||||||
if (pshare) goto Polyfill;
|
if (pshare) goto Polyfill;
|
||||||
rc = nsync_futex_wait_win32_ (w, expect, pshare, timeout, pt);
|
rc = nsync_futex_wait_win32_ (w, expect, pshare, timeout, pt);
|
||||||
|
} else if (IsXnu ()) {
|
||||||
|
uint32_t op, us;
|
||||||
|
if (pshare) {
|
||||||
|
op = UL_COMPARE_AND_WAIT_SHARED;
|
||||||
|
} else {
|
||||||
|
op = UL_COMPARE_AND_WAIT;
|
||||||
|
}
|
||||||
|
if (timeout) {
|
||||||
|
us = timespec_tomicros (*timeout);
|
||||||
|
} else {
|
||||||
|
us = -1u;
|
||||||
|
}
|
||||||
|
rc = ulock_wait (op, w, expect, us);
|
||||||
|
if (rc > 0) rc = 0; // TODO(jart): What does it mean?
|
||||||
} else if (IsFreebsd ()) {
|
} else if (IsFreebsd ()) {
|
||||||
rc = sys_umtx_timedwait_uint (w, expect, pshare, timeout);
|
rc = sys_umtx_timedwait_uint (w, expect, pshare, timeout);
|
||||||
} else {
|
} else {
|
||||||
|
@ -326,6 +347,22 @@ int nsync_futex_wake_ (atomic_int *w, int count, char pshare) {
|
||||||
WakeByAddressAll (w);
|
WakeByAddressAll (w);
|
||||||
}
|
}
|
||||||
rc = 0;
|
rc = 0;
|
||||||
|
} else if (IsXnu ()) {
|
||||||
|
uint32_t op;
|
||||||
|
if (pshare) {
|
||||||
|
op = UL_COMPARE_AND_WAIT_SHARED;
|
||||||
|
} else {
|
||||||
|
op = UL_COMPARE_AND_WAIT;
|
||||||
|
}
|
||||||
|
if (count > 1) {
|
||||||
|
op |= ULF_WAKE_ALL;
|
||||||
|
}
|
||||||
|
rc = ulock_wake (op, w, 0);
|
||||||
|
if (!rc) {
|
||||||
|
rc = 1;
|
||||||
|
} else if (rc == -ENOENT) {
|
||||||
|
rc = 0;
|
||||||
|
}
|
||||||
} else if (IsFreebsd ()) {
|
} else if (IsFreebsd ()) {
|
||||||
if (pshare) {
|
if (pshare) {
|
||||||
fop = UMTX_OP_WAKE;
|
fop = UMTX_OP_WAKE;
|
||||||
|
|
15
third_party/nsync/mu_semaphore.c
vendored
15
third_party/nsync/mu_semaphore.c
vendored
|
@ -20,6 +20,11 @@
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "third_party/nsync/mu_semaphore.internal.h"
|
#include "third_party/nsync/mu_semaphore.internal.h"
|
||||||
|
|
||||||
|
/* Apple's ulock (part by Cosmo futexes) is an internal API, but:
|
||||||
|
1. Unlike GCD it's cancellable, i.e. can be EINTR'd by signals
|
||||||
|
2. We currently always use ulock anyway for joining threads */
|
||||||
|
#define PREFER_GCD_OVER_ULOCK 0
|
||||||
|
|
||||||
asm(".ident\t\"\\n\\n\
|
asm(".ident\t\"\\n\\n\
|
||||||
*NSYNC (Apache 2.0)\\n\
|
*NSYNC (Apache 2.0)\\n\
|
||||||
Copyright 2016 Google, Inc.\\n\
|
Copyright 2016 Google, Inc.\\n\
|
||||||
|
@ -28,7 +33,7 @@ https://github.com/google/nsync\"");
|
||||||
|
|
||||||
/* Initialize *s; the initial value is 0. */
|
/* Initialize *s; the initial value is 0. */
|
||||||
void nsync_mu_semaphore_init (nsync_semaphore *s) {
|
void nsync_mu_semaphore_init (nsync_semaphore *s) {
|
||||||
if (IsXnuSilicon ()) {
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
||||||
return nsync_mu_semaphore_init_gcd (s);
|
return nsync_mu_semaphore_init_gcd (s);
|
||||||
} else if (IsNetbsd ()) {
|
} else if (IsNetbsd ()) {
|
||||||
return nsync_mu_semaphore_init_sem (s);
|
return nsync_mu_semaphore_init_sem (s);
|
||||||
|
@ -39,7 +44,7 @@ void nsync_mu_semaphore_init (nsync_semaphore *s) {
|
||||||
|
|
||||||
/* Releases system resources associated with *s. */
|
/* Releases system resources associated with *s. */
|
||||||
void nsync_mu_semaphore_destroy (nsync_semaphore *s) {
|
void nsync_mu_semaphore_destroy (nsync_semaphore *s) {
|
||||||
if (IsXnuSilicon ()) {
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
||||||
return nsync_mu_semaphore_destroy_gcd (s);
|
return nsync_mu_semaphore_destroy_gcd (s);
|
||||||
} else if (IsNetbsd ()) {
|
} else if (IsNetbsd ()) {
|
||||||
return nsync_mu_semaphore_destroy_sem (s);
|
return nsync_mu_semaphore_destroy_sem (s);
|
||||||
|
@ -53,7 +58,7 @@ void nsync_mu_semaphore_destroy (nsync_semaphore *s) {
|
||||||
errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
||||||
errno_t err;
|
errno_t err;
|
||||||
BEGIN_CANCELLATION_POINT;
|
BEGIN_CANCELLATION_POINT;
|
||||||
if (IsXnuSilicon ()) {
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
||||||
err = nsync_mu_semaphore_p_gcd (s);
|
err = nsync_mu_semaphore_p_gcd (s);
|
||||||
} else if (IsNetbsd ()) {
|
} else if (IsNetbsd ()) {
|
||||||
err = nsync_mu_semaphore_p_sem (s);
|
err = nsync_mu_semaphore_p_sem (s);
|
||||||
|
@ -71,7 +76,7 @@ errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
||||||
errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, nsync_time abs_deadline) {
|
errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, nsync_time abs_deadline) {
|
||||||
errno_t err;
|
errno_t err;
|
||||||
BEGIN_CANCELLATION_POINT;
|
BEGIN_CANCELLATION_POINT;
|
||||||
if (IsXnuSilicon ()) {
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
||||||
err = nsync_mu_semaphore_p_with_deadline_gcd (s, abs_deadline);
|
err = nsync_mu_semaphore_p_with_deadline_gcd (s, abs_deadline);
|
||||||
} else if (IsNetbsd ()) {
|
} else if (IsNetbsd ()) {
|
||||||
err = nsync_mu_semaphore_p_with_deadline_sem (s, abs_deadline);
|
err = nsync_mu_semaphore_p_with_deadline_sem (s, abs_deadline);
|
||||||
|
@ -84,7 +89,7 @@ errno_t nsync_mu_semaphore_p_with_deadline (nsync_semaphore *s, nsync_time abs_d
|
||||||
|
|
||||||
/* Ensure that the count of *s is at least 1. */
|
/* Ensure that the count of *s is at least 1. */
|
||||||
void nsync_mu_semaphore_v (nsync_semaphore *s) {
|
void nsync_mu_semaphore_v (nsync_semaphore *s) {
|
||||||
if (IsXnuSilicon ()) {
|
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
|
||||||
return nsync_mu_semaphore_v_gcd (s);
|
return nsync_mu_semaphore_v_gcd (s);
|
||||||
} else if (IsNetbsd ()) {
|
} else if (IsNetbsd ()) {
|
||||||
return nsync_mu_semaphore_v_sem (s);
|
return nsync_mu_semaphore_v_sem (s);
|
||||||
|
|
Loading…
Reference in a new issue