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