mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-05 04:02:28 +00:00
Eliminate cyclic locks in runtime
This change introduces a new deadlock detector for Cosmo's POSIX threads implementation. Error check mutexes will now track a DAG of nested locks and report EDEADLK when a deadlock is theoretically possible. These will occur rarely, but it's important for production hardening your code. You don't even need to change your mutexes to use the POSIX error check mode because `cosmocc -mdbg` will enable error checking on mutexes by default globally. When cycles are found, an error message showing your demangled symbols describing the strongly connected component are printed and then the SIGTRAP is raised, which means you'll also get a backtrace if you're using ShowCrashReports() too. This new error checker is so low-level and so pure that it's able to verify the relationships of every libc runtime lock, including those locks upon which the mutex implementation depends.
This commit is contained in:
parent
26c051c297
commit
af7bd80430
141 changed files with 2094 additions and 1601 deletions
|
@ -64,6 +64,14 @@
|
|||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
if (pledge(0, 0) == -1) {
|
||||
fprintf(stderr, "warning: pledge() not supported on this system %m\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
void SetUp(void) {
|
||||
__pledge_mode = PLEDGE_PENALTY_RETURN_EPERM;
|
||||
}
|
||||
|
||||
void OnSig(int sig) {
|
||||
|
@ -72,16 +80,6 @@ void OnSig(int sig) {
|
|||
|
||||
int sys_memfd_secret(unsigned int); // our ENOSYS threshold
|
||||
|
||||
void SetUp(void) {
|
||||
if (pledge(0, 0) == -1) {
|
||||
fprintf(stderr, "warning: pledge() not supported on this system %m\n");
|
||||
exit(0);
|
||||
}
|
||||
testlib_extract("/zip/life.elf", "life.elf", 0755);
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
__pledge_mode = PLEDGE_PENALTY_RETURN_EPERM;
|
||||
}
|
||||
|
||||
TEST(pledge, default_allowsExit) {
|
||||
int *job;
|
||||
int ws, pid;
|
||||
|
@ -107,6 +105,7 @@ TEST(pledge, execpromises_notok) {
|
|||
if (IsOpenbsd())
|
||||
return; // b/c testing linux bpf
|
||||
int ws, pid;
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
putenv("COMDBG=REDACTED");
|
||||
|
@ -532,6 +531,7 @@ TEST(pledge, open_cpath) {
|
|||
TEST(pledge, execpromises_ok) {
|
||||
if (IsOpenbsd())
|
||||
return; // b/c testing linux bpf
|
||||
testlib_extract("/zip/life.elf", "life.elf", 0755);
|
||||
int ws, pid;
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
|
@ -549,6 +549,7 @@ TEST(pledge, execpromises_notok1) {
|
|||
if (IsOpenbsd())
|
||||
return; // b/c testing linux bpf
|
||||
int ws, pid;
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
putenv("COMDBG=REDACTED");
|
||||
|
@ -565,6 +566,7 @@ TEST(pledge, execpromises_reducesAtExecOnLinux) {
|
|||
if (IsOpenbsd())
|
||||
return; // b/c testing linux bpf
|
||||
int ws, pid;
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
putenv("COMDBG=REDACTED");
|
||||
|
@ -583,6 +585,7 @@ TEST(pledge_openbsd, execpromisesIsNull_letsItDoAnything) {
|
|||
if (!IsOpenbsd())
|
||||
return;
|
||||
int ws, pid;
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
ASSERT_SYS(0, 0, pledge("stdio exec", 0));
|
||||
|
@ -602,6 +605,7 @@ TEST(pledge_openbsd, execpromisesIsSuperset_letsItDoAnything) {
|
|||
if (!IsOpenbsd())
|
||||
return;
|
||||
int ws, pid;
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
ASSERT_SYS(0, 0, pledge("stdio rpath exec", "stdio rpath tty inet"));
|
||||
|
@ -623,6 +627,7 @@ TEST(pledge_openbsd, execpromises_notok) {
|
|||
if (IsOpenbsd())
|
||||
return; // mimmutable() ugh
|
||||
int ws, pid;
|
||||
testlib_extract("/zip/sock.elf", "sock.elf", 0755);
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
putenv("COMDBG=REDACTED");
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/siginfo.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/sysv/consts/sicode.h"
|
||||
|
@ -30,6 +31,7 @@
|
|||
#include "libc/thread/thread.h"
|
||||
|
||||
TEST(raise, trap) {
|
||||
AssertNoLocksAreHeld();
|
||||
signal(SIGTRAP, SIG_DFL);
|
||||
SPAWN(fork);
|
||||
raise(SIGTRAP);
|
||||
|
@ -44,6 +46,7 @@ TEST(raise, fpe) {
|
|||
}
|
||||
|
||||
TEST(raise, usr1) {
|
||||
AssertNoLocksAreHeld();
|
||||
SPAWN(fork);
|
||||
raise(SIGUSR1);
|
||||
TERMS(SIGUSR1);
|
||||
|
@ -69,6 +72,7 @@ void *Worker(void *arg) {
|
|||
|
||||
TEST(raise, threaded) {
|
||||
SPAWN(fork);
|
||||
AssertNoLocksAreHeld();
|
||||
signal(SIGILL, SIG_DFL);
|
||||
pthread_t worker;
|
||||
ASSERT_EQ(0, pthread_create(&worker, 0, Worker, 0));
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
|
@ -28,8 +29,10 @@
|
|||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
|
@ -62,6 +65,9 @@ pthread_mutex_t mu;
|
|||
__assert_eq_fail(__FILE__, __LINE__, #WANT, #GOT, _want, _got); \
|
||||
} while (0)
|
||||
|
||||
void ignore_signal(int sig) {
|
||||
}
|
||||
|
||||
void __assert_eq_fail(const char *file, int line, const char *wantstr,
|
||||
const char *gotstr, long want, long got) {
|
||||
kprintf("%s:%d: %s vs. %s was %ld vs. %ld (%s)\n", file, line, wantstr,
|
||||
|
@ -177,6 +183,12 @@ void TestUncontendedLock(const char *name, int kind) {
|
|||
int main(int argc, char *argv[]) {
|
||||
pthread_mutexattr_t attr;
|
||||
|
||||
#ifdef MODE_DBG
|
||||
GetSymbolTable();
|
||||
signal(SIGTRAP, ignore_signal);
|
||||
kprintf("running %s\n", argv[0]);
|
||||
#endif
|
||||
|
||||
#ifdef __aarch64__
|
||||
// our usage of raw clone() is probably broken in aarch64
|
||||
// we should just get rid of clone()
|
||||
|
@ -190,7 +202,7 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&mu, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&mu));
|
||||
|
@ -216,28 +228,12 @@ int main(int argc, char *argv[]) {
|
|||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&mu));
|
||||
|
||||
ASSERT_EQ(1, __tls_enabled);
|
||||
|
||||
TestUncontendedLock("PTHREAD_MUTEX_NORMAL RAW TLS", PTHREAD_MUTEX_NORMAL);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_DEFAULT RAW TLS", PTHREAD_MUTEX_DEFAULT);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_RECURSIVE RAW TLS",
|
||||
PTHREAD_MUTEX_RECURSIVE);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_ERRORCHECK RAW TLS",
|
||||
PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
TestContendedLock("PTHREAD_MUTEX_NORMAL RAW TLS", PTHREAD_MUTEX_NORMAL);
|
||||
TestContendedLock("PTHREAD_MUTEX_DEFAULT RAW TLS", PTHREAD_MUTEX_DEFAULT);
|
||||
TestContendedLock("PTHREAD_MUTEX_RECURSIVE RAW TLS", PTHREAD_MUTEX_RECURSIVE);
|
||||
TestContendedLock("PTHREAD_MUTEX_ERRORCHECK RAW TLS",
|
||||
PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
__tls_enabled_set(false);
|
||||
|
||||
TestUncontendedLock("PTHREAD_MUTEX_NORMAL RAW", PTHREAD_MUTEX_NORMAL);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_RECURSIVE RAW", PTHREAD_MUTEX_RECURSIVE);
|
||||
TestUncontendedLock("PTHREAD_MUTEX_ERRORCHECK RAW", PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
TestContendedLock("PTHREAD_MUTEX_NORMAL RAW", PTHREAD_MUTEX_NORMAL);
|
||||
TestContendedLock("PTHREAD_MUTEX_RECURSIVE RAW", PTHREAD_MUTEX_RECURSIVE);
|
||||
TestContendedLock("PTHREAD_MUTEX_ERRORCHECK RAW", PTHREAD_MUTEX_ERRORCHECK);
|
||||
|
||||
//
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ TEST(lockipc, mutex) {
|
|||
// create shared mutex
|
||||
pthread_mutexattr_t mattr;
|
||||
pthread_mutexattr_init(&mattr);
|
||||
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_DEFAULT);
|
||||
pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
|
||||
pthread_mutex_init(&shm->mutex, &mattr);
|
||||
pthread_mutexattr_destroy(&mattr);
|
||||
|
|
|
@ -66,9 +66,11 @@ TEST(bzero, hug) {
|
|||
|
||||
#define N (256 * 1024 * 1024)
|
||||
|
||||
BENCH(strlen, bench) {
|
||||
BENCH(memset, bench) {
|
||||
void *memset_(void *, int, size_t) asm("memset");
|
||||
printf("\n");
|
||||
static char A[N];
|
||||
memset(A, 2, N);
|
||||
for (int n = 1; n <= N; n *= 2)
|
||||
BENCHMARK(100, n, X(memset(V(A), 1, n)));
|
||||
BENCHMARK(100, n, X(memset_(V(A), 0, n)));
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ pthread_mutexattr_t attr;
|
|||
|
||||
FIXTURE(pthread_mutex_lock, normal) {
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ TEST(pthread_mutex_lock, contention) {
|
|||
int i;
|
||||
pthread_t *th = gc(malloc(sizeof(pthread_t) * THREADS));
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
||||
pthread_mutex_init(&lock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
count = 0;
|
||||
|
@ -128,7 +128,7 @@ BENCH(pthread_mutex_lock, bench_uncontended) {
|
|||
pthread_mutex_t m;
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
||||
pthread_mutex_init(&m, &attr);
|
||||
EZBENCH2("normal 1x", donothing, BenchLockUnlock(&m));
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ BENCH(pthread_mutex_lock, bench_contended) {
|
|||
pthread_mutex_t m;
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
||||
pthread_mutex_init(&m, &attr);
|
||||
struct MutexContentionArgs a = {&m};
|
||||
pthread_create(&t, 0, MutexContentionWorker, &a);
|
||||
|
|
|
@ -20,12 +20,16 @@
|
|||
#include "libc/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/strace.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/math.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
@ -34,6 +38,7 @@
|
|||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/rlimit.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
@ -48,16 +53,38 @@
|
|||
int count;
|
||||
atomic_int started;
|
||||
atomic_int finished;
|
||||
pthread_mutex_t lock;
|
||||
pthread_mutex_t mylock;
|
||||
pthread_spinlock_t slock;
|
||||
pthread_t th[THREADS];
|
||||
|
||||
void ignore_signal(int sig) {
|
||||
}
|
||||
|
||||
void SetUpOnce(void) {
|
||||
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
|
||||
kprintf("running %s\n", program_invocation_name);
|
||||
signal(SIGTRAP, ignore_signal);
|
||||
}
|
||||
|
||||
TEST(pthread_mutex_lock, default) {
|
||||
pthread_mutexattr_t attr;
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&lock, 0));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&lock));
|
||||
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_trylock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&lock));
|
||||
}
|
||||
|
||||
TEST(pthread_mutex_lock, normal) {
|
||||
pthread_mutex_t lock;
|
||||
pthread_mutexattr_t attr;
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL));
|
||||
|
@ -75,7 +102,6 @@ TEST(pthread_mutex_lock, normal) {
|
|||
}
|
||||
|
||||
TEST(pthread_mutex_lock, recursive) {
|
||||
pthread_mutex_t lock;
|
||||
pthread_mutexattr_t attr;
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE));
|
||||
|
@ -99,15 +125,15 @@ TEST(pthread_mutex_lock, recursive) {
|
|||
}
|
||||
|
||||
TEST(pthread_mutex_lock, errorcheck) {
|
||||
pthread_mutex_t lock;
|
||||
pthread_mutexattr_t attr;
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&lock, &attr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&lock));
|
||||
ASSERT_EQ(1, __deadlock_tracked(&lock));
|
||||
ASSERT_EQ(EDEADLK, pthread_mutex_lock(&lock));
|
||||
ASSERT_EQ(EDEADLK, pthread_mutex_trylock(&lock));
|
||||
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&lock));
|
||||
}
|
||||
|
@ -130,7 +156,7 @@ TEST(pthread_mutex_lock, contention) {
|
|||
int i;
|
||||
pthread_mutexattr_t attr;
|
||||
pthread_mutexattr_init(&attr);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
||||
pthread_mutex_init(&mylock, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
count = 0;
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/intrin/safemacros.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
@ -33,8 +34,8 @@
|
|||
void *Worker(void *arg) {
|
||||
for (int i = 0; i < ITERATIONS; ++i) {
|
||||
char *p;
|
||||
ASSERT_NE(NULL, (p = malloc(lemur64() % SIZE)));
|
||||
ASSERT_NE(NULL, (p = realloc(p, max(lemur64() % SIZE, 1))));
|
||||
ASSERT_NE(NULL, (p = malloc(rand() % SIZE)));
|
||||
ASSERT_NE(NULL, (p = realloc(p, rand() % SIZE)));
|
||||
free(p);
|
||||
}
|
||||
return 0;
|
||||
|
@ -48,6 +49,7 @@ TEST(malloc, torture) {
|
|||
printf("\nmalloc torture test w/ %d threads and %d iterations\n", n,
|
||||
ITERATIONS);
|
||||
SPAWN(fork);
|
||||
AssertNoLocksAreHeld();
|
||||
struct timespec t1 = timespec_real();
|
||||
for (i = 0; i < n; ++i)
|
||||
ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0));
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/stdio/internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
TEST(fgetwc, testAscii_oneChar) {
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
FILE *f;
|
||||
char buf[32];
|
||||
|
@ -169,5 +168,3 @@ TEST(popen, torture) {
|
|||
ASSERT_EQ(0, pthread_join(t[i], 0));
|
||||
CheckForFdLeaks();
|
||||
}
|
||||
|
||||
#endif /* __x86_64__ */
|
||||
|
|
|
@ -349,7 +349,7 @@ int main() {
|
|||
#if USE == POSIX_RECURSIVE
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||
#else
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);
|
||||
#endif
|
||||
pthread_mutex_init(&g_locker, &attr);
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/gc.h"
|
||||
#include "libc/mem/leaks.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
@ -51,7 +52,6 @@ TEST(pthread_atfork, test) {
|
|||
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]);
|
||||
|
@ -60,7 +60,6 @@ TEST(pthread_atfork, test) {
|
|||
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]);
|
||||
|
@ -79,7 +78,7 @@ void mu_unlock(void) {
|
|||
}
|
||||
|
||||
void mu_wipe(void) {
|
||||
pthread_mutex_init(&mu, 0);
|
||||
pthread_mutex_wipe_np(&mu);
|
||||
}
|
||||
|
||||
void *Worker(void *arg) {
|
||||
|
|
|
@ -1,8 +1,23 @@
|
|||
// 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 <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
||||
int got_cleanup;
|
||||
pthread_cond_t cv;
|
||||
|
@ -26,7 +41,7 @@ int main(int argc, char* argv[]) {
|
|||
pthread_t th;
|
||||
pthread_mutexattr_t at;
|
||||
pthread_mutexattr_init(&at);
|
||||
pthread_mutexattr_settype(&at, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutexattr_settype(&at, PTHREAD_MUTEX_DEFAULT);
|
||||
pthread_mutex_init(&mu, &at);
|
||||
pthread_mutexattr_destroy(&at);
|
||||
pthread_cond_init(&cv, 0);
|
||||
|
@ -42,8 +57,6 @@ int main(int argc, char* argv[]) {
|
|||
return 6;
|
||||
if (pthread_mutex_trylock(&mu) != EBUSY)
|
||||
return 7;
|
||||
if (pthread_mutex_unlock(&mu))
|
||||
return 8;
|
||||
pthread_mutex_destroy(&mu);
|
||||
pthread_cond_destroy(&cv);
|
||||
}
|
||||
|
|
|
@ -40,11 +40,7 @@ atomic_int gotcleanup;
|
|||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
pthread_mutexattr_t at;
|
||||
pthread_mutexattr_init(&at);
|
||||
pthread_mutexattr_settype(&at, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutex_init(&mu, &at);
|
||||
pthread_mutexattr_destroy(&at);
|
||||
pthread_mutex_init(&mu, 0);
|
||||
pthread_cond_init(&cv, 0);
|
||||
}
|
||||
|
||||
|
@ -194,6 +190,7 @@ TEST(pthread_cancel, condDeferredWait_reacquiresMutex) {
|
|||
ASSERT_EQ(0, pthread_join(th, &rc));
|
||||
ASSERT_EQ(PTHREAD_CANCELED, rc);
|
||||
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_consistent(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
}
|
||||
|
||||
|
@ -206,6 +203,7 @@ TEST(pthread_cancel, condDeferredWaitDelayed) {
|
|||
ASSERT_EQ(0, pthread_join(th, &rc));
|
||||
ASSERT_EQ(PTHREAD_CANCELED, rc);
|
||||
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_consistent(&mu));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&mu));
|
||||
}
|
||||
|
||||
|
|
|
@ -76,11 +76,11 @@ void *Writer(void *arg) {
|
|||
ASSERT_EQ(0, pthread_rwlock_wrlock(&lock));
|
||||
// cosmo_trace_begin("writer");
|
||||
++foo;
|
||||
delay(100);
|
||||
delay(10);
|
||||
++bar;
|
||||
// cosmo_trace_end("writer");
|
||||
ASSERT_EQ(0, pthread_rwlock_unlock(&lock));
|
||||
delay(100);
|
||||
delay(10);
|
||||
}
|
||||
done = true;
|
||||
return 0;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/sysv/consts/itimer.h"
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/itimerval.h"
|
||||
|
@ -28,7 +29,6 @@
|
|||
#include "libc/errno.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/itimer.h"
|
||||
#include "libc/sysv/consts/sa.h"
|
||||
#include "libc/sysv/consts/sicode.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue