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 | // ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-self-move |
14 | |
15 | // jthread& operator=(jthread&&) noexcept; |
16 | |
17 | #include <atomic> |
18 | #include <cassert> |
19 | #include <concepts> |
20 | #include <stop_token> |
21 | #include <thread> |
22 | #include <type_traits> |
23 | #include <utility> |
24 | #include <vector> |
25 | |
26 | #include "make_test_thread.h" |
27 | #include "test_macros.h" |
28 | |
29 | static_assert(std::is_nothrow_move_assignable_v<std::jthread>); |
30 | |
31 | int main(int, char**) { |
32 | // If &x == this is true, there are no effects. |
33 | { |
34 | std::jthread j = support::make_test_jthread([] {}); |
35 | auto id = j.get_id(); |
36 | auto ssource = j.get_stop_source(); |
37 | j = std::move(j); |
38 | assert(j.get_id() == id); |
39 | assert(j.get_stop_source() == ssource); |
40 | } |
41 | |
42 | // if joinable() is true, calls request_stop() and then join() |
43 | // request_stop is called |
44 | { |
45 | std::jthread j1 = support::make_test_jthread([] {}); |
46 | bool called = false; |
47 | std::stop_callback cb(j1.get_stop_token(), [&called] { called = true; }); |
48 | |
49 | std::jthread j2 = support::make_test_jthread([] {}); |
50 | j1 = std::move(j2); |
51 | assert(called); |
52 | } |
53 | |
54 | // if joinable() is true, calls request_stop() and then join() |
55 | // join is called |
56 | { |
57 | std::atomic_int calledTimes = 0; |
58 | std::vector<std::jthread> jts; |
59 | constexpr auto numberOfThreads = 10u; |
60 | jts.reserve(numberOfThreads); |
61 | for (auto i = 0u; i < numberOfThreads; ++i) { |
62 | jts.emplace_back(support::make_test_jthread([&] { |
63 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
64 | calledTimes.fetch_add(1, std::memory_order_relaxed); |
65 | })); |
66 | } |
67 | |
68 | for (auto i = 0u; i < numberOfThreads; ++i) { |
69 | jts[i] = std::jthread{}; |
70 | } |
71 | |
72 | // If join was called as expected, calledTimes must equal to numberOfThreads |
73 | // If join was not called, there is a chance that the check below happened |
74 | // before test threads incrementing the counter, thus calledTimed would |
75 | // be less than numberOfThreads. |
76 | // This is not going to catch issues 100%. Creating more threads to increase |
77 | // the probability of catching the issue |
78 | assert(calledTimes.load(std::memory_order_relaxed) == numberOfThreads); |
79 | } |
80 | |
81 | // then assigns the state of x to *this |
82 | { |
83 | std::jthread j1 = support::make_test_jthread([] {}); |
84 | std::jthread j2 = support::make_test_jthread([] {}); |
85 | auto id2 = j2.get_id(); |
86 | auto ssource2 = j2.get_stop_source(); |
87 | |
88 | j1 = std::move(j2); |
89 | |
90 | assert(j1.get_id() == id2); |
91 | assert(j1.get_stop_source() == ssource2); |
92 | } |
93 | |
94 | // sets x to a default constructed state |
95 | { |
96 | std::jthread j1 = support::make_test_jthread([] {}); |
97 | std::jthread j2 = support::make_test_jthread([] {}); |
98 | j1 = std::move(j2); |
99 | |
100 | assert(j2.get_id() == std::jthread::id()); |
101 | assert(!j2.get_stop_source().stop_possible()); |
102 | } |
103 | |
104 | // joinable is false |
105 | { |
106 | std::jthread j1; |
107 | std::jthread j2 = support::make_test_jthread([] {}); |
108 | |
109 | auto j2Id = j2.get_id(); |
110 | |
111 | j1 = std::move(j2); |
112 | |
113 | assert(j1.get_id() == j2Id); |
114 | } |
115 | |
116 | return 0; |
117 | } |
118 | |