118 lines · cpp
1//===-- Tests for pthread_once --------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/stdint_proxy.h" // uintptr_t10#include "src/__support/CPP/atomic.h"11#include "src/pthread/pthread_create.h"12#include "src/pthread/pthread_join.h"13#include "src/pthread/pthread_mutex_destroy.h"14#include "src/pthread/pthread_mutex_init.h"15#include "src/pthread/pthread_mutex_lock.h"16#include "src/pthread/pthread_mutex_unlock.h"17#include "src/pthread/pthread_once.h"18#include "test/IntegrationTest/test.h"19 20#include <pthread.h>21 22static constexpr unsigned int NUM_THREADS = 5;23static LIBC_NAMESPACE::cpp::Atomic<unsigned int> thread_count;24 25static unsigned int call_count;26static void pthread_once_func() { ++call_count; }27 28static void *func(void *) {29 static pthread_once_t flag = PTHREAD_ONCE_INIT;30 ASSERT_EQ(LIBC_NAMESPACE::pthread_once(&flag, pthread_once_func), 0);31 32 thread_count.fetch_add(1);33 34 return nullptr;35}36 37void call_from_5_threads() {38 // Ensure the call count and thread count are 0 to begin with.39 call_count = 0;40 thread_count = 0;41 42 pthread_t threads[NUM_THREADS];43 for (unsigned int i = 0; i < NUM_THREADS; ++i) {44 ASSERT_EQ(45 LIBC_NAMESPACE::pthread_create(threads + i, nullptr, func, nullptr), 0);46 }47 48 for (unsigned int i = 0; i < NUM_THREADS; ++i) {49 void *retval;50 ASSERT_EQ(LIBC_NAMESPACE::pthread_join(threads[i], &retval), 0);51 ASSERT_EQ(uintptr_t(retval), uintptr_t(0));52 }53 54 EXPECT_EQ(thread_count.val, 5U);55 EXPECT_EQ(call_count, 1U);56}57 58static pthread_mutex_t once_func_blocker;59static void blocking_once_func() {60 LIBC_NAMESPACE::pthread_mutex_lock(&once_func_blocker);61 LIBC_NAMESPACE::pthread_mutex_unlock(&once_func_blocker);62}63 64static LIBC_NAMESPACE::cpp::Atomic<unsigned int> start_count;65static LIBC_NAMESPACE::cpp::Atomic<unsigned int> done_count;66static void *once_func_caller(void *) {67 static pthread_once_t flag;68 start_count.fetch_add(1);69 LIBC_NAMESPACE::pthread_once(&flag, blocking_once_func);70 done_count.fetch_add(1);71 return nullptr;72}73 74// Test the synchronization aspect of the pthread_once function.75// This is not a fool proof test, but something which might be76// useful when we add a flakiness detection scheme to UnitTest.77void test_synchronization() {78 start_count = 0;79 done_count = 0;80 81 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_init(&once_func_blocker, nullptr), 0);82 // Lock the blocking mutex so that the once func blocks.83 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_lock(&once_func_blocker), 0);84 85 pthread_t t1, t2;86 ASSERT_EQ(87 LIBC_NAMESPACE::pthread_create(&t1, nullptr, once_func_caller, nullptr),88 0);89 ASSERT_EQ(90 LIBC_NAMESPACE::pthread_create(&t2, nullptr, once_func_caller, nullptr),91 0);92 93 while (start_count.load() != 2)94 ; // Spin until both threads start.95 96 // Since the once func is blocked, the threads should not be done yet.97 EXPECT_EQ(done_count.val, 0U);98 99 // Unlock the blocking mutex so that the once func blocks.100 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&once_func_blocker), 0);101 102 void *retval;103 ASSERT_EQ(LIBC_NAMESPACE::pthread_join(t1, &retval), uintptr_t(0));104 ASSERT_EQ(uintptr_t(retval), 0);105 ASSERT_EQ(LIBC_NAMESPACE::pthread_join(t2, &retval), uintptr_t(0));106 ASSERT_EQ(uintptr_t(retval), 0);107 108 ASSERT_EQ(done_count.val, 2U);109 110 LIBC_NAMESPACE::pthread_mutex_destroy(&once_func_blocker);111}112 113TEST_MAIN() {114 call_from_5_threads();115 test_synchronization();116 return 0;117}118