1 //===-- Tests for pthread_once --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
18 #include "test/IntegrationTest/test.h"
21 #include <stdint.h> // uintptr_t
23 static constexpr unsigned int NUM_THREADS
= 5;
24 static LIBC_NAMESPACE::cpp::Atomic
<unsigned int> thread_count
;
26 static unsigned int call_count
;
27 static void pthread_once_func() { ++call_count
; }
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);
33 thread_count
.fetch_add(1);
38 void call_from_5_threads() {
39 // Ensure the call count and thread count are 0 to begin with.
43 pthread_t threads
[NUM_THREADS
];
44 for (unsigned int i
= 0; i
< NUM_THREADS
; ++i
) {
46 LIBC_NAMESPACE::pthread_create(threads
+ i
, nullptr, func
, nullptr), 0);
49 for (unsigned int i
= 0; i
< NUM_THREADS
; ++i
) {
51 ASSERT_EQ(LIBC_NAMESPACE::pthread_join(threads
[i
], &retval
), 0);
52 ASSERT_EQ(uintptr_t(retval
), uintptr_t(0));
55 EXPECT_EQ(thread_count
.val
, 5U);
56 EXPECT_EQ(call_count
, 1U);
59 static pthread_mutex_t once_func_blocker
;
60 static void blocking_once_func() {
61 LIBC_NAMESPACE::pthread_mutex_lock(&once_func_blocker
);
62 LIBC_NAMESPACE::pthread_mutex_unlock(&once_func_blocker
);
65 static LIBC_NAMESPACE::cpp::Atomic
<unsigned int> start_count
;
66 static LIBC_NAMESPACE::cpp::Atomic
<unsigned int> done_count
;
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);
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.
78 void test_synchronization() {
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);
88 LIBC_NAMESPACE::pthread_create(&t1
, nullptr, once_func_caller
, nullptr),
91 LIBC_NAMESPACE::pthread_create(&t2
, nullptr, once_func_caller
, nullptr),
94 while (start_count
.load() != 2)
95 ; // Spin until both threads start.
97 // Since the once func is blocked, the threads should not be done yet.
98 EXPECT_EQ(done_count
.val
, 0U);
100 // Unlock the blocking mutex so that the once func blocks.
101 ASSERT_EQ(LIBC_NAMESPACE::pthread_mutex_unlock(&once_func_blocker
), 0);
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);
109 ASSERT_EQ(done_count
.val
, 2U);
111 LIBC_NAMESPACE::pthread_mutex_destroy(&once_func_blocker
);
115 call_from_5_threads();
116 test_synchronization();