Make fixes and improvements

- clock_nanosleep() is now much faster on OpenBSD and NetBSD
- Thread joining is now much faster on NetBSD
- FreeBSD timestamps are now more accurate
- Thread spawning now goes faster on XNU
- Clean up the clone() code
This commit is contained in:
Justine Tunney 2022-11-08 10:09:47 -08:00
parent aee50b1327
commit b407327972
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
47 changed files with 645 additions and 306 deletions

View file

@ -22,6 +22,7 @@
#include "libc/calls/struct/timespec.h"
#include "libc/errno.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/itimer.h"
#include "libc/sysv/consts/sa.h"
@ -31,6 +32,7 @@
void OnAlrm(int sig) {
// do nothing
STRACE("OnAlrm()");
}
TEST(nanosleep, testFault) {
@ -54,7 +56,7 @@ TEST(nanosleep, testInterrupt_remIsUpdated) {
.sa_flags = SA_RESETHAND,
};
ASSERT_SYS(0, 0, sigaction(SIGALRM, &sa, 0));
struct itimerval it = {{0, 0}, {0, 10000}}; // 10ms singleshot
struct itimerval it = {{0, 0}, {0, 100000}}; // 100ms singleshot
ASSERT_SYS(0, 0, setitimer(ITIMER_REAL, &it, 0));
struct timespec ts = {500, 0};
ASSERT_SYS(EINTR, -1, nanosleep(&ts, &ts));
@ -75,7 +77,7 @@ TEST(clock_nanosleep, testInterrupt_remIsUpdated) {
.sa_flags = SA_RESETHAND,
};
ASSERT_SYS(0, 0, sigaction(SIGALRM, &sa, 0));
struct itimerval it = {{0, 0}, {0, 10000}}; // 10ms singleshot
struct itimerval it = {{0, 0}, {0, 100000}}; // 100ms singleshot
ASSERT_SYS(0, 0, setitimer(ITIMER_REAL, &it, 0));
struct timespec ts = {500, 0};
ASSERT_EQ(EINTR, clock_nanosleep(CLOCK_REALTIME, 0, &ts, &ts));

View file

@ -39,8 +39,8 @@ void *Worker(void *arg) {
TEST(tkill, test) {
if (IsWindows()) return; // TODO(jart): fix me
int i;
void *res;
int i, tid;
pthread_t t;
sigset_t ss, oldss;
sighandler_t oldsig;
@ -49,7 +49,8 @@ TEST(tkill, test) {
oldsig = signal(SIGUSR1, OnSig);
ASSERT_SYS(0, 0, sigprocmask(SIG_BLOCK, &ss, &oldss));
ASSERT_EQ(0, pthread_create(&t, 0, Worker, 0));
ASSERT_SYS(0, 0, tkill(pthread_getunique_np(t), SIGUSR1));
ASSERT_EQ(0, pthread_getunique_np(t, &tid));
ASSERT_SYS(0, 0, tkill(tid, SIGUSR1));
ASSERT_EQ(0, pthread_join(t, &res));
ASSERT_EQ(SIGUSR1, (intptr_t)res);
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &oldss, 0));

View file

@ -22,6 +22,7 @@
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/macros.internal.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
@ -50,9 +51,9 @@ void *TortureWorker(void *arg) {
ready = true;
while (!done) {
if (!IsWindows()) pthread_kill(parent, SIGUSR1);
usleep(3);
usleep(1);
if (!IsWindows()) pthread_kill(parent, SIGUSR2);
usleep(3);
usleep(1);
}
return 0;
}
@ -61,7 +62,7 @@ TEST(getentropy, test) {
pthread_t child;
double e, w = 7.7;
struct sigaction sa;
int i, j, k, n = 999;
int i, j, k, m, n = 999;
char *buf = _gc(calloc(1, n));
sa.sa_flags = 0;
sa.sa_handler = OnSig;
@ -71,11 +72,13 @@ TEST(getentropy, test) {
parent = pthread_self();
ASSERT_EQ(0, pthread_create(&child, 0, TortureWorker, 0));
while (!ready) pthread_yield();
for (k = 0; k < 200; ++k) {
ASSERT_SYS(0, 0, getrandom(0, 0, 0));
ASSERT_SYS(0, n, getrandom(buf, n, 0));
ASSERT_SYS(EFAULT, -1, getrandom(0, n, 0));
ASSERT_SYS(EINVAL, -1, getrandom(buf, n, -1));
for (k = 0; k < 10; ++k) {
ASSERT_SYS(0, 0, getentropy(0, 0));
for (i = 0; i < n; i += m) {
m = MIN(n - i, 256);
ASSERT_SYS(0, 0, getentropy(buf + i, m));
ASSERT_SYS(EFAULT, -1, getentropy(0, m));
}
if ((e = MeasureEntropy(buf, n)) < w) {
fprintf(stderr, "error: entropy suspect! got %g but want >=%g\n", e, w);
for (i = 0; i < n;) {

View file

@ -24,6 +24,7 @@
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/log/check.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/mem/gc.h"
#include "libc/mem/mem.h"
@ -41,6 +42,29 @@
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
atomic_int done;
atomic_int ready;
pthread_t parent;
atomic_int gotsome;
void OnSig(int sig) {
++gotsome;
}
void *TortureWorker(void *arg) {
sigset_t ss;
sigfillset(&ss);
ASSERT_SYS(0, 0, sigprocmask(SIG_SETMASK, &ss, 0));
ready = true;
while (!done) {
if (!IsWindows()) pthread_kill(parent, SIGUSR1);
usleep(1);
if (!IsWindows()) pthread_kill(parent, SIGUSR2);
usleep(1);
}
return 0;
}
TEST(getrandom, test) {
double e, w = 7.7;
int i, j, n = 999;
@ -61,6 +85,45 @@ TEST(getrandom, test) {
}
}
TEST(getrandom, test2) {
pthread_t child;
double e, w = 7.7;
struct sigaction sa;
int i, j, k, m, n = 999;
char *buf = _gc(calloc(1, n));
sa.sa_flags = 0;
sa.sa_handler = OnSig;
sigemptyset(&sa.sa_mask);
ASSERT_SYS(0, 0, sigaction(SIGUSR1, &sa, 0));
ASSERT_SYS(0, 0, sigaction(SIGUSR2, &sa, 0));
parent = pthread_self();
ASSERT_EQ(0, pthread_create(&child, 0, TortureWorker, 0));
while (!ready) pthread_yield();
for (k = 0; k < 10; ++k) {
ASSERT_SYS(0, 0, getrandom(0, 0, 0));
for (i = 0; i < n; i += m) {
ASSERT_NE(-1, (m = getrandom(buf + i, n - i, 0)));
}
ASSERT_SYS(EFAULT, -1, getrandom(0, n, 0));
ASSERT_SYS(EINVAL, -1, getrandom(buf, n, -1));
if ((e = MeasureEntropy(buf, n)) < w) {
fprintf(stderr, "error: entropy suspect! got %g but want >=%g\n", e, w);
for (i = 0; i < n;) {
if (!(i % 16)) fprintf(stderr, "%6x ", i);
fprintf(stderr, "%lc", kCp437[buf[i] & 255]);
if (!(++i % 16)) fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
done = true;
pthread_join(child, 0);
exit(1);
}
}
done = true;
ASSERT_EQ(0, pthread_join(child, 0));
if (!IsWindows()) ASSERT_GT(gotsome, 0);
}
/* JustReturnZero */
/* entropy: 0 */
/* chi-square: 2.55e+07 */