cosmopolitan/test/libc/intrin/pthread_atfork_test.c
Justine Tunney f7ff77d865
Make fixes and improvements
- Invent iso8601us() for faster timestamps
- Improve --strace descriptions of sigset_t
- Rebuild the Landlock Make bootstrap binary
- Introduce MODE=sysv for non-Windows builds
- Permit OFD fcntl() locks under pledge(flock)
- redbean can now protect your kernel from ddos
- Have vfork() fallback to sys_fork() not fork()
- Change kmalloc() to not die when out of memory
- Improve documentation for some termios functions
- Rewrite putenv() and friends to conform to POSIX
- Fix linenoise + strace verbosity issue on Windows
- Fix regressions in our ability to show backtraces
- Change redbean SetHeader() to no-op if value is nil
- Improve fcntl() so SQLite locks work in non-WAL mode
- Remove some unnecessary work during fork() on Windows
- Create redbean-based SSL reverse proxy for IPv4 TurfWar
- Fix ape/apeinstall.sh warning when using non-bash shells
- Add ProgramTrustedIp(), and IsTrustedIp() APIs to redbean
- Support $PWD, $UID, $GID, and $EUID in command interpreter
- Introduce experimental JTqFpD APE prefix for non-Windows builds
- Invent blackhole daemon for firewalling IP addresses via UNIX named socket
- Add ProgramTokenBucket(), AcquireToken(), and CountTokens() APIs to redbean
2022-10-19 07:19:19 -07:00

113 lines
3.9 KiB
C

/*-*- 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 2022 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/runtime/internal.h"
#include "libc/stdio/lock.internal.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
#include "third_party/nsync/mu.h"
int Ai;
const char *A[16];
// clang-format off
void prepare1(void) { A[Ai++] = "prepare1"; }
void prepare2(void) { A[Ai++] = "prepare2"; }
void parent1(void) { A[Ai++] = "parent1"; }
void parent2(void) { A[Ai++] = "parent2"; }
void child1(void) { A[Ai++] = "child1"; }
void child2(void) { A[Ai++] = "child2"; }
// clang-format on
void *ForceThreadingMode(void *arg) {
return 0;
}
TEST(pthread_atfork, test) {
int pid;
__enable_threads();
SPAWN(fork);
ASSERT_EQ(0, pthread_atfork(prepare1, parent1, child1));
ASSERT_EQ(0, pthread_atfork(prepare2, parent2, child2));
flockfile(stdout);
SPAWN(fork);
flockfile(stdout);
ASSERT_STREQ("prepare2", A[0]);
ASSERT_STREQ("prepare1", A[1]);
ASSERT_STREQ("child1", A[2]);
ASSERT_STREQ("child2", A[3]);
funlockfile(stdout);
EXITS(0);
funlockfile(stdout);
ASSERT_STREQ("prepare2", A[0]);
ASSERT_STREQ("prepare1", A[1]);
ASSERT_STREQ("parent1", A[2]);
ASSERT_STREQ("parent2", A[3]);
EXITS(0);
}
pthread_mutex_t mu;
void mu_lock(void) {
pthread_mutex_lock(&mu);
}
void mu_unlock(void) {
pthread_mutex_unlock(&mu);
}
void mu_funlock(void) {
pthread_mutex_init(&mu, 0);
}
void *Worker(void *arg) {
for (int i = 0; i < 20; ++i) {
mu_lock();
usleep(20);
mu_unlock();
SPAWN(fork);
mu_lock();
usleep(1);
mu_unlock();
mu_lock();
mu_unlock();
EXITS(0);
mu_lock();
mu_unlock();
}
return 0;
}
TEST(pthread_atfork, torture) {
if (IsWindows()) return; // TODO(jart): why do we get EBADF? it worked before
pthread_mutex_init(&mu, 0);
pthread_atfork(mu_lock, mu_unlock, mu_funlock);
int i, n = 4;
pthread_t *t = _gc(malloc(sizeof(pthread_t) * n));
for (i = 0; i < n; ++i) {
ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
}
for (i = 0; i < n; ++i) {
ASSERT_EQ(0, pthread_join(t[i], 0));
}
}