2022-04-07 07:15:35 +00:00
|
|
|
/*-*- 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/calls/calls.h"
|
|
|
|
#include "libc/calls/struct/sigaction.h"
|
|
|
|
#include "libc/calls/struct/sigset.h"
|
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
2022-08-20 19:32:51 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/intrin/spinlock.h"
|
2022-05-28 12:50:01 +00:00
|
|
|
#include "libc/limits.h"
|
2022-04-07 07:15:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-05-17 14:40:00 +00:00
|
|
|
#include "libc/nexgen32e/threaded.h"
|
2022-08-11 07:15:29 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/runtime/stack.h"
|
2022-08-20 19:32:51 +00:00
|
|
|
#include "libc/stdio/rand.h"
|
2022-04-07 07:15:35 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/clone.h"
|
2022-05-12 13:43:59 +00:00
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
2022-04-07 07:15:35 +00:00
|
|
|
#include "libc/sysv/consts/sa.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
2022-07-10 11:01:17 +00:00
|
|
|
#include "libc/thread/spawn.h"
|
2022-05-28 12:50:01 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-05-13 00:52:13 +00:00
|
|
|
#include "libc/time/time.h"
|
2022-04-07 07:15:35 +00:00
|
|
|
|
2022-04-15 06:39:48 +00:00
|
|
|
#define THREADS 8
|
2022-05-19 23:57:49 +00:00
|
|
|
#define ENTRIES 1024
|
2022-04-07 07:15:35 +00:00
|
|
|
|
|
|
|
volatile uint64_t A[THREADS * ENTRIES];
|
2022-09-05 15:26:03 +00:00
|
|
|
pthread_barrier_t barrier = PTHREAD_BARRIER_INITIALIZER;
|
2022-04-07 07:15:35 +00:00
|
|
|
|
2022-08-11 07:15:29 +00:00
|
|
|
void SetUpOnce(void) {
|
|
|
|
__enable_threads();
|
|
|
|
ASSERT_SYS(0, 0, pledge("stdio", 0));
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:15:35 +00:00
|
|
|
void OnChld(int sig) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:41:29 +00:00
|
|
|
dontinline void Generate(int i) {
|
|
|
|
A[i] = rand64();
|
|
|
|
}
|
|
|
|
|
2022-07-10 11:01:17 +00:00
|
|
|
int Thrasher(void *arg, int tid) {
|
2022-04-07 07:15:35 +00:00
|
|
|
int i, id = (intptr_t)arg;
|
2022-09-05 15:26:03 +00:00
|
|
|
pthread_barrier_wait(&barrier);
|
2022-04-07 07:15:35 +00:00
|
|
|
for (i = 0; i < ENTRIES; ++i) {
|
2022-05-18 23:41:29 +00:00
|
|
|
Generate(id * ENTRIES + i);
|
2022-04-07 07:15:35 +00:00
|
|
|
}
|
|
|
|
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) {
|
2022-05-23 17:15:53 +00:00
|
|
|
int i, j, rc, ws;
|
2022-04-07 07:15:35 +00:00
|
|
|
sigset_t ss, oldss;
|
|
|
|
struct sigaction oldsa;
|
2022-07-10 11:01:17 +00:00
|
|
|
struct spawn th[THREADS];
|
2022-04-07 07:15:35 +00:00
|
|
|
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));
|
2022-09-05 15:26:03 +00:00
|
|
|
pthread_barrier_init(&barrier, 0, THREADS);
|
2022-04-07 07:15:35 +00:00
|
|
|
for (i = 0; i < THREADS; ++i) {
|
2022-07-10 11:01:17 +00:00
|
|
|
ASSERT_SYS(0, 0, _spawn(Thrasher, (void *)(intptr_t)i, th + i));
|
2022-04-07 07:15:35 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < THREADS; ++i) {
|
2022-07-10 11:01:17 +00:00
|
|
|
ASSERT_SYS(0, 0, _join(th + i));
|
2022-04-07 07:15:35 +00:00
|
|
|
}
|
2022-04-20 16:56:53 +00:00
|
|
|
sigaction(SIGCHLD, &oldsa, 0);
|
|
|
|
sigprocmask(SIG_BLOCK, &oldss, 0);
|
2022-04-07 07:15:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|