mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-25 23:02:27 +00:00
Improve multithreading
This commit is contained in:
parent
d3167126aa
commit
30afd6ddbb
38 changed files with 752 additions and 174 deletions
40
test/libc/thread/pthread_cancel_deferred_cond_test.c
Normal file
40
test/libc/thread/pthread_cancel_deferred_cond_test.c
Normal 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;
|
||||
}
|
43
test/libc/thread/pthread_cancel_masked_cond_test.c
Normal file
43
test/libc/thread/pthread_cancel_masked_cond_test.c
Normal 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;
|
||||
}
|
41
test/libc/thread/pthread_cancel_masked_read_test.c
Normal file
41
test/libc/thread/pthread_cancel_masked_read_test.c
Normal 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;
|
||||
}
|
30
test/libc/thread/pthread_create_inherit_mask_test.c
Normal file
30
test/libc/thread/pthread_create_inherit_mask_test.c
Normal 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;
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue