2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/internal.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/fd.internal.h"
|
|
|
|
#include "libc/calls/struct/iovec.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/calls/ttydefaults.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/errno.h"
|
2023-09-19 18:42:38 +00:00
|
|
|
#include "libc/fmt/itoa.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
|
|
|
#include "libc/intrin/dll.h"
|
2023-10-04 05:34:45 +00:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2023-08-18 11:55:57 +00:00
|
|
|
#include "libc/intrin/nomultics.internal.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2023-07-23 17:57:18 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2023-09-21 20:28:52 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2023-08-19 13:41:06 +00:00
|
|
|
#include "libc/nt/console.h"
|
2023-10-04 05:34:45 +00:00
|
|
|
#include "libc/nt/enum/consolemodeflags.h"
|
2022-04-16 17:40:23 +00:00
|
|
|
#include "libc/nt/enum/filetype.h"
|
2023-09-19 18:42:38 +00:00
|
|
|
#include "libc/nt/enum/vk.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/nt/enum/wait.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/nt/events.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/files.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
2023-09-19 18:42:38 +00:00
|
|
|
#include "libc/nt/struct/inputrecord.h"
|
2022-04-15 06:39:48 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/nt/thread.h"
|
2023-09-07 23:03:19 +00:00
|
|
|
#include "libc/nt/thunk/msabi.h"
|
2023-08-08 11:00:29 +00:00
|
|
|
#include "libc/str/str.h"
|
2023-09-19 18:42:38 +00:00
|
|
|
#include "libc/str/utf16.h"
|
Support non-blocking i/o across platforms
This change introduces new tests for `O_NONBLOCK` and `SOCK_NONBLOCK` to
confirm that non-blocking i/o is now working on all supported platforms,
including Windows. For example, you can now say on Windows, MacOS, etc.:
socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
To create a non-blocking IPv4 TCP socket. Or you can enable non-blocking
i/o on an existing socket / pipe / etc. file descriptor by calling fcntl
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
This functionality is polyfilled on older Linux kernels too, e.g. RHEL5.
Now that fcntl() support is much better the FIOCLEX / FIONCLEX polyfills
for ioctl() have been removed since they're ugly non-POSIX diameond APIs
This change fixes a weakness in kprintf() that was causing Windows trace
tools to frequently crash.
2023-07-23 09:56:47 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2023-08-18 11:55:57 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
#include "libc/sysv/consts/termios.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2023-09-21 14:30:39 +00:00
|
|
|
#include "libc/thread/thread.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2023-08-18 11:55:57 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2023-09-19 18:42:38 +00:00
|
|
|
static const struct {
|
2023-09-21 14:30:39 +00:00
|
|
|
int vk;
|
|
|
|
int normal_str;
|
|
|
|
int shift_str;
|
|
|
|
int ctrl_str;
|
|
|
|
int shift_ctrl_str;
|
2023-09-19 18:42:38 +00:00
|
|
|
} kVirtualKey[] = {
|
2023-09-21 14:30:39 +00:00
|
|
|
#define S(s) W(s "\0\0")
|
|
|
|
#define W(s) (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0])
|
|
|
|
{kNtVkUp, S("A"), S("1;2A"), S("1;5A"), S("1;6A")},
|
|
|
|
{kNtVkDown, S("B"), S("1;2B"), S("1;5B"), S("1;6B")},
|
|
|
|
{kNtVkLeft, S("D"), S("1;2D"), S("1;5D"), S("1;6D")},
|
|
|
|
{kNtVkRight, S("C"), S("1;2C"), S("1;5C"), S("1;6C")},
|
|
|
|
{kNtVkInsert, S("2~"), S("2;2~"), S("2;5~"), S("2;6~")},
|
|
|
|
{kNtVkDelete, S("3~"), S("3;2~"), S("3;5~"), S("3;6~")},
|
|
|
|
{kNtVkHome, S("H"), S("1;2H"), S("1;5H"), S("1;6H")},
|
|
|
|
{kNtVkEnd, S("F"), S("1;2F"), S("1;5F"), S("1;6F")},
|
|
|
|
{kNtVkPrior, S("5~"), S("5;2~"), S("5;5~"), S("5;6~")},
|
|
|
|
{kNtVkNext, S("6~"), S("6;2~"), S("6;5~"), S("6;6~")},
|
|
|
|
{kNtVkF1, -S("OP"), S("1;2P"), S("11^"), S("1;6P")},
|
|
|
|
{kNtVkF2, -S("OQ"), S("1;2Q"), S("12^"), S("1;6Q")},
|
|
|
|
{kNtVkF3, -S("OR"), S("1;2R"), S("13^"), S("1;6R")},
|
|
|
|
{kNtVkF4, -S("OS"), S("1;2S"), S("14^"), S("1;6S")},
|
|
|
|
{kNtVkF5, S("15~"), S("28~"), S("15^"), S("28^")},
|
|
|
|
{kNtVkF6, S("17~"), S("29~"), S("17^"), S("29^")},
|
|
|
|
{kNtVkF7, S("18~"), S("31~"), S("18^"), S("31^")},
|
|
|
|
{kNtVkF8, S("19~"), S("32~"), S("19^"), S("32^")},
|
|
|
|
{kNtVkF9, S("20~"), S("33~"), S("20^"), S("33^")},
|
|
|
|
{kNtVkF10, S("21~"), S("34~"), S("21^"), S("34^")},
|
|
|
|
{kNtVkF11, S("23~"), S("23$"), S("23^"), S("23@")},
|
|
|
|
{kNtVkF12, S("24~"), S("24$"), S("24^"), S("24@")},
|
|
|
|
#undef W
|
|
|
|
#undef S
|
2023-09-19 18:42:38 +00:00
|
|
|
};
|
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
#define KEYSTROKE_CONTAINER(e) DLL_CONTAINER(struct Keystroke, elem, e)
|
|
|
|
|
|
|
|
struct Keystroke {
|
2023-10-04 05:34:45 +00:00
|
|
|
char buf[23];
|
|
|
|
unsigned char buflen;
|
2023-09-21 14:30:39 +00:00
|
|
|
struct Dll elem;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Keystrokes {
|
|
|
|
struct Dll *list;
|
2023-10-04 05:34:45 +00:00
|
|
|
struct Dll *line;
|
2023-09-21 14:30:39 +00:00
|
|
|
struct Dll *free;
|
|
|
|
bool end_of_file;
|
2023-10-04 05:34:45 +00:00
|
|
|
unsigned char pc;
|
2023-09-21 14:30:39 +00:00
|
|
|
uint16_t utf16hs;
|
|
|
|
pthread_mutex_t lock;
|
2023-10-04 06:53:59 +00:00
|
|
|
struct Keystroke pool[512];
|
2023-09-21 14:30:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct Keystrokes __keystroke;
|
|
|
|
|
|
|
|
static textwindows void LockKeystrokes(void) {
|
|
|
|
pthread_mutex_lock(&__keystroke.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows void UnlockKeystrokes(void) {
|
|
|
|
pthread_mutex_unlock(&__keystroke.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows uint64_t BlockSignals(void) {
|
|
|
|
return atomic_exchange(&__get_tls()->tib_sigmask, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows void UnblockSignals(uint64_t mask) {
|
|
|
|
atomic_store_explicit(&__get_tls()->tib_sigmask, mask, memory_order_release);
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
static textwindows int RaiseSignal(int sig) {
|
|
|
|
__get_tls()->tib_sigpending |= 1ull << (sig - 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
static textwindows int GetVirtualKey(uint16_t vk, bool shift, bool ctrl) {
|
2023-09-19 18:42:38 +00:00
|
|
|
for (int i = 0; i < ARRAYLEN(kVirtualKey); ++i) {
|
|
|
|
if (kVirtualKey[i].vk == vk) {
|
|
|
|
if (shift && ctrl) {
|
|
|
|
return kVirtualKey[i].shift_ctrl_str;
|
|
|
|
} else if (shift) {
|
|
|
|
return kVirtualKey[i].shift_str;
|
|
|
|
} else if (ctrl) {
|
|
|
|
return kVirtualKey[i].ctrl_str;
|
|
|
|
} else {
|
|
|
|
return kVirtualKey[i].normal_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
static textwindows int ProcessKeyEvent(const struct NtInputRecord *r, char *p) {
|
2023-09-19 18:42:38 +00:00
|
|
|
|
|
|
|
uint16_t c = r->Event.KeyEvent.uChar.UnicodeChar;
|
|
|
|
uint16_t vk = r->Event.KeyEvent.wVirtualKeyCode;
|
|
|
|
uint16_t cks = r->Event.KeyEvent.dwControlKeyState;
|
|
|
|
|
|
|
|
// ignore keyup events
|
|
|
|
if (!r->Event.KeyEvent.bKeyDown && (!c || vk != kNtVkMenu)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
// process arrow keys, function keys, etc.
|
2023-09-21 14:30:39 +00:00
|
|
|
int n = 0;
|
2023-09-19 18:42:38 +00:00
|
|
|
if (!c) {
|
2023-09-21 14:30:39 +00:00
|
|
|
int w;
|
2023-09-19 18:42:38 +00:00
|
|
|
w = GetVirtualKey(vk, !!(cks & kNtShiftPressed),
|
|
|
|
!!(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed)));
|
|
|
|
if (!w) return 0;
|
|
|
|
p[n++] = 033;
|
|
|
|
if (cks & (kNtLeftAltPressed | kNtRightAltPressed)) {
|
|
|
|
p[n++] = 033;
|
|
|
|
}
|
2023-09-21 14:30:39 +00:00
|
|
|
if (w > 0) {
|
|
|
|
p[n++] = '[';
|
|
|
|
} else {
|
|
|
|
w = -w;
|
|
|
|
}
|
2023-09-19 18:42:38 +00:00
|
|
|
do p[n++] = w;
|
|
|
|
while ((w >>= 8));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate utf-16 into utf-32
|
|
|
|
if (IsHighSurrogate(c)) {
|
2023-09-21 14:30:39 +00:00
|
|
|
__keystroke.utf16hs = c;
|
2023-09-19 18:42:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (IsLowSurrogate(c)) {
|
2023-09-21 14:30:39 +00:00
|
|
|
c = MergeUtf16(__keystroke.utf16hs, c);
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enter sends \r in a raw terminals
|
|
|
|
// make it a multics newline instead
|
2023-10-04 05:34:45 +00:00
|
|
|
if (c == '\r' && !(__ttyconf.magic & kTtyNoCr2Nl)) {
|
2023-09-19 18:42:38 +00:00
|
|
|
c = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
// microsoft doesn't encode ctrl-space (^@) as nul
|
|
|
|
// detecting it is also impossible w/ kNtEnableVirtualTerminalInput
|
|
|
|
if (c == ' ' && (cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
|
|
|
|
c = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// make it possible to distinguish ctrl-h (^H) from backspace (^?)
|
2023-10-04 06:53:59 +00:00
|
|
|
if (c == kNtVkBack && !(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
|
2023-09-19 18:42:38 +00:00
|
|
|
c = 0177;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle ctrl-c and ctrl-\, which tcsetattr() is able to remap
|
2023-10-04 05:34:45 +00:00
|
|
|
if (c && !(__ttyconf.magic & kTtyNoIsigs)) {
|
|
|
|
if (c == __ttyconf.vintr) {
|
|
|
|
return RaiseSignal(SIGINT);
|
|
|
|
} else if (c == __ttyconf.vquit) {
|
|
|
|
return RaiseSignal(SIGQUIT);
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
// handle ctrl-d which generates end-of-file, unless pending line data
|
|
|
|
// is present, in which case we flush that without the newline instead
|
|
|
|
if (c && c == __ttyconf.veof && !(__ttyconf.magic & kTtyUncanon)) {
|
|
|
|
if (dll_is_empty(__keystroke.line)) {
|
2023-09-21 17:10:20 +00:00
|
|
|
__keystroke.end_of_file = true;
|
2023-10-04 05:34:45 +00:00
|
|
|
} else {
|
|
|
|
dll_make_last(&__keystroke.list, __keystroke.line);
|
|
|
|
__keystroke.line = 0;
|
2023-09-21 17:10:20 +00:00
|
|
|
}
|
2023-10-04 05:34:45 +00:00
|
|
|
return 0;
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert esc prefix when alt is held
|
|
|
|
if ((cks & (kNtLeftAltPressed | kNtRightAltPressed)) &&
|
|
|
|
!(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed)) &&
|
|
|
|
r->Event.KeyEvent.bKeyDown) {
|
|
|
|
p[n++] = 033;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert utf-32 to utf-8
|
|
|
|
uint64_t w = tpenc(c);
|
|
|
|
do p[n++] = w;
|
|
|
|
while ((w >>= 8));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To use the tty mouse events feature:
|
|
|
|
// - write(1, "\e[?1000;1002;1015;1006h") to enable
|
|
|
|
// - write(1, "\e[?1000;1002;1015;1006l") to disable
|
|
|
|
// See o//examples/ttyinfo.com and o//tool/viz/life.com
|
2023-09-21 14:30:39 +00:00
|
|
|
static textwindows int ProcessMouseEvent(const struct NtInputRecord *r,
|
|
|
|
char *b) {
|
2023-09-19 18:42:38 +00:00
|
|
|
char *p = b;
|
2023-10-04 05:34:45 +00:00
|
|
|
unsigned char e = 0;
|
|
|
|
uint32_t currentbs = __ttyconf.mousebs;
|
2023-09-19 18:42:38 +00:00
|
|
|
uint32_t ev = r->Event.MouseEvent.dwEventFlags;
|
|
|
|
uint32_t bs = r->Event.MouseEvent.dwButtonState;
|
|
|
|
ev &= kNtMouseMoved | kNtMouseWheeled;
|
|
|
|
bs &= kNtFromLeft1stButtonPressed | kNtRightmostButtonPressed;
|
|
|
|
if (ev & kNtMouseWheeled) {
|
|
|
|
// scroll wheel (unnatural mode)
|
|
|
|
if (!(r->Event.MouseEvent.dwControlKeyState &
|
|
|
|
(kNtShiftPressed | kNtLeftCtrlPressed | kNtRightCtrlPressed |
|
|
|
|
kNtLeftAltPressed | kNtRightAltPressed))) {
|
|
|
|
bool isup = ((int)r->Event.MouseEvent.dwButtonState >> 16) > 0;
|
2023-10-04 05:34:45 +00:00
|
|
|
if (__ttyconf.magic & kTtyXtMouse) {
|
2023-09-19 18:42:38 +00:00
|
|
|
e = isup ? 80 : 81;
|
|
|
|
goto OutputXtermMouseEvent;
|
|
|
|
} else {
|
|
|
|
// we disable mouse highlighting when the tty is put in raw mode
|
|
|
|
// to mouse wheel events with widely understood vt100 arrow keys
|
|
|
|
*p++ = 033;
|
|
|
|
*p++ = '[';
|
|
|
|
if (isup) {
|
|
|
|
*p++ = 'A'; // \e[A up
|
|
|
|
} else {
|
|
|
|
*p++ = 'B'; // \e[B down
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-04 05:34:45 +00:00
|
|
|
} else if ((bs || currentbs) && (__ttyconf.magic & kTtyXtMouse)) {
|
2023-09-19 18:42:38 +00:00
|
|
|
if (bs && (ev & kNtMouseMoved) && currentbs) {
|
|
|
|
e |= 32; // dragging
|
|
|
|
}
|
|
|
|
if ((bs | currentbs) & kNtRightmostButtonPressed) {
|
|
|
|
e |= 2; // right
|
|
|
|
}
|
|
|
|
OutputXtermMouseEvent:
|
|
|
|
*p++ = 033;
|
|
|
|
*p++ = '[';
|
|
|
|
*p++ = '<';
|
|
|
|
p = FormatInt32(p, e);
|
|
|
|
*p++ = ';';
|
2023-10-04 05:34:45 +00:00
|
|
|
p = FormatInt32(p, (r->Event.MouseEvent.dwMousePosition.X + 1) & 0x7fff);
|
2023-09-19 18:42:38 +00:00
|
|
|
*p++ = ';';
|
2023-10-04 05:34:45 +00:00
|
|
|
p = FormatInt32(p, (r->Event.MouseEvent.dwMousePosition.Y + 1) & 0x7fff);
|
2023-09-19 18:42:38 +00:00
|
|
|
if (!bs && currentbs) {
|
|
|
|
*p++ = 'm'; // up
|
|
|
|
} else {
|
|
|
|
*p++ = 'M'; // down
|
|
|
|
}
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.mousebs = bs;
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
return p - b;
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
static textwindows int ConvertConsoleInputToAnsi(const struct NtInputRecord *r,
|
2023-10-04 05:34:45 +00:00
|
|
|
char p[hasatleast 23]) {
|
2023-09-19 18:42:38 +00:00
|
|
|
switch (r->EventType) {
|
|
|
|
case kNtKeyEvent:
|
2023-09-21 14:30:39 +00:00
|
|
|
return ProcessKeyEvent(r, p);
|
2023-09-19 18:42:38 +00:00
|
|
|
case kNtMouseEvent:
|
2023-09-21 14:30:39 +00:00
|
|
|
return ProcessMouseEvent(r, p);
|
2023-09-19 18:42:38 +00:00
|
|
|
case kNtWindowBufferSizeEvent:
|
2023-10-04 05:34:45 +00:00
|
|
|
return RaiseSignal(SIGWINCH);
|
2023-09-19 18:42:38 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
static textwindows struct Keystroke *NewKeystroke(void) {
|
|
|
|
struct Dll *e;
|
|
|
|
struct Keystroke *k = 0;
|
|
|
|
int i, n = ARRAYLEN(__keystroke.pool);
|
2023-10-04 05:34:45 +00:00
|
|
|
if (atomic_load_explicit(&__keystroke.pc, memory_order_acquire) < n &&
|
|
|
|
(i = atomic_fetch_add(&__keystroke.pc, 1)) < n) {
|
2023-09-21 14:30:39 +00:00
|
|
|
k = __keystroke.pool + i;
|
|
|
|
} else {
|
2023-09-21 20:28:52 +00:00
|
|
|
if ((e = dll_first(__keystroke.free))) {
|
|
|
|
k = KEYSTROKE_CONTAINER(e);
|
|
|
|
dll_remove(&__keystroke.free, &k->elem);
|
|
|
|
}
|
|
|
|
if (!k) {
|
|
|
|
if (_weaken(malloc)) {
|
|
|
|
k = _weaken(malloc)(sizeof(struct Keystroke));
|
|
|
|
} else {
|
|
|
|
enomem();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k) {
|
|
|
|
bzero(k, sizeof(*k));
|
|
|
|
dll_init(&k->elem);
|
2023-09-21 14:30:39 +00:00
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
static textwindows void WriteTty(struct Fd *f, const char *p, size_t n) {
|
|
|
|
int64_t hOutput;
|
|
|
|
uint32_t dwConsoleMode;
|
|
|
|
if (f->kind == kFdConsole) {
|
|
|
|
hOutput = f->extra;
|
|
|
|
} else if (g_fds.p[1].kind == kFdFile &&
|
|
|
|
GetConsoleMode(g_fds.p[1].handle, &dwConsoleMode)) {
|
|
|
|
hOutput = g_fds.p[1].handle;
|
|
|
|
} else if (g_fds.p[2].kind == kFdFile &&
|
|
|
|
GetConsoleMode(g_fds.p[2].handle, &dwConsoleMode)) {
|
|
|
|
hOutput = g_fds.p[2].handle;
|
|
|
|
} else {
|
|
|
|
hOutput = g_fds.p[1].handle;
|
|
|
|
}
|
|
|
|
WriteFile(hOutput, p, n, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows bool IsCtl(int c) {
|
|
|
|
return isascii(c) && iscntrl(c) && c != '\n' && c != '\t';
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows void WriteTtyCtl(struct Fd *f, const char *p, size_t n) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (IsCtl(p[i])) {
|
|
|
|
char ctl[2];
|
|
|
|
ctl[0] = '^';
|
|
|
|
ctl[1] = p[i] ^ 0100;
|
|
|
|
WriteTty(f, ctl, 2);
|
|
|
|
} else {
|
|
|
|
WriteTty(f, p + i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows void EchoTty(struct Fd *f, const char *p, size_t n) {
|
|
|
|
if (__ttyconf.magic & kTtyEchoRaw) {
|
|
|
|
WriteTty(f, p, n);
|
|
|
|
} else {
|
|
|
|
WriteTtyCtl(f, p, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 06:53:59 +00:00
|
|
|
static textwindows bool EraseKeystroke(struct Fd *f) {
|
|
|
|
struct Dll *e;
|
|
|
|
if ((e = dll_last(__keystroke.line))) {
|
|
|
|
struct Keystroke *k = KEYSTROKE_CONTAINER(e);
|
|
|
|
dll_remove(&__keystroke.line, e);
|
|
|
|
dll_make_first(&__keystroke.free, e);
|
|
|
|
for (int i = k->buflen; i--;) {
|
|
|
|
if ((k->buf[i] & 0300) == 0200) continue;
|
|
|
|
WriteTty(f, "\b \b", 3);
|
|
|
|
if (!(__ttyconf.magic & kTtyEchoRaw) && IsCtl(k->buf[i])) {
|
|
|
|
WriteTty(f, "\b \b", 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
static textwindows void IngestConsoleInputRecord(struct Fd *f,
|
|
|
|
struct NtInputRecord *r) {
|
|
|
|
|
|
|
|
// convert win32 console event into ansi
|
2023-09-21 14:30:39 +00:00
|
|
|
int len;
|
2023-10-04 05:34:45 +00:00
|
|
|
char buf[23];
|
|
|
|
if (!(len = ConvertConsoleInputToAnsi(r, buf))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle backspace in canonical mode
|
|
|
|
if (len == 1 && buf[0] && //
|
|
|
|
(buf[0] & 255) == __ttyconf.verase && //
|
|
|
|
!(__ttyconf.magic & kTtyUncanon)) {
|
2023-10-04 06:53:59 +00:00
|
|
|
EraseKeystroke(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle kill in canonical mode
|
|
|
|
if (len == 1 && buf[0] && //
|
|
|
|
(buf[0] & 255) == __ttyconf.vkill && //
|
|
|
|
!(__ttyconf.magic & kTtyUncanon)) {
|
|
|
|
while (EraseKeystroke(f)) {
|
2023-10-04 05:34:45 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate an object to hold this keystroke
|
2023-09-21 14:30:39 +00:00
|
|
|
struct Keystroke *k;
|
2023-10-04 05:34:45 +00:00
|
|
|
if (!(k = NewKeystroke())) {
|
|
|
|
STRACE("ran out of memory to hold keystroke %#.*s", len, buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(k->buf, buf, sizeof(k->buf));
|
|
|
|
k->buflen = len;
|
|
|
|
|
|
|
|
// echo input if it was successfully recorded
|
|
|
|
// assuming the win32 console isn't doing it already
|
|
|
|
if (!(__ttyconf.magic & kTtySilence)) {
|
|
|
|
EchoTty(f, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// save keystroke to appropriate list
|
|
|
|
if (__ttyconf.magic & kTtyUncanon) {
|
|
|
|
dll_make_last(&__keystroke.list, &k->elem);
|
|
|
|
} else {
|
|
|
|
dll_make_last(&__keystroke.line, &k->elem);
|
|
|
|
|
|
|
|
// handle end-of-line in canonical mode
|
|
|
|
if (len == 1 && buf[0] &&
|
|
|
|
((buf[0] & 255) == '\n' || //
|
|
|
|
(buf[0] & 255) == __ttyconf.veol || //
|
|
|
|
(buf[0] & 255) == __ttyconf.veol2)) {
|
|
|
|
dll_make_last(&__keystroke.list, __keystroke.line);
|
|
|
|
__keystroke.line = 0;
|
|
|
|
return;
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
2023-09-21 14:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
static textwindows void IngestConsoleInput(struct Fd *f) {
|
2023-09-21 14:30:39 +00:00
|
|
|
uint32_t i, n;
|
|
|
|
struct NtInputRecord records[16];
|
|
|
|
if (!__keystroke.end_of_file) {
|
|
|
|
do {
|
2023-10-04 06:53:59 +00:00
|
|
|
if (GetNumberOfConsoleInputEvents(f->handle, &n)) {
|
|
|
|
if (n) {
|
|
|
|
n = MIN(ARRAYLEN(records), n);
|
|
|
|
if (ReadConsoleInput(f->handle, records, n, &n)) {
|
|
|
|
for (i = 0; i < n && !__keystroke.end_of_file; ++i) {
|
|
|
|
IngestConsoleInputRecord(f, records + i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
STRACE("ReadConsoleInput failed w/ %d", GetLastError());
|
|
|
|
__keystroke.end_of_file = true;
|
|
|
|
break;
|
|
|
|
}
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
2023-09-21 14:30:39 +00:00
|
|
|
} else {
|
2023-10-04 06:53:59 +00:00
|
|
|
STRACE("GetNumberOfConsoleInputRecords failed w/ %d", GetLastError());
|
2023-09-21 14:30:39 +00:00
|
|
|
__keystroke.end_of_file = true;
|
2023-09-19 18:42:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-09-21 14:30:39 +00:00
|
|
|
} while (n == ARRAYLEN(records));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
textwindows int FlushConsoleInputBytes(int64_t handle) {
|
|
|
|
int rc;
|
|
|
|
uint64_t m;
|
|
|
|
m = BlockSignals();
|
|
|
|
LockKeystrokes();
|
|
|
|
if (FlushConsoleInputBuffer(handle)) {
|
|
|
|
dll_make_first(&__keystroke.free, __keystroke.list);
|
|
|
|
__keystroke.list = 0;
|
2023-10-04 15:07:07 +00:00
|
|
|
dll_make_first(&__keystroke.free, __keystroke.line);
|
|
|
|
__keystroke.line = 0;
|
2023-09-21 14:30:39 +00:00
|
|
|
rc = 0;
|
|
|
|
} else {
|
|
|
|
rc = __winerr();
|
|
|
|
}
|
|
|
|
UnlockKeystrokes();
|
|
|
|
UnblockSignals(m);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
textwindows int CountConsoleInputBytes(struct Fd *f) {
|
2023-09-21 14:30:39 +00:00
|
|
|
int count = 0;
|
|
|
|
struct Dll *e;
|
|
|
|
uint64_t m = BlockSignals();
|
|
|
|
LockKeystrokes();
|
2023-10-04 05:34:45 +00:00
|
|
|
IngestConsoleInput(f);
|
2023-09-21 14:30:39 +00:00
|
|
|
for (e = dll_first(__keystroke.list); e; e = dll_next(__keystroke.list, e)) {
|
|
|
|
count += KEYSTROKE_CONTAINER(e)->buflen;
|
|
|
|
}
|
|
|
|
if (!count && __keystroke.end_of_file) {
|
|
|
|
count = -1;
|
|
|
|
}
|
|
|
|
UnlockKeystrokes();
|
|
|
|
UnblockSignals(m);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
static textwindows bool DigestConsoleInput(char *data, size_t size, int *rc) {
|
|
|
|
|
|
|
|
// handle eof once available input is consumed
|
|
|
|
if (dll_is_empty(__keystroke.list) && __keystroke.end_of_file) {
|
|
|
|
*rc = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy keystroke(s) into user buffer
|
|
|
|
int toto = 0;
|
2023-09-21 14:30:39 +00:00
|
|
|
struct Dll *e;
|
2023-10-04 06:53:59 +00:00
|
|
|
while (size && (e = dll_first(__keystroke.list))) {
|
2023-09-21 14:30:39 +00:00
|
|
|
struct Keystroke *k = KEYSTROKE_CONTAINER(e);
|
|
|
|
uint32_t got = MIN(size, k->buflen);
|
|
|
|
uint32_t remain = k->buflen - got;
|
2023-10-04 05:34:45 +00:00
|
|
|
if (got) {
|
|
|
|
memcpy(data, k->buf, got);
|
|
|
|
data += got;
|
|
|
|
size -= got;
|
|
|
|
toto += got;
|
|
|
|
}
|
|
|
|
if (remain) {
|
|
|
|
memmove(k->buf, k->buf + got, remain);
|
|
|
|
} else {
|
2023-09-21 14:30:39 +00:00
|
|
|
dll_remove(&__keystroke.list, e);
|
|
|
|
dll_make_first(&__keystroke.free, e);
|
|
|
|
}
|
|
|
|
k->buflen = remain;
|
2023-10-04 05:34:45 +00:00
|
|
|
if ((__ttyconf.magic & kTtyUncanon) && toto >= __ttyconf.vmin) {
|
|
|
|
break;
|
2023-09-21 14:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 05:34:45 +00:00
|
|
|
// return result
|
|
|
|
if (toto) {
|
|
|
|
*rc = toto;
|
|
|
|
return true;
|
2023-09-21 14:30:39 +00:00
|
|
|
} else {
|
2023-10-04 05:34:45 +00:00
|
|
|
return false;
|
2023-09-21 14:30:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows ssize_t ReadFromWindowsConsole(struct Fd *f, void *data,
|
|
|
|
size_t size) {
|
|
|
|
int rc = -1;
|
|
|
|
for (;;) {
|
|
|
|
bool done = false;
|
|
|
|
uint64_t m;
|
|
|
|
m = BlockSignals();
|
|
|
|
LockKeystrokes();
|
2023-10-04 05:34:45 +00:00
|
|
|
IngestConsoleInput(f);
|
2023-09-21 14:30:39 +00:00
|
|
|
done = DigestConsoleInput(data, size, &rc);
|
|
|
|
UnlockKeystrokes();
|
|
|
|
UnblockSignals(m);
|
|
|
|
if (done) break;
|
|
|
|
if (f->flags & O_NONBLOCK) return eagain();
|
|
|
|
uint32_t ms = __SIG_POLL_INTERVAL_MS;
|
2023-10-04 05:34:45 +00:00
|
|
|
if (!__ttyconf.vmin) {
|
|
|
|
if (!__ttyconf.vtime) {
|
2023-09-21 14:30:39 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2023-10-04 05:34:45 +00:00
|
|
|
ms = __ttyconf.vtime * 100;
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-21 14:30:39 +00:00
|
|
|
if (_check_interrupts(kSigOpRestartable)) return -1;
|
2023-10-04 06:53:59 +00:00
|
|
|
if (__pause_thread(ms)) {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
errno = EINTR; // TODO(jart): Why do we need it?
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2023-09-19 18:42:38 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-09-07 23:03:19 +00:00
|
|
|
textwindows ssize_t sys_read_nt_impl(int fd, void *data, size_t size,
|
|
|
|
int64_t offset) {
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2023-07-23 17:57:18 +00:00
|
|
|
bool32 ok;
|
2023-09-07 23:03:19 +00:00
|
|
|
struct Fd *f;
|
2023-07-23 17:57:18 +00:00
|
|
|
uint32_t got;
|
2023-08-14 05:42:25 +00:00
|
|
|
int64_t handle;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
struct PosixThread *pt;
|
|
|
|
|
2023-09-07 23:03:19 +00:00
|
|
|
f = g_fds.p + fd;
|
2023-09-19 18:42:38 +00:00
|
|
|
handle = f->handle;
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
pt = _pthread_self();
|
2023-09-19 18:42:38 +00:00
|
|
|
pt->abort_errno = EAGAIN;
|
|
|
|
size = MIN(size, 0x7ffff000);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
|
|
|
|
bool pwriting = offset != -1;
|
|
|
|
bool seekable = f->kind == kFdFile && GetFileType(handle) == kNtFileTypeDisk;
|
|
|
|
bool nonblock = !!(f->flags & O_NONBLOCK);
|
|
|
|
|
|
|
|
if (pwriting && !seekable) {
|
|
|
|
return espipe();
|
|
|
|
}
|
|
|
|
if (!pwriting) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-19 18:42:38 +00:00
|
|
|
uint32_t cm;
|
|
|
|
if (!seekable && (f->kind == kFdConsole || GetConsoleMode(handle, &cm))) {
|
|
|
|
return ReadFromWindowsConsole(f, data, size);
|
2023-08-19 13:41:06 +00:00
|
|
|
}
|
|
|
|
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
if (!pwriting && seekable) {
|
|
|
|
pthread_mutex_lock(&f->lock);
|
|
|
|
offset = f->pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NtOverlapped overlap = {.hEvent = CreateEvent(0, 0, 0, 0),
|
|
|
|
.Pointer = offset};
|
|
|
|
// the win32 manual says it's important to *not* put &got here
|
|
|
|
// since for overlapped i/o, we always use GetOverlappedResult
|
2023-09-19 18:42:38 +00:00
|
|
|
ok = ReadFile(handle, data, size, 0, &overlap);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
if (!ok && GetLastError() == kNtErrorIoPending) {
|
|
|
|
BlockingOperation:
|
|
|
|
if (!nonblock) {
|
|
|
|
pt->ioverlap = &overlap;
|
|
|
|
pt->iohandle = handle;
|
2023-08-08 11:00:29 +00:00
|
|
|
}
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
if (nonblock) {
|
|
|
|
CancelIoEx(handle, &overlap);
|
|
|
|
} else if (_check_interrupts(kSigOpRestartable)) {
|
|
|
|
Interrupted:
|
|
|
|
pt->abort_errno = errno;
|
|
|
|
CancelIoEx(handle, &overlap);
|
|
|
|
} else {
|
|
|
|
for (;;) {
|
|
|
|
uint32_t i;
|
|
|
|
if (g_fds.stdin.inisem) {
|
|
|
|
ReleaseSemaphore(g_fds.stdin.inisem, 1, 0);
|
2023-08-08 11:00:29 +00:00
|
|
|
}
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
i = WaitForSingleObject(overlap.hEvent, __SIG_IO_INTERVAL_MS);
|
|
|
|
if (i == kNtWaitTimeout) {
|
|
|
|
if (_check_interrupts(kSigOpRestartable)) {
|
|
|
|
goto Interrupted;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
2023-09-07 05:39:36 +00:00
|
|
|
}
|
2023-08-08 11:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
pt->ioverlap = 0;
|
|
|
|
pt->iohandle = 0;
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
// overlapped is allocated on stack, so it's important we wait
|
|
|
|
// for windows to acknowledge that it's done using that memory
|
|
|
|
ok = GetOverlappedResult(handle, &overlap, &got, nonblock);
|
|
|
|
if (!ok && GetLastError() == kNtErrorIoIncomplete) {
|
|
|
|
goto BlockingOperation;
|
2023-07-23 17:57:18 +00:00
|
|
|
}
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
}
|
2023-10-03 02:25:19 +00:00
|
|
|
CloseHandle(overlap.hEvent);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
|
|
|
|
if (!pwriting && seekable) {
|
|
|
|
if (ok) f->pointer = offset + got;
|
|
|
|
pthread_mutex_unlock(&f->lock);
|
2022-10-03 05:14:33 +00:00
|
|
|
}
|
2023-08-19 13:41:06 +00:00
|
|
|
|
2022-10-03 05:14:33 +00:00
|
|
|
if (ok) {
|
2021-03-09 19:21:08 +00:00
|
|
|
return got;
|
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2023-09-21 14:30:39 +00:00
|
|
|
errno_t err;
|
|
|
|
if (_weaken(pthread_testcancel_np) &&
|
|
|
|
(err = _weaken(pthread_testcancel_np)())) {
|
|
|
|
return ecanceled();
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:56:53 +00:00
|
|
|
switch (GetLastError()) {
|
|
|
|
case kNtErrorBrokenPipe: // broken pipe
|
|
|
|
case kNtErrorNoData: // closing named pipe
|
|
|
|
case kNtErrorHandleEof: // pread read past EOF
|
|
|
|
return 0; //
|
|
|
|
case kNtErrorAccessDenied: // read doesn't return EACCESS
|
|
|
|
return ebadf(); //
|
2023-08-08 11:00:29 +00:00
|
|
|
case kNtErrorOperationAborted:
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
errno = pt->abort_errno;
|
2023-08-08 11:00:29 +00:00
|
|
|
return -1;
|
2022-04-20 16:56:53 +00:00
|
|
|
default:
|
|
|
|
return __winerr();
|
|
|
|
}
|
2021-03-09 19:21:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 23:03:19 +00:00
|
|
|
textwindows ssize_t sys_read_nt(int fd, const struct iovec *iov, size_t iovlen,
|
|
|
|
int64_t opt_offset) {
|
2021-03-09 19:21:08 +00:00
|
|
|
ssize_t rc;
|
2021-02-07 14:11:44 +00:00
|
|
|
size_t i, total;
|
2022-09-15 04:29:50 +00:00
|
|
|
if (opt_offset < -1) return einval();
|
2020-06-15 14:18:57 +00:00
|
|
|
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
|
2021-02-07 14:11:44 +00:00
|
|
|
if (iovlen) {
|
|
|
|
for (total = i = 0; i < iovlen; ++i) {
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
// TODO(jart): disable cancelations after first iteration
|
2021-03-09 19:21:08 +00:00
|
|
|
if (!iov[i].iov_len) continue;
|
|
|
|
rc = sys_read_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset);
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
if (rc == -1) {
|
|
|
|
if (total && errno != ECANCELED) {
|
|
|
|
return total;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 19:21:08 +00:00
|
|
|
total += rc;
|
|
|
|
if (opt_offset != -1) opt_offset += rc;
|
|
|
|
if (rc < iov[i].iov_len) break;
|
2021-02-07 14:11:44 +00:00
|
|
|
}
|
|
|
|
return total;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2021-03-09 19:21:08 +00:00
|
|
|
return sys_read_nt_impl(fd, NULL, 0, opt_offset);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-18 11:55:57 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|