xref: /llvm-project/libcxxabi/test/thread_local_destruction_order.pass.cpp (revision 7f00b2aa75f0f090c37bf9d5ccb55fca15c35538)
1 //===----------------------------------------------------------------------===//
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 // UNSUPPORTED: c++03
10 // UNSUPPORTED: no-threads
11 
12 // XFAIL: LIBCXX-FREEBSD-FIXME
13 
14 // TODO: This test does start working with newer updates of the mingw-w64
15 // toolchain, when it includes the following commit:
16 // https://github.com/mingw-w64/mingw-w64/commit/71eddccd746c56d9cde28bb5620d027d49259de9
17 // Thus, remove this UNSUPPORTED marking after the next update of the CI
18 // toolchain.
19 // UNSUPPORTED: target={{.*-windows-gnu}}
20 
21 #include <cassert>
22 #include <thread>
23 
24 #include "make_test_thread.h"
25 
26 int seq = 0;
27 
28 class OrderChecker {
29 public:
OrderChecker(int n)30   explicit OrderChecker(int n) : n_{n} { }
31 
~OrderChecker()32   ~OrderChecker() {
33     assert(seq++ == n_);
34   }
35 
36 private:
37   int n_;
38 };
39 
40 template <int ID>
41 class CreatesThreadLocalInDestructor {
42 public:
~CreatesThreadLocalInDestructor()43   ~CreatesThreadLocalInDestructor() {
44     thread_local OrderChecker checker{ID};
45   }
46 };
47 
48 OrderChecker global{7};
49 
thread_fn()50 void thread_fn() {
51   static OrderChecker fn_static{5};
52   thread_local CreatesThreadLocalInDestructor<2> creates_tl2;
53   thread_local OrderChecker fn_thread_local{1};
54   thread_local CreatesThreadLocalInDestructor<0> creates_tl0;
55 }
56 
main(int,char **)57 int main(int, char**) {
58   static OrderChecker fn_static{6};
59 
60   support::make_test_thread(thread_fn).join();
61   assert(seq == 3);
62 
63   thread_local OrderChecker fn_thread_local{4};
64   thread_local CreatesThreadLocalInDestructor<3> creates_tl;
65 
66   return 0;
67 }
68