Improve multithreading

This commit is contained in:
Justine Tunney 2024-07-21 06:41:30 -07:00
parent d3167126aa
commit 30afd6ddbb
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
38 changed files with 752 additions and 174 deletions

View file

@ -0,0 +1,86 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include <cosmo.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
/**
* stack overflow recovery technique #5
* use the cosmo posix threads extensions
*/
sig_atomic_t smashed_stack;
void CrashHandler(int sig) {
smashed_stack = true;
pthread_exit(0);
}
int StackOverflow(int f(), int n) {
if (n < INT_MAX) {
return f(f, n + 1) - 1;
} else {
return INT_MAX;
}
}
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
void *MyPosixThread(void *arg) {
exit(pStackOverflow(pStackOverflow, 0));
return 0;
}
int main() {
// choose the most dangerously small size possible
size_t sigstacksize = sysconf(_SC_MINSIGSTKSZ) + 2048;
// setup signal handler
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_ONSTACK;
sa.sa_handler = CrashHandler;
if (sigaction(SIGBUS, &sa, 0))
return 1;
if (sigaction(SIGSEGV, &sa, 0))
return 2;
// create thread with signal stack
pthread_t id;
pthread_attr_t attr;
if (pthread_attr_init(&attr))
return 3;
if (pthread_attr_setguardsize(&attr, getpagesize()))
return 4;
if (pthread_attr_setsigaltstacksize_np(&attr, sigstacksize))
return 5;
if (pthread_create(&id, &attr, MyPosixThread, 0))
return 6;
if (pthread_attr_destroy(&attr))
return 7;
if (pthread_join(id, 0))
return 8;
if (!smashed_stack)
return 9;
CheckForMemoryLeaks();
}

View file

@ -50,12 +50,10 @@ TEST(cosmo_once, test) {
pthread_t th[N];
x = y = 0;
ASSERT_EQ(0, pthread_barrier_init(&b, 0, N));
for (i = 0; i < N; ++i) {
for (i = 0; i < N; ++i)
ASSERT_EQ(0, pthread_create(th + i, 0, Worker, 0));
}
for (i = 0; i < N; ++i) {
for (i = 0; i < N; ++i)
ASSERT_EQ(0, pthread_join(th[i], 0));
}
ASSERT_EQ(N, atomic_load(&x));
ASSERT_EQ(1, atomic_load(&y));
ASSERT_EQ(0, pthread_barrier_destroy(&b));

View file

@ -528,18 +528,21 @@ TEST(mmap, sharedFileMapFork) {
int count;
void *ptrs[N];
size_t sizes[N];
void BenchMmapPrivate(void) {
void *p;
p = mmap(0, gransz * 10, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
p = mmap(0, (sizes[count] = rand() % (pagesz * 500)), PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (p == MAP_FAILED)
__builtin_trap();
ptrs[count++] = p;
ptrs[count] = p;
++count;
}
void BenchUnmap(void) {
if (munmap(ptrs[--count], gransz * 10))
--count;
if (munmap(ptrs[count], sizes[count]))
__builtin_trap();
}
@ -557,7 +560,7 @@ void BenchBigMunmap(void) {
__builtin_trap();
}
BENCH(mmap, bench) {
TEST(mmap, bench) {
EZBENCH2("mmap", donothing, BenchMmapPrivate());
EZBENCH2("munmap", donothing, BenchUnmap());
// EZBENCH2("big mmap", donothing, BenchBigMmap());

View file

@ -0,0 +1,40 @@
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
int got_cleanup;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
void cleanup(void* arg) {
got_cleanup = 1;
}
void* worker(void* arg) {
pthread_cleanup_push(cleanup, 0);
if (pthread_mutex_lock(&mu))
_Exit(11);
pthread_cond_wait(&cv, &mu);
_Exit(12);
pthread_cleanup_pop(0);
}
int main(int argc, char* argv[]) {
void* rc;
pthread_t th;
if (pthread_create(&th, 0, worker, 0))
return 2;
if (pthread_cancel(th))
return 3;
if (pthread_join(th, &rc))
return 4;
if (rc != PTHREAD_CANCELED)
return 5;
if (!got_cleanup)
return 6;
if (pthread_mutex_trylock(&mu) != EBUSY)
return 7;
if (pthread_mutex_unlock(&mu))
return 8;
}

View file

@ -0,0 +1,43 @@
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
int got_cleanup;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
void cleanup(void* arg) {
got_cleanup = 1;
}
void* worker(void* arg) {
if (pthread_setcancelstate(PTHREAD_CANCEL_MASKED, 0))
_Exit(10);
pthread_cleanup_push(cleanup, 0);
if (pthread_mutex_lock(&mu))
_Exit(11);
if (pthread_cond_wait(&cv, &mu) != ECANCELED)
_Exit(12);
if (pthread_mutex_trylock(&mu) != EBUSY)
_Exit(13);
if (pthread_mutex_unlock(&mu))
_Exit(14);
pthread_cleanup_pop(0);
return (void*)123;
}
int main(int argc, char* argv[]) {
void* rc;
pthread_t th;
if (pthread_create(&th, 0, worker, 0))
return 2;
if (pthread_cancel(th))
return 3;
if (pthread_join(th, &rc))
return 4;
if (rc != (void*)123)
return 5;
if (got_cleanup)
return 6;
}

View file

@ -0,0 +1,41 @@
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
int pfds[2];
int got_cleanup;
void cleanup(void* arg) {
got_cleanup = 1;
}
void* worker(void* arg) {
char buf[8];
if (pthread_setcancelstate(PTHREAD_CANCEL_MASKED, 0))
_Exit(10);
pthread_cleanup_push(cleanup, 0);
if (read(pfds[0], buf, sizeof(buf)) != -1)
_Exit(11);
if (errno != ECANCELED)
_Exit(12);
pthread_cleanup_pop(0);
return (void*)123;
}
int main(int argc, char* argv[]) {
void* rc;
pthread_t th;
if (pipe(pfds))
return 1;
if (pthread_create(&th, 0, worker, 0))
return 2;
if (pthread_cancel(th))
return 3;
if (pthread_join(th, &rc))
return 4;
if (rc != (void*)123)
return 5;
if (got_cleanup)
return 7;
}

View file

@ -0,0 +1,30 @@
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
sigset_t parent_mask;
sigset_t child_mask;
void* worker(void* arg) {
if (pthread_sigmask(SIG_SETMASK, 0, &child_mask))
_Exit(1);
return 0;
}
int main(int argc, char* argv[]) {
pthread_t th;
sigemptyset(&parent_mask);
sigaddset(&parent_mask, SIGSYS);
sigaddset(&parent_mask, SIGUSR2);
sigaddset(&parent_mask, SIGWINCH);
if (pthread_sigmask(SIG_SETMASK, &parent_mask, 0))
return 1;
if (pthread_create(&th, 0, worker, 0))
return 2;
if (pthread_join(th, 0))
return 3;
for (int i = 1; i <= _NSIG; ++i)
if (sigismember(&parent_mask, i) != sigismember(&child_mask, i))
return 4;
}

View file

@ -34,6 +34,7 @@
#include "libc/runtime/runtime.h"
#include "libc/runtime/stack.h"
#include "libc/runtime/sysconf.h"
#include "libc/stdio/rand.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sched.h"
@ -279,11 +280,10 @@ static void CreateDetached(void) {
ASSERT_EQ(0, pthread_attr_destroy(&attr));
}
BENCH(pthread_create, bench) {
TEST(pthread_create, bench) {
EZBENCH2("CreateJoin", donothing, CreateJoin());
EZBENCH2("CreateDetach", donothing, CreateDetach());
EZBENCH2("CreateDetached", donothing, CreateDetached());
while (!pthread_orphan_np()) {
_pthread_decimate();
}
while (!pthread_orphan_np())
pthread_decimate_np();
}

View file

@ -50,9 +50,8 @@ TEST(pthread_detach, testCreateReturn) {
pthread_t id;
ASSERT_EQ(0, pthread_create(&id, 0, Increment, 0));
ASSERT_EQ(0, pthread_detach(id));
while (!pthread_orphan_np()) {
_pthread_decimate();
}
while (!pthread_orphan_np())
pthread_decimate_np();
}
TEST(pthread_detach, testDetachUponCreation) {
@ -62,7 +61,6 @@ TEST(pthread_detach, testDetachUponCreation) {
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
ASSERT_EQ(0, pthread_create(&th, &attr, Increment, 0));
ASSERT_EQ(0, pthread_attr_destroy(&attr));
while (!pthread_orphan_np()) {
_pthread_decimate();
}
while (!pthread_orphan_np())
pthread_decimate_np();
}