]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / stop_token / stop_token.cc
1 // Copyright (C) 2019-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-add-options libatomic }
19 // { dg-do run { target c++20 } }
20
21 #include <stop_token>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 std::stop_source ssrc;
28 std::stop_token tok = ssrc.get_token();
29 VERIFY(tok.stop_possible());
30 VERIFY(!tok.stop_requested());
31
32 std::stop_token copy(tok);
33 VERIFY(copy == tok);
34 VERIFY(copy.stop_possible());
35 VERIFY(!copy.stop_requested());
36 VERIFY(tok.stop_possible());
37 VERIFY(!tok.stop_requested());
38
39 std::stop_token move(std::move(tok));
40 VERIFY(move != tok);
41 VERIFY(move == copy);
42 VERIFY(move.stop_possible());
43 VERIFY(!move.stop_requested());
44 VERIFY(!tok.stop_possible());
45 VERIFY(!tok.stop_requested());
46 VERIFY(copy.stop_possible());
47 VERIFY(!copy.stop_requested());
48
49 ssrc.request_stop();
50 VERIFY(move.stop_possible());
51 VERIFY(move.stop_requested());
52 VERIFY(!tok.stop_possible());
53 VERIFY(!tok.stop_requested());
54 VERIFY(copy.stop_possible());
55 VERIFY(copy.stop_requested());
56
57 tok.swap(move);
58 VERIFY(tok == copy);
59 VERIFY(!move.stop_possible());
60 VERIFY(!move.stop_requested());
61 VERIFY(tok.stop_possible());
62 VERIFY(tok.stop_requested());
63 VERIFY(copy.stop_possible());
64 VERIFY(copy.stop_requested());
65
66 swap(move, copy);
67 VERIFY(tok == move);
68 VERIFY(move.stop_possible());
69 VERIFY(move.stop_requested());
70 VERIFY(tok.stop_possible());
71 VERIFY(tok.stop_requested());
72 VERIFY(!copy.stop_possible());
73 VERIFY(!copy.stop_requested());
74 }
75
76 void
77 test02()
78 {
79 std::stop_source src1, src2;
80 std::stop_token tok = src1.get_token();
81 VERIFY(tok.stop_possible());
82 VERIFY(!tok.stop_requested());
83
84 std::stop_token copy = src2.get_token();
85 VERIFY(copy != tok);
86 copy = tok;
87 VERIFY(copy == tok);
88 copy = src2.get_token();
89 VERIFY(copy != tok);
90 }
91
92 void
93 test03()
94 {
95 // create stop_source
96 std::stop_source ssrc;
97 VERIFY(ssrc.stop_possible());
98 VERIFY(!ssrc.stop_requested());
99
100 // create stop_token from stop_source
101 std::stop_token stok{ssrc.get_token()};
102 VERIFY(ssrc.stop_possible());
103 VERIFY(!ssrc.stop_requested());
104 VERIFY(stok.stop_possible());
105 VERIFY(!stok.stop_requested());
106
107 // register callback
108 bool cb1called{false};
109 auto cb1 = [&]{
110 cb1called = true;
111 };
112 {
113 std::stop_callback scb1{stok, cb1};
114 VERIFY(ssrc.stop_possible());
115 VERIFY(!ssrc.stop_requested());
116 VERIFY(stok.stop_possible());
117 VERIFY(!stok.stop_requested());
118 VERIFY(!cb1called);
119 } // unregister callback
120
121 // register another callback
122 bool cb2called{false};
123 auto cb2 = [&]{
124 VERIFY(stok.stop_requested());
125 cb2called = true;
126 };
127 std::stop_callback scb2a{stok, cb2}; // copies cb2
128 // std::stop_callback scb2b{stok, std::move(cb2)};
129 VERIFY(ssrc.stop_possible());
130 VERIFY(!ssrc.stop_requested());
131 VERIFY(stok.stop_possible());
132 VERIFY(!stok.stop_requested());
133 VERIFY(!cb1called);
134 VERIFY(!cb2called);
135
136 // request stop
137 auto b = ssrc.request_stop();
138 VERIFY(b);
139 VERIFY(ssrc.stop_possible());
140 VERIFY(ssrc.stop_requested());
141 VERIFY(stok.stop_possible());
142 VERIFY(stok.stop_requested());
143 VERIFY(!cb1called);
144 VERIFY(cb2called);
145
146 b = ssrc.request_stop();
147 VERIFY(!b);
148
149 // register another callback
150 bool cb3called{false};
151 std::stop_callback scb3{stok, [&]
152 {
153 cb3called = true;
154 }};
155 VERIFY(ssrc.stop_possible());
156 VERIFY(ssrc.stop_requested());
157 VERIFY(stok.stop_possible());
158 VERIFY(stok.stop_requested());
159 VERIFY(!cb1called);
160 VERIFY(cb2called);
161 VERIFY(cb3called);
162 }
163
164 int main()
165 {
166 test01();
167 test02();
168 test03();
169 }