Explicitly link with libatomic when needed.
[official-gcc.git] / libstdc++-v3 / testsuite / 30_threads / stop_token / stop_token.cc
blob2ab16765bea37c1229d4c07e86db7ac1aa69987c
1 // Copyright (C) 2019-2020 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.
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-options "-std=gnu++2a" }
19 // { dg-add-options libatomic }
20 // { dg-do run { target c++2a } }
22 #include <stop_token>
23 #include <testsuite_hooks.h>
25 void
26 test01()
28 std::stop_source ssrc;
29 std::stop_token tok = ssrc.get_token();
30 VERIFY(tok.stop_possible());
31 VERIFY(!tok.stop_requested());
33 std::stop_token copy(tok);
34 VERIFY(copy == tok);
35 VERIFY(copy.stop_possible());
36 VERIFY(!copy.stop_requested());
37 VERIFY(tok.stop_possible());
38 VERIFY(!tok.stop_requested());
40 std::stop_token move(std::move(tok));
41 VERIFY(move != tok);
42 VERIFY(move == copy);
43 VERIFY(move.stop_possible());
44 VERIFY(!move.stop_requested());
45 VERIFY(!tok.stop_possible());
46 VERIFY(!tok.stop_requested());
47 VERIFY(copy.stop_possible());
48 VERIFY(!copy.stop_requested());
50 ssrc.request_stop();
51 VERIFY(move.stop_possible());
52 VERIFY(move.stop_requested());
53 VERIFY(!tok.stop_possible());
54 VERIFY(!tok.stop_requested());
55 VERIFY(copy.stop_possible());
56 VERIFY(copy.stop_requested());
58 tok.swap(move);
59 VERIFY(tok == copy);
60 VERIFY(!move.stop_possible());
61 VERIFY(!move.stop_requested());
62 VERIFY(tok.stop_possible());
63 VERIFY(tok.stop_requested());
64 VERIFY(copy.stop_possible());
65 VERIFY(copy.stop_requested());
67 swap(move, copy);
68 VERIFY(tok == move);
69 VERIFY(move.stop_possible());
70 VERIFY(move.stop_requested());
71 VERIFY(tok.stop_possible());
72 VERIFY(tok.stop_requested());
73 VERIFY(!copy.stop_possible());
74 VERIFY(!copy.stop_requested());
77 void
78 test02()
80 std::stop_source src1, src2;
81 std::stop_token tok = src1.get_token();
82 VERIFY(tok.stop_possible());
83 VERIFY(!tok.stop_requested());
85 std::stop_token copy = src2.get_token();
86 VERIFY(copy != tok);
87 copy = tok;
88 VERIFY(copy == tok);
89 copy = src2.get_token();
90 VERIFY(copy != tok);
93 void
94 test03()
96 // create stop_source
97 std::stop_source ssrc;
98 VERIFY(ssrc.stop_possible());
99 VERIFY(!ssrc.stop_requested());
101 // create stop_token from stop_source
102 std::stop_token stok{ssrc.get_token()};
103 VERIFY(ssrc.stop_possible());
104 VERIFY(!ssrc.stop_requested());
105 VERIFY(stok.stop_possible());
106 VERIFY(!stok.stop_requested());
108 // register callback
109 bool cb1called{false};
110 auto cb1 = [&]{
111 cb1called = true;
114 std::stop_callback scb1{stok, cb1};
115 VERIFY(ssrc.stop_possible());
116 VERIFY(!ssrc.stop_requested());
117 VERIFY(stok.stop_possible());
118 VERIFY(!stok.stop_requested());
119 VERIFY(!cb1called);
120 } // unregister callback
122 // register another callback
123 bool cb2called{false};
124 auto cb2 = [&]{
125 VERIFY(stok.stop_requested());
126 cb2called = true;
128 std::stop_callback scb2a{stok, cb2}; // copies cb2
129 // std::stop_callback scb2b{stok, std::move(cb2)};
130 VERIFY(ssrc.stop_possible());
131 VERIFY(!ssrc.stop_requested());
132 VERIFY(stok.stop_possible());
133 VERIFY(!stok.stop_requested());
134 VERIFY(!cb1called);
135 VERIFY(!cb2called);
137 // request stop
138 auto b = ssrc.request_stop();
139 VERIFY(b);
140 VERIFY(ssrc.stop_possible());
141 VERIFY(ssrc.stop_requested());
142 VERIFY(stok.stop_possible());
143 VERIFY(stok.stop_requested());
144 VERIFY(!cb1called);
145 VERIFY(cb2called);
147 b = ssrc.request_stop();
148 VERIFY(!b);
150 // register another callback
151 bool cb3called{false};
152 std::stop_callback scb3{stok, [&]
154 cb3called = true;
156 VERIFY(ssrc.stop_possible());
157 VERIFY(ssrc.stop_requested());
158 VERIFY(stok.stop_possible());
159 VERIFY(stok.stop_requested());
160 VERIFY(!cb1called);
161 VERIFY(cb2called);
162 VERIFY(cb3called);
165 int main()
167 test01();
168 test02();
169 test03();