mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Tidy up the threading implementation
The organization of the source files is now much more rational. Old experiments that didn't work out are now deleted. Naming of things like files is now more intuitive.
This commit is contained in:
parent
e9272f03fb
commit
155b378a39
199 changed files with 526 additions and 685 deletions
157
test/libc/thread/clone_test.c
Normal file
157
test/libc/thread/clone_test.c
Normal file
|
@ -0,0 +1,157 @@
|
|||
/*-*- 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 2021 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/timespec.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/futex.internal.h"
|
||||
#include "libc/intrin/wait0.internal.h"
|
||||
#include "libc/log/backtrace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/nexgen32e.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/runtime/stack.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/clock.h"
|
||||
#include "libc/sysv/consts/clone.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/nr.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "libc/thread/tls2.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
int x, me, tid;
|
||||
_Atomic(int) thechilde;
|
||||
|
||||
void SetUpOnce(void) {
|
||||
__enable_threads();
|
||||
ASSERT_SYS(0, 0, pledge("stdio rpath", 0));
|
||||
}
|
||||
|
||||
int Hog(void *arg, int tid) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SetUp(void) {
|
||||
x = 0;
|
||||
me = gettid();
|
||||
}
|
||||
|
||||
void TearDown(void) {
|
||||
}
|
||||
|
||||
int DoNothing(void *arg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TEST THREADS WORK
|
||||
|
||||
int CloneTest1(void *arg, int tid) {
|
||||
intptr_t rsp, top, bot;
|
||||
CheckStackIsAligned();
|
||||
// PrintBacktraceUsingSymbols(2, __builtin_frame_address(0),
|
||||
// GetSymbolTable());
|
||||
rsp = (intptr_t)__builtin_frame_address(0);
|
||||
bot = ROUNDDOWN((intptr_t)rsp, GetStackSize());
|
||||
top = bot + GetStackSize();
|
||||
ASSERT_GT(rsp, bot); // check we're on stack
|
||||
ASSERT_LT(rsp, top); // check we're on stack
|
||||
ASSERT_GT(rsp, top - 256); // check we're near top of stack
|
||||
ASSERT_TRUE(IS2POW(GetStackSize()));
|
||||
ASSERT_EQ(0, bot & (GetStackSize() - 1));
|
||||
x = 42;
|
||||
ASSERT_EQ(23, (intptr_t)arg);
|
||||
ASSERT_NE(gettid(), getpid());
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(clone, test1) {
|
||||
int ptid = 0;
|
||||
struct spawn th;
|
||||
ASSERT_SYS(0, 0, _spawn(CloneTest1, (void *)23, &th));
|
||||
ASSERT_SYS(0, 0, _join(&th));
|
||||
ASSERT_NE(gettid(), tid);
|
||||
ASSERT_EQ(tid, ptid);
|
||||
ASSERT_EQ(42, x);
|
||||
ASSERT_NE(me, tid);
|
||||
ASSERT_EQ(0, errno);
|
||||
errno = 31337;
|
||||
ASSERT_EQ(31337, errno);
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TEST THREADS CAN ISSUE SYSTEM CALLS WITH INDEPENDENT ERRNOS
|
||||
|
||||
_Atomic(int) sysbarrier;
|
||||
|
||||
int CloneTestSys(void *arg, int tid) {
|
||||
int i, id = (intptr_t)arg;
|
||||
CheckStackIsAligned();
|
||||
while (!sysbarrier) asm("pause");
|
||||
for (i = 0; i < 20; ++i) {
|
||||
switch (id % 3) {
|
||||
case 0:
|
||||
errno = 123;
|
||||
open(0, 0);
|
||||
asm("pause");
|
||||
ASSERT_EQ(EFAULT, errno);
|
||||
break;
|
||||
case 1:
|
||||
errno = 123;
|
||||
dup(-1);
|
||||
asm("pause");
|
||||
ASSERT_EQ(EBADF, errno);
|
||||
break;
|
||||
case 2:
|
||||
errno = 123;
|
||||
dup3(0, 0, 0);
|
||||
asm("pause");
|
||||
ASSERT_EQ(EINVAL, errno);
|
||||
break;
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(clone, tlsSystemCallsErrno_wontClobberMainThreadBecauseTls) {
|
||||
int i;
|
||||
struct spawn th[8];
|
||||
ASSERT_EQ(0, errno);
|
||||
for (i = 0; i < 8; ++i) {
|
||||
ASSERT_SYS(0, 0, _spawn(CloneTestSys, (void *)(intptr_t)i, th + i));
|
||||
}
|
||||
sysbarrier = 1;
|
||||
for (i = 0; i < 8; ++i) {
|
||||
ASSERT_SYS(0, 0, _join(th + i));
|
||||
}
|
||||
ASSERT_EQ(0, errno);
|
||||
}
|
82
test/libc/thread/pthread_barrier_wait_test.c
Normal file
82
test/libc/thread/pthread_barrier_wait_test.c
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*-*- 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/assert.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
int i, n;
|
||||
_Atomic(int) p, w;
|
||||
pthread_barrier_t barrier;
|
||||
|
||||
int Worker(void *arg, int tid) {
|
||||
int rc;
|
||||
rc = pthread_barrier_wait(&barrier);
|
||||
atomic_fetch_add(&w, 1);
|
||||
ASSERT_GE(rc, 0);
|
||||
if (rc == PTHREAD_BARRIER_SERIAL_THREAD) {
|
||||
atomic_fetch_add(&p, 1);
|
||||
ASSERT_EQ(0, barrier.popped);
|
||||
ASSERT_EQ(0, barrier.waits);
|
||||
ASSERT_EQ(n, barrier.count);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(pthread_barrier_init, test0_isInvalid) {
|
||||
__assert_disable = true;
|
||||
ASSERT_EQ(EINVAL, pthread_barrier_init(&barrier, 0, 0));
|
||||
__assert_disable = false;
|
||||
}
|
||||
|
||||
TEST(pthread_barrier_wait, test1) {
|
||||
struct spawn t;
|
||||
p = 0;
|
||||
w = 0;
|
||||
n = 1;
|
||||
ASSERT_EQ(0, pthread_barrier_init(&barrier, 0, n));
|
||||
ASSERT_SYS(0, 0, _spawn(Worker, 0, &t));
|
||||
EXPECT_SYS(0, 0, _join(&t));
|
||||
ASSERT_EQ(1, p);
|
||||
ASSERT_EQ(n, w);
|
||||
ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
|
||||
}
|
||||
|
||||
TEST(pthread_barrier_wait, test32) {
|
||||
struct spawn *t;
|
||||
p = 0;
|
||||
w = 0;
|
||||
n = 32;
|
||||
t = gc(malloc(sizeof(struct spawn) * n));
|
||||
ASSERT_EQ(0, pthread_barrier_init(&barrier, 0, n));
|
||||
for (i = 0; i < n; ++i) {
|
||||
ASSERT_EQ(0, w);
|
||||
ASSERT_SYS(0, 0, _spawn(Worker, 0, t + i));
|
||||
}
|
||||
for (i = 0; i < n; ++i) {
|
||||
EXPECT_SYS(0, 0, _join(t + i));
|
||||
}
|
||||
ASSERT_EQ(1, p);
|
||||
ASSERT_EQ(n, w);
|
||||
ASSERT_EQ(0, pthread_barrier_destroy(&barrier));
|
||||
}
|
123
test/libc/thread/pthread_cond_broadcast_test.c
Normal file
123
test/libc/thread/pthread_cond_broadcast_test.c
Normal file
|
@ -0,0 +1,123 @@
|
|||
/*-*- 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/struct/timespec.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/thread2.h"
|
||||
|
||||
// TODO(jart): Re-enable me.
|
||||
#if 0
|
||||
_Atomic(int) bReady;
|
||||
pthread_cond_t bCond;
|
||||
pthread_mutex_t bMutex;
|
||||
|
||||
int BroadcastWorker(void *arg, int tid) {
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&bMutex));
|
||||
atomic_fetch_add(&bReady, 1);
|
||||
ASSERT_EQ(0, pthread_cond_wait(&bCond, &bMutex));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&bMutex));
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(pthread_cond_broadcast, test) {
|
||||
pthread_condattr_t cAttr;
|
||||
pthread_mutexattr_t mAttr;
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&mAttr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_ERRORCHECK));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&bMutex, &mAttr));
|
||||
ASSERT_EQ(0, pthread_condattr_init(&cAttr));
|
||||
ASSERT_EQ(0, pthread_condattr_setpshared(&cAttr, PTHREAD_PROCESS_PRIVATE));
|
||||
ASSERT_EQ(0, pthread_cond_init(&bCond, &cAttr));
|
||||
int i, n = 16;
|
||||
struct spawn *t = gc(malloc(sizeof(struct spawn) * n));
|
||||
for (i = 0; i < n; ++i) {
|
||||
ASSERT_SYS(0, 0, _spawn(BroadcastWorker, 0, t + i));
|
||||
}
|
||||
for (;;) {
|
||||
if (atomic_load_explicit(&bReady, memory_order_relaxed) == n) {
|
||||
break;
|
||||
} else {
|
||||
pthread_yield();
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&bMutex));
|
||||
ASSERT_EQ(0, pthread_cond_broadcast(&bCond));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&bMutex));
|
||||
for (i = 0; i < n; ++i) {
|
||||
EXPECT_SYS(0, 0, _join(t + i));
|
||||
}
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&bMutex));
|
||||
ASSERT_EQ(0, pthread_cond_destroy(&bCond));
|
||||
}
|
||||
|
||||
TEST(pthread_cond_timedwait, testTimeoutInPast_timesOutImmediately) {
|
||||
struct timespec t = {100000};
|
||||
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
|
||||
pthread_mutex_t m = {PTHREAD_MUTEX_ERRORCHECK};
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&m));
|
||||
ASSERT_EQ(ETIMEDOUT, pthread_cond_timedwait(&c, &m, &t));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&m));
|
||||
}
|
||||
|
||||
_Atomic(int) tReady;
|
||||
pthread_cond_t tCond;
|
||||
pthread_mutex_t tMutex;
|
||||
|
||||
int TimedWorker(void *arg, int tid) {
|
||||
struct timespec ts;
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&tMutex));
|
||||
atomic_fetch_add(&tReady, 1);
|
||||
ts = _timespec_add(_timespec_mono(), _timespec_frommillis(30000));
|
||||
ASSERT_EQ(0, pthread_cond_timedwait(&tCond, &tMutex, &ts));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&tMutex));
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(pthread_cond_timedwait, testThirtySeconds_doesntTimeOut) {
|
||||
pthread_condattr_t cAttr;
|
||||
pthread_mutexattr_t mAttr;
|
||||
ASSERT_EQ(0, pthread_mutexattr_init(&mAttr));
|
||||
ASSERT_EQ(0, pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_ERRORCHECK));
|
||||
ASSERT_EQ(0, pthread_mutex_init(&tMutex, &mAttr));
|
||||
ASSERT_EQ(0, pthread_condattr_init(&cAttr));
|
||||
ASSERT_EQ(0, pthread_condattr_setpshared(&cAttr, PTHREAD_PROCESS_PRIVATE));
|
||||
ASSERT_EQ(0, pthread_cond_init(&tCond, &cAttr));
|
||||
struct spawn t;
|
||||
ASSERT_SYS(0, 0, _spawn(TimedWorker, 0, &t));
|
||||
for (;;) {
|
||||
if (atomic_load_explicit(&tReady, memory_order_relaxed)) {
|
||||
break;
|
||||
} else {
|
||||
pthread_yield();
|
||||
}
|
||||
}
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&tMutex));
|
||||
ASSERT_EQ(0, pthread_cond_signal(&tCond));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&tMutex));
|
||||
EXPECT_SYS(0, 0, _join(&t));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&tMutex));
|
||||
ASSERT_EQ(0, pthread_cond_destroy(&tCond));
|
||||
}
|
||||
#endif
|
|
@ -22,8 +22,6 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/intrin/pthread2.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/nexgen32e.h"
|
||||
|
@ -37,6 +35,7 @@
|
|||
#include "libc/testlib/subprocess.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/thread2.h"
|
||||
|
||||
void OnUsr1(int sig, struct siginfo *si, void *vctx) {
|
||||
struct ucontext *ctx = vctx;
|
||||
|
|
|
@ -16,9 +16,7 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
|
|
66
test/libc/thread/pthread_rwlock_rdlock_test.c
Normal file
66
test/libc/thread/pthread_rwlock_rdlock_test.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*-*- 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/mem/mem.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
#define ITERATIONS 50000
|
||||
#define READERS 8
|
||||
#define WRITERS 2
|
||||
|
||||
_Atomic(int) reads;
|
||||
_Atomic(int) writes;
|
||||
pthread_rwlock_t lock;
|
||||
pthread_barrier_t barrier;
|
||||
|
||||
int Reader(void *arg, int tid) {
|
||||
pthread_barrier_wait(&barrier);
|
||||
for (int i = 0; i < ITERATIONS; ++i) {
|
||||
pthread_rwlock_rdlock(&lock);
|
||||
++reads;
|
||||
pthread_rwlock_unlock(&lock);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Writer(void *arg, int tid) {
|
||||
pthread_barrier_wait(&barrier);
|
||||
for (int i = 0; i < ITERATIONS; ++i) {
|
||||
pthread_rwlock_wrlock(&lock);
|
||||
++writes;
|
||||
pthread_rwlock_unlock(&lock);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST(pthread_rwlock_rdlock, test) {
|
||||
int i;
|
||||
struct spawn *t = _gc(malloc(sizeof(struct spawn) * (READERS + WRITERS)));
|
||||
pthread_barrier_init(&barrier, 0, READERS + WRITERS);
|
||||
for (i = 0; i < READERS + WRITERS; ++i) {
|
||||
ASSERT_SYS(0, 0, _spawn(i < READERS ? Reader : Writer, 0, t + i));
|
||||
}
|
||||
for (i = 0; i < READERS + WRITERS; ++i) {
|
||||
EXPECT_SYS(0, 0, _join(t + i));
|
||||
}
|
||||
EXPECT_EQ(READERS * ITERATIONS, reads);
|
||||
EXPECT_EQ(WRITERS * ITERATIONS, writes);
|
||||
}
|
|
@ -18,7 +18,6 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
#include "libc/runtime/internal.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/thread/spawn.h"
|
||||
#include "libc/thread/thread.h"
|
||||
|
||||
_Atomic(int) itworked;
|
||||
_Thread_local int var;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue