aboutsummaryrefslogtreecommitdiff
path: root/libc/test/integration/src/threads/call_once_test.cpp
blob: efb920d00723a4d9d0ea12cc859393cd2c53c757 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===-- Tests for call_once -----------------------------------------------===//
//
// 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 "src/__support/CPP/atomic.h"
#include "src/threads/call_once.h"
#include "src/threads/mtx_destroy.h"
#include "src/threads/mtx_init.h"
#include "src/threads/mtx_lock.h"
#include "src/threads/mtx_unlock.h"
#include "src/threads/thrd_create.h"
#include "src/threads/thrd_join.h"

#include "test/IntegrationTest/test.h"

#include <threads.h>

static constexpr unsigned int NUM_THREADS = 5;
static LIBC_NAMESPACE::cpp::Atomic<unsigned int> thread_count;

static unsigned int call_count;
static void call_once_func() { ++call_count; }

static int func(void *) {
  static once_flag flag = ONCE_FLAG_INIT;
  LIBC_NAMESPACE::call_once(&flag, call_once_func);

  thread_count.fetch_add(1);

  return 0;
}

void call_from_5_threads() {
  // Ensure the call count and thread count are 0 to begin with.
  call_count = 0;
  thread_count = 0;

  thrd_t threads[NUM_THREADS];
  for (unsigned int i = 0; i < NUM_THREADS; ++i) {
    ASSERT_EQ(LIBC_NAMESPACE::thrd_create(threads + i, func, nullptr),
              static_cast<int>(thrd_success));
  }

  for (unsigned int i = 0; i < NUM_THREADS; ++i) {
    int retval;
    ASSERT_EQ(LIBC_NAMESPACE::thrd_join(threads[i], &retval),
              static_cast<int>(thrd_success));
    ASSERT_EQ(retval, 0);
  }

  EXPECT_EQ(thread_count.val, 5U);
  EXPECT_EQ(call_count, 1U);
}

static mtx_t once_func_blocker;
static void blocking_once_func() {
  LIBC_NAMESPACE::mtx_lock(&once_func_blocker);
  LIBC_NAMESPACE::mtx_unlock(&once_func_blocker);
}

static LIBC_NAMESPACE::cpp::Atomic<unsigned int> start_count;
static LIBC_NAMESPACE::cpp::Atomic<unsigned int> done_count;
static int once_func_caller(void *) {
  static once_flag flag;
  start_count.fetch_add(1);
  LIBC_NAMESPACE::call_once(&flag, blocking_once_func);
  done_count.fetch_add(1);
  return 0;
}

// Test the synchronization aspect of the call_once function.
// This is not a fool proof test, but something which might be
// useful when we add a flakiness detection scheme to UnitTest.
void test_synchronization() {
  start_count = 0;
  done_count = 0;

  ASSERT_EQ(LIBC_NAMESPACE::mtx_init(&once_func_blocker, mtx_plain),
            static_cast<int>(thrd_success));
  // Lock the blocking mutex so that the once func blocks.
  ASSERT_EQ(LIBC_NAMESPACE::mtx_lock(&once_func_blocker),
            static_cast<int>(thrd_success));

  thrd_t t1, t2;
  ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&t1, once_func_caller, nullptr),
            static_cast<int>(thrd_success));
  ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&t2, once_func_caller, nullptr),
            static_cast<int>(thrd_success));

  while (start_count.load() != 2)
    ; // Spin until both threads start.

  // Since the once func is blocked, the threads should not be done yet.
  EXPECT_EQ(done_count.val, 0U);

  // Unlock the blocking mutex so that the once func blocks.
  ASSERT_EQ(LIBC_NAMESPACE::mtx_unlock(&once_func_blocker),
            static_cast<int>(thrd_success));

  int retval;
  ASSERT_EQ(LIBC_NAMESPACE::thrd_join(t1, &retval),
            static_cast<int>(thrd_success));
  ASSERT_EQ(retval, 0);
  ASSERT_EQ(LIBC_NAMESPACE::thrd_join(t2, &retval),
            static_cast<int>(thrd_success));
  ASSERT_EQ(retval, 0);

  ASSERT_EQ(done_count.val, 2U);

  LIBC_NAMESPACE::mtx_destroy(&once_func_blocker);
}

TEST_MAIN() {
  call_from_5_threads();
  test_synchronization();
  return 0;
}