]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
1 // Copyright (C) 2019-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-add-options libatomic }
19 // { dg-do run { target c++20 } }
22 #include <testsuite_hooks.h>
27 std::stop_source ssrc
;
28 std::stop_token tok
= ssrc
.get_token();
29 VERIFY(tok
.stop_possible());
30 VERIFY(!tok
.stop_requested());
32 std::stop_token
copy(tok
);
34 VERIFY(copy
.stop_possible());
35 VERIFY(!copy
.stop_requested());
36 VERIFY(tok
.stop_possible());
37 VERIFY(!tok
.stop_requested());
39 std::stop_token
move(std::move(tok
));
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());
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());
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());
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());
79 std::stop_source src1
, src2
;
80 std::stop_token tok
= src1
.get_token();
81 VERIFY(tok
.stop_possible());
82 VERIFY(!tok
.stop_requested());
84 std::stop_token copy
= src2
.get_token();
88 copy
= src2
.get_token();
96 std::stop_source ssrc
;
97 VERIFY(ssrc
.stop_possible());
98 VERIFY(!ssrc
.stop_requested());
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());
108 bool cb1called
{false};
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());
119 } // unregister callback
121 // register another callback
122 bool cb2called
{false};
124 VERIFY(stok
.stop_requested());
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());
137 auto b
= ssrc
.request_stop();
139 VERIFY(ssrc
.stop_possible());
140 VERIFY(ssrc
.stop_requested());
141 VERIFY(stok
.stop_possible());
142 VERIFY(stok
.stop_requested());
146 b
= ssrc
.request_stop();
149 // register another callback
150 bool cb3called
{false};
151 std::stop_callback scb3
{stok
, [&]
155 VERIFY(ssrc
.stop_possible());
156 VERIFY(ssrc
.stop_requested());
157 VERIFY(stok
.stop_possible());
158 VERIFY(stok
.stop_requested());