• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: no-threads
10 // UNSUPPORTED: libcpp-has-no-experimental-stop_token
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // XFAIL: availability-synchronization_library-missing
13 
14 // friend void swap(jthread& x, jthread& y) noexcept;
15 
16 #include <cassert>
17 #include <thread>
18 #include <type_traits>
19 
20 #include "make_test_thread.h"
21 #include "test_macros.h"
22 
23 template <class T>
24 concept IsFreeSwapNoexcept = requires(T& a, T& b) {
25   { swap(a, b) } noexcept;
26 };
27 
28 static_assert(IsFreeSwapNoexcept<std::jthread>);
29 
main(int,char **)30 int main(int, char**) {
31   // x is default constructed
32   {
33     std::jthread t1;
34     std::jthread t2        = support::make_test_jthread([] {});
35     const auto originalId2 = t2.get_id();
36     swap(t1, t2);
37 
38     assert(t1.get_id() == originalId2);
39     assert(t2.get_id() == std::jthread::id());
40   }
41 
42   // y is default constructed
43   {
44     std::jthread t1 = support::make_test_jthread([] {});
45     std::jthread t2{};
46     const auto originalId1 = t1.get_id();
47     swap(t1, t2);
48 
49     assert(t1.get_id() == std::jthread::id());
50     assert(t2.get_id() == originalId1);
51   }
52 
53   // both not default constructed
54   {
55     std::jthread t1        = support::make_test_jthread([] {});
56     std::jthread t2        = support::make_test_jthread([] {});
57     const auto originalId1 = t1.get_id();
58     const auto originalId2 = t2.get_id();
59     swap(t1, t2);
60 
61     assert(t1.get_id() == originalId2);
62     assert(t2.get_id() == originalId1);
63   }
64 
65   // both default constructed
66   {
67     std::jthread t1;
68     std::jthread t2;
69     swap(t1, t2);
70 
71     assert(t1.get_id() == std::jthread::id());
72     assert(t2.get_id() == std::jthread::id());
73   }
74 
75   return 0;
76 }
77