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 #include <cassert>
13 #include <thread>
14 
15 #include "make_test_thread.h"
16 
17 int seq = 0;
18 
19 class OrderChecker {
20 public:
OrderChecker(int n)21   explicit OrderChecker(int n) : n_{n} { }
22 
~OrderChecker()23   ~OrderChecker() {
24     assert(seq++ == n_);
25   }
26 
27 private:
28   int n_;
29 };
30 
31 template <int ID>
32 class CreatesThreadLocalInDestructor {
33 public:
~CreatesThreadLocalInDestructor()34   ~CreatesThreadLocalInDestructor() {
35     thread_local OrderChecker checker{ID};
36   }
37 };
38 
39 OrderChecker global{7};
40 
thread_fn()41 void thread_fn() {
42   static OrderChecker fn_static{5};
43   thread_local CreatesThreadLocalInDestructor<2> creates_tl2;
44   thread_local OrderChecker fn_thread_local{1};
45   thread_local CreatesThreadLocalInDestructor<0> creates_tl0;
46 }
47 
main(int,char **)48 int main(int, char**) {
49   static OrderChecker fn_static{6};
50 
51   support::make_test_thread(thread_fn).join();
52   assert(seq == 3);
53 
54   thread_local OrderChecker fn_thread_local{4};
55   thread_local CreatesThreadLocalInDestructor<3> creates_tl;
56 
57   return 0;
58 }
59