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