cosmopolitan/test/libc/rand/rand64_test.c
Justine Tunney 5f4f6b0e69 Make _Thread_local work across platforms
We now rewrite the binary image at runtime on Windows and XNU to change
mov %fs:0,%reg instructions to use %gs instead. There's also simpler
threading API introduced by this change and it's called _spawn() and
_join(), which has replaced most clone() usage.
2022-07-10 04:01:17 -07:00

113 lines
4.2 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/bits/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/spinlock.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/threaded.h"
#include "libc/rand/rand.h"
#include "libc/runtime/stack.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/clone.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/spawn.h"
#include "libc/thread/thread.h"
#include "libc/time/time.h"
#define THREADS 8
#define ENTRIES 1024
int ready;
volatile uint64_t A[THREADS * ENTRIES];
void OnChld(int sig) {
// do nothing
}
dontinline void Generate(int i) {
A[i] = rand64();
}
int Thrasher(void *arg, int tid) {
int i, id = (intptr_t)arg;
while (!atomic_load(&ready)) {
cthread_memory_wait32(&ready, 0, 0);
}
for (i = 0; i < ENTRIES; ++i) {
Generate(id * ENTRIES + i);
}
return 0;
}
TEST(rand64, testLcg_doesntProduceIdenticalValues) {
int i, j;
bzero(A, sizeof(A));
for (i = 0; i < ARRAYLEN(A); ++i) {
A[i] = rand64();
}
for (i = 0; i < ARRAYLEN(A); ++i) {
EXPECT_NE(0, A[i], "i=%d", i);
for (j = 0; j < ARRAYLEN(A); ++j) {
if (i == j) continue;
EXPECT_NE(A[i], A[j], "i=%d j=%d", i, j);
}
}
}
TEST(rand64, testThreadSafety_doesntProduceIdenticalValues) {
int i, j, rc, ws;
sigset_t ss, oldss;
struct sigaction oldsa;
struct spawn th[THREADS];
struct sigaction sa = {.sa_handler = OnChld, .sa_flags = SA_RESTART};
EXPECT_NE(-1, sigaction(SIGCHLD, &sa, &oldsa));
bzero(A, sizeof(A));
sigemptyset(&ss);
sigaddset(&ss, SIGCHLD);
EXPECT_EQ(0, sigprocmask(SIG_BLOCK, &ss, &oldss));
ready = false;
for (i = 0; i < THREADS; ++i) {
ASSERT_SYS(0, 0, _spawn(Thrasher, (void *)(intptr_t)i, th + i));
}
atomic_store(&ready, 1);
cthread_memory_wake32(&ready, INT_MAX);
for (i = 0; i < THREADS; ++i) {
ASSERT_SYS(0, 0, _join(th + i));
}
sigaction(SIGCHLD, &oldsa, 0);
sigprocmask(SIG_BLOCK, &oldss, 0);
for (i = 0; i < ARRAYLEN(A); ++i) {
EXPECT_NE(0, A[i], "i=%d", i);
for (j = 0; j < ARRAYLEN(A); ++j) {
if (i == j) continue;
EXPECT_NE(A[i], A[j], "i=%d j=%d", i, j);
}
}
}