summaryrefslogtreecommitdiff
path: root/libc/test/integration/src/__support/threads/double_exit_test.cpp
blob: 86749d937c10c031d156f0c279c9978b7c63a9a6 (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
//===-- Test handling of thread local data --------------------------------===//
//
// 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/threads/thread.h"
#include "src/stdlib/exit.h"
#include "test/IntegrationTest/test.h"

extern "C" {
[[gnu::weak]]
void *__dso_handle = nullptr;
int __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso);
}

int call_num = 0;

[[gnu::destructor]]
void check() {
  // This destructor should be called only once.
  if (call_num != 1)
    __builtin_trap();
}

TEST_MAIN() {
  __cxa_thread_atexit_impl([](void *) { LIBC_NAMESPACE::exit(0); }, nullptr,
                           __dso_handle);
  __cxa_thread_atexit_impl([](void *) { ++call_num; }, nullptr, __dso_handle);
  LIBC_NAMESPACE::exit(1);
}