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 | // void swap(jthread& x) 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 IsMemberSwapNoexcept = requires(T& a, T& b) { |
25 | { a.swap(b) } noexcept; |
26 | }; |
27 | |
28 | static_assert(IsMemberSwapNoexcept<std::jthread>); |
29 | |
30 | int main(int, char**) { |
31 | // this is default constructed |
32 | { |
33 | std::jthread t1; |
34 | std::jthread t2 = support::make_test_jthread([] {}); |
35 | const auto originalId2 = t2.get_id(); |
36 | t1.swap(t&: t2); |
37 | |
38 | assert(t1.get_id() == originalId2); |
39 | assert(t2.get_id() == std::jthread::id()); |
40 | } |
41 | |
42 | // that is default constructed |
43 | { |
44 | std::jthread t1 = support::make_test_jthread([] {}); |
45 | std::jthread t2{}; |
46 | const auto originalId1 = t1.get_id(); |
47 | t1.swap(t&: 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 | t1.swap(t&: 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 | t1.swap(t&: 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 | |