aboutsummaryrefslogtreecommitdiff
path: root/libc/test/integration/src/pthread/pthread_test.cpp
blob: 4d635fd9a96da0ec994b1c6049631513c70bb240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//===-- Tests for pthread_t -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "hdr/stdint_proxy.h" // uintptr_t
#include "src/pthread/pthread_create.h"
#include "src/pthread/pthread_join.h"
#include "test/IntegrationTest/test.h"

#include <pthread.h>

static constexpr int thread_count = 1000;
static int counter = 0;
static void *thread_func(void *) {
  ++counter;
  return nullptr;
}

void create_and_join() {
  for (counter = 0; counter <= thread_count;) {
    pthread_t thread;
    int old_counter_val = counter;
    ASSERT_EQ(
        LIBC_NAMESPACE::pthread_create(&thread, nullptr, thread_func, nullptr),
        0);

    // Start with a retval we dont expect.
    void *retval = reinterpret_cast<void *>(thread_count + 1);
    ASSERT_EQ(LIBC_NAMESPACE::pthread_join(thread, &retval), 0);
    ASSERT_EQ(uintptr_t(retval), uintptr_t(nullptr));
    ASSERT_EQ(counter, old_counter_val + 1);
  }
}

static void *return_arg(void *arg) { return arg; }

void spawn_and_join() {
  pthread_t thread_list[thread_count];
  int args[thread_count];

  for (int i = 0; i < thread_count; ++i) {
    args[i] = i;
    ASSERT_EQ(LIBC_NAMESPACE::pthread_create(thread_list + i, nullptr,
                                             return_arg, args + i),
              0);
  }

  for (int i = 0; i < thread_count; ++i) {
    // Start with a retval we dont expect.
    void *retval = reinterpret_cast<void *>(thread_count + 1);
    ASSERT_EQ(LIBC_NAMESPACE::pthread_join(thread_list[i], &retval), 0);
    ASSERT_EQ(*reinterpret_cast<int *>(retval), i);
  }
}

TEST_MAIN() {
  create_and_join();
  spawn_and_join();
  return 0;
}