1 //===-- Tests for thrd_t creation and joining -----------------------------===//
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/threads/thrd_create.h"
10 #include "src/threads/thrd_join.h"
12 #include "test/IntegrationTest/test.h"
16 static constexpr int thread_count
= 1000;
17 static int counter
= 0;
18 static int thread_func(void *) {
23 void create_and_join() {
24 for (counter
= 0; counter
<= thread_count
;) {
26 int old_counter_val
= counter
;
27 ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&thread
, thread_func
, nullptr),
29 int retval
= thread_count
+ 1; // Start with a retval we dont expect.
30 ASSERT_EQ(LIBC_NAMESPACE::thrd_join(thread
, &retval
), (int)thrd_success
);
32 ASSERT_EQ(counter
, old_counter_val
+ 1);
36 static int return_arg(void *arg
) { return *reinterpret_cast<int *>(arg
); }
38 void spawn_and_join() {
39 thrd_t thread_list
[thread_count
];
40 int args
[thread_count
];
42 for (int i
= 0; i
< thread_count
; ++i
) {
45 LIBC_NAMESPACE::thrd_create(thread_list
+ i
, return_arg
, args
+ i
),
49 for (int i
= 0; i
< thread_count
; ++i
) {
50 int retval
= thread_count
+ 1; // Start with a retval we dont expect.
51 ASSERT_EQ(LIBC_NAMESPACE::thrd_join(thread_list
[i
], &retval
),