1 //===-- Tests for thrd_t creation and joining -----------------------------===// 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/threads/thrd_create.h" 10 #include "src/threads/thrd_join.h" 11 12 #include "utils/IntegrationTest/test.h" 13 14 #include <threads.h> 15 16 static constexpr int thread_count = 1000; 17 static int counter = 0; 18 static int thread_func(void *) { 19 ++counter; 20 return 0; 21 } 22 23 void create_and_join() { 24 for (counter = 0; counter <= thread_count;) { 25 thrd_t thread; 26 int old_counter_val = counter; 27 ASSERT_EQ(__llvm_libc::thrd_create(&thread, thread_func, nullptr), 28 (int)thrd_success); 29 int retval = thread_count + 1; // Start with a retval we dont expect. 30 ASSERT_EQ(__llvm_libc::thrd_join(&thread, &retval), (int)thrd_success); 31 ASSERT_EQ(retval, 0); 32 ASSERT_EQ(counter, old_counter_val + 1); 33 } 34 } 35 36 static int return_arg(void *arg) { return *reinterpret_cast<int *>(arg); } 37 38 void spawn_and_join() { 39 thrd_t thread_list[thread_count]; 40 int args[thread_count]; 41 42 for (int i = 0; i < thread_count; ++i) { 43 args[i] = i; 44 ASSERT_EQ(__llvm_libc::thrd_create(thread_list + i, return_arg, args + i), 45 (int)thrd_success); 46 } 47 48 for (int i = 0; i < thread_count; ++i) { 49 int retval = thread_count + 1; // Start with a retval we dont expect. 50 ASSERT_EQ(__llvm_libc::thrd_join(&thread_list[i], &retval), 51 (int)thrd_success); 52 ASSERT_EQ(retval, i); 53 } 54 } 55 56 int main() { 57 create_and_join(); 58 spawn_and_join(); 59 } 60