mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-24 14:59:03 +00:00
This change doubles the performance of thread spawning. That's thanks to our new stack manager, which allows us to avoid zeroing stacks. It gives us 15µs spawns rather than 30µs spawns on Linux. Also, pthread_exit() is faster now, since it doesn't need to acquire the pthread GIL. On NetBSD, that helps us avoid allocating too many semaphores. Even if that happens we're now able to survive semaphores running out and even memory running out, when allocating *NSYNC waiter objects. I found a lot more rare bugs in the POSIX threads runtime that could cause things to crash, if you've got dozens of threads all spawning and joining dozens of threads. I want cosmo to be world class production worthy for 2025 so happy holidays all
168 lines
3.7 KiB
C
168 lines
3.7 KiB
C
// Copyright 2024 Justine Alexandra Roberts Tunney
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for
|
|
// any purpose with or without fee is hereby granted, provided that the
|
|
// above copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
#include <cosmo.h>
|
|
#include <signal.h>
|
|
#include <stdatomic.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/mman.h>
|
|
#include <unistd.h>
|
|
|
|
// test that lseek() is shared across execve()
|
|
|
|
__static_yoink("zipos");
|
|
|
|
void on_unexpected_death(int sig) {
|
|
int ws;
|
|
if (wait(&ws) == -1)
|
|
_Exit(33);
|
|
if (!WIFEXITED(ws))
|
|
_Exit(34);
|
|
if (!(WEXITSTATUS(ws) & 255))
|
|
_Exit(35);
|
|
_Exit(WEXITSTATUS(ws));
|
|
}
|
|
|
|
int main() {
|
|
|
|
// TODO(jart): fix flakes
|
|
if (IsWindows())
|
|
return 0;
|
|
|
|
signal(SIGCHLD, on_unexpected_death);
|
|
|
|
// extract test program
|
|
int exefd;
|
|
int zipfd;
|
|
ssize_t got;
|
|
char exepath[] = "/tmp/file_offset_exec_prog.XXXXXX";
|
|
if ((exefd = mkstemp(exepath)) == -1)
|
|
return 2;
|
|
if (fchmod(exefd, 0755))
|
|
return 3;
|
|
if ((zipfd = open("/zip/file_offset_exec_prog", O_RDONLY)) == -1)
|
|
return 4;
|
|
for (;;) {
|
|
char chunk[512];
|
|
if ((got = read(zipfd, chunk, sizeof(chunk))) == -1)
|
|
return 5;
|
|
if (!got)
|
|
break;
|
|
if (write(exefd, chunk, got) != got)
|
|
return 6;
|
|
}
|
|
if (close(zipfd))
|
|
return 7;
|
|
if (close(exefd))
|
|
return 8;
|
|
|
|
// create file shared memory mapping for synchronization
|
|
int mapfd;
|
|
atomic_int *phase;
|
|
char mappath[] = "/tmp/file_offset_exec_phase.XXXXXX";
|
|
if ((mapfd = mkstemp(mappath)) == -1)
|
|
return 9;
|
|
if (ftruncate(mapfd, sizeof(atomic_int)))
|
|
return 10;
|
|
if ((phase = mmap(0, sizeof(atomic_int), PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
mapfd, 0)) == MAP_FAILED)
|
|
return 11;
|
|
|
|
// create test file to which both processes shall be writing
|
|
int fd;
|
|
char path[] = "/tmp/file_offset_exec_file.XXXXXX";
|
|
if ((fd = mkstemp(path)) == -1)
|
|
return 12;
|
|
if (lseek(fd, 0, SEEK_CUR) != 0)
|
|
return 13;
|
|
|
|
// start writing to file
|
|
if (write(fd, "0", 1) != 1)
|
|
return 14;
|
|
if (lseek(fd, 0, SEEK_CUR) != 1)
|
|
return 15;
|
|
|
|
// spawn program
|
|
int pid;
|
|
if ((pid = fork()) == -1)
|
|
return 16;
|
|
if (!pid) {
|
|
char str[2][12];
|
|
char *envs[] = {0};
|
|
char *args[] = {exepath, str[0], str[1], 0};
|
|
sprintf(str[0], "%d", fd);
|
|
sprintf(str[1], "%d", mapfd);
|
|
execve(exepath, args, envs);
|
|
_Exit(17);
|
|
}
|
|
|
|
for (;;)
|
|
if (*phase == 1)
|
|
break;
|
|
|
|
if (write(fd, "2", 1) != 1)
|
|
return 18;
|
|
if (lseek(fd, 0, SEEK_CUR) != 3)
|
|
return 19;
|
|
|
|
*phase = 2;
|
|
for (;;)
|
|
if (*phase == 3)
|
|
break;
|
|
|
|
if (write(fd, "4", 1) != 1)
|
|
return 20;
|
|
if (lseek(fd, 0, SEEK_CUR) != 5)
|
|
return 21;
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
*phase = 4;
|
|
|
|
int ws;
|
|
if (wait(&ws) == -1)
|
|
return 22;
|
|
if (!WIFEXITED(ws))
|
|
return 23;
|
|
if (WEXITSTATUS(ws))
|
|
return WEXITSTATUS(ws);
|
|
|
|
char buf[16] = {0};
|
|
if (pread(fd, buf, 15, 0) != 5)
|
|
return 24;
|
|
if (lseek(fd, 0, SEEK_CUR) != 5)
|
|
return 25;
|
|
|
|
if (close(fd))
|
|
return 26;
|
|
|
|
if (unlink(path))
|
|
return 27;
|
|
|
|
if (unlink(exepath))
|
|
return 28;
|
|
|
|
if (munmap(phase, sizeof(atomic_int)))
|
|
return 29;
|
|
|
|
if (close(mapfd))
|
|
return 30;
|
|
|
|
if (unlink(mappath))
|
|
return 31;
|
|
|
|
if (strcmp(buf, "01234"))
|
|
return 32;
|
|
}
|