23 changes: 11 additions & 12 deletions libc/test/src/threads/call_once_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "include/threads.h"
#include "src/__support/CPP/atomic.h"
#include "src/threads/call_once.h"
#include "src/threads/mtx_destroy.h"
#include "src/threads/mtx_init.h"
Expand All @@ -16,10 +17,8 @@
#include "src/threads/thrd_join.h"
#include "utils/UnitTest/Test.h"

#include <stdatomic.h>

static constexpr unsigned int NUM_THREADS = 5;
static atomic_uint thread_count;
static __llvm_libc::cpp::Atomic<unsigned int> thread_count;

static unsigned int call_count;
static void call_once_func() { ++call_count; }
Expand All @@ -28,7 +27,7 @@ static int func(void *) {
static once_flag flag = ONCE_FLAG_INIT;
__llvm_libc::call_once(&flag, call_once_func);

++thread_count; // This is a an atomic update.
thread_count.fetch_add(1);

return 0;
}
Expand All @@ -51,7 +50,7 @@ TEST(LlvmLibcCallOnceTest, CallFrom5Threads) {
ASSERT_EQ(retval, 0);
}

EXPECT_EQ(static_cast<unsigned int>(thread_count), 5U);
EXPECT_EQ(thread_count.val, 5U);
EXPECT_EQ(call_count, 1U);
}

Expand All @@ -61,13 +60,13 @@ static void blocking_once_func() {
__llvm_libc::mtx_unlock(&once_func_blocker);
}

static atomic_uint start_count;
static atomic_uint done_count;
static __llvm_libc::cpp::Atomic<unsigned int> start_count;
static __llvm_libc::cpp::Atomic<unsigned int> done_count;
static int once_func_caller(void *) {
static once_flag flag;
++start_count;
start_count.fetch_add(1);
__llvm_libc::call_once(&flag, blocking_once_func);
++done_count;
done_count.fetch_add(1);
return 0;
}

Expand All @@ -90,11 +89,11 @@ TEST(LlvmLibcCallOnceTest, TestSynchronization) {
ASSERT_EQ(__llvm_libc::thrd_create(&t2, once_func_caller, nullptr),
static_cast<int>(thrd_success));

while (start_count != 2)
while (start_count.load() != 2)
; // Spin until both threads start.

// Since the once func is blocked, the threads should not be done yet.
EXPECT_EQ(static_cast<unsigned int>(done_count), 0U);
EXPECT_EQ(done_count.val, 0U);

// Unlock the blocking mutex so that the once func blocks.
ASSERT_EQ(__llvm_libc::mtx_unlock(&once_func_blocker),
Expand All @@ -108,7 +107,7 @@ TEST(LlvmLibcCallOnceTest, TestSynchronization) {
static_cast<int>(thrd_success));
ASSERT_EQ(retval, 0);

ASSERT_EQ(static_cast<unsigned int>(done_count), 2U);
ASSERT_EQ(done_count.val, 2U);

__llvm_libc::mtx_destroy(&once_func_blocker);
}
15 changes: 7 additions & 8 deletions libc/test/src/threads/cnd_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
//
//===----------------------------------------------------------------------===//

#include <stdatomic.h>

#include "include/threads.h"
#include "src/__support/CPP/atomic.h"
#include "src/threads/cnd_broadcast.h"
#include "src/threads/cnd_destroy.h"
#include "src/threads/cnd_init.h"
Expand All @@ -34,15 +33,15 @@ namespace wait_notify_broadcast_test {
// |broadcast_count| by 1 before they start waiting on |broadcast_cnd|, and
// decrement it by 1 after getting signalled on |broadcast_cnd|.

constexpr int THRD_COUNT = 10000;
constexpr unsigned int THRD_COUNT = 10000;

static atomic_uint broadcast_count = 0;
static __llvm_libc::cpp::Atomic<unsigned int> broadcast_count(0);
static cnd_t broadcast_cnd, threads_ready_cnd;
static mtx_t broadcast_mtx, threads_ready_mtx;

int broadcast_thread_func(void *) {
__llvm_libc::mtx_lock(&broadcast_mtx);
int oldval = atomic_fetch_add(&broadcast_count, 1);
int oldval = broadcast_count.fetch_add(1);
if (oldval == THRD_COUNT - 1) {
__llvm_libc::mtx_lock(&threads_ready_mtx);
__llvm_libc::cnd_signal(&threads_ready_cnd);
Expand All @@ -51,7 +50,7 @@ int broadcast_thread_func(void *) {

__llvm_libc::cnd_wait(&broadcast_cnd, &broadcast_mtx);
__llvm_libc::mtx_unlock(&broadcast_mtx);
atomic_fetch_sub(&broadcast_count, 1);
broadcast_count.fetch_sub(1);
return 0;
}

Expand All @@ -70,7 +69,7 @@ TEST(LlvmLibcCndVarTest, WaitNotifyBroadcastTest) {
__llvm_libc::mtx_unlock(&threads_ready_mtx);

__llvm_libc::mtx_lock(&broadcast_mtx);
ASSERT_EQ(int(broadcast_count), THRD_COUNT);
ASSERT_EQ(broadcast_count.val, THRD_COUNT);
__llvm_libc::cnd_broadcast(&broadcast_cnd);
__llvm_libc::mtx_unlock(&broadcast_mtx);

Expand All @@ -80,7 +79,7 @@ TEST(LlvmLibcCndVarTest, WaitNotifyBroadcastTest) {
ASSERT_EQ(retval, 0);
}

ASSERT_EQ(int(broadcast_count), 0);
ASSERT_EQ(broadcast_count.val, 0U);

__llvm_libc::cnd_destroy(&broadcast_cnd);
__llvm_libc::cnd_destroy(&threads_ready_cnd);
Expand Down