1 // Copyright (C) 2019-2021 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-options "-std=gnu++2a" }
19 // { dg-add-options libatomic }
20 // { dg-do run { target c++2a } }
21
22 #include <stop_token>
23 #include <functional>
24 #include <testsuite_hooks.h>
25
26 void
test01()27 test01()
28 {
29 bool called = false;
30 std::function<void()> f = [&called]{ called = true; };
31 std::stop_source ssrc;
32 ssrc.request_stop();
33 std::stop_token tok = ssrc.get_token();
34 std::stop_callback cb1(tok, f);
35 VERIFY( tok.stop_possible() );
36 VERIFY( f != nullptr );
37 VERIFY( called == true );
38
39 called = false;
40 std::stop_callback cb2(std::move(tok), f);
41 // when callback is executed immediately, no change in ownership:
42 VERIFY( tok.stop_possible() );
43 VERIFY( f != nullptr );
44 VERIFY( called == true );
45
46 std::stop_token sink(std::move(tok)); // leave tok empty
47
48 called = false;
49 std::stop_callback cb3(tok, f);
50 VERIFY( f != nullptr );
51 VERIFY( called == false );
52
53 called = false;
54 std::stop_callback cb4(std::move(tok), f);
55 VERIFY( f != nullptr );
56 VERIFY( called == false );
57 }
58
59 void
test02()60 test02()
61 {
62 bool called = false;
63 std::function<void()> f0 = [&called]{ called = true; };
64 std::function<void()> f = f0;
65 std::stop_source ssrc;
66 ssrc.request_stop();
67 std::stop_token tok = ssrc.get_token();
68
69 std::stop_callback cb1(tok, std::move(f));
70 VERIFY( tok.stop_possible() );
71 VERIFY( f == nullptr );
72 VERIFY( called == true );
73
74 called = false;
75 f = f0;
76 std::stop_callback cb2(std::move(tok), std::move(f));
77 // when callback is executed immediately, no change in ownership:
78 VERIFY( tok.stop_possible() );
79 VERIFY( f == nullptr );
80 VERIFY( called == true );
81
82 std::stop_token sink(std::move(tok)); // leave tok empty
83
84 called = false;
85 f = f0;
86 std::stop_callback cb3(tok, std::move(f));
87 VERIFY( f == nullptr );
88 VERIFY( called == false );
89
90 called = false;
91 f = f0;
92 std::stop_callback cb4(std::move(tok), std::move(f));
93 VERIFY( f == nullptr );
94 VERIFY( called == false );
95 }
96
97 void
test03()98 test03()
99 {
100 bool called[4] = { };
101 std::stop_source ssrc;
102 std::stop_token tok = ssrc.get_token();
103 std::stop_callback cb1(tok, [&]{ called[0] = true; });
104 VERIFY( tok.stop_possible() );
105 VERIFY( called[0] == false );
106
107 std::stop_callback cb2(std::move(tok), [&]{ called[1] = true; });
108 VERIFY( !tok.stop_possible() );
109 VERIFY( called[1] == false );
110
111 std::stop_callback cb3(tok, [&]{ called[2] = true; });
112 VERIFY( called[2] == false );
113
114 std::stop_callback cb4(std::move(tok), [&]{ called[3] = true; });
115 VERIFY( called[3] == false );
116
117 ssrc.request_stop();
118 VERIFY( called[0] == true );
119 VERIFY( called[1] == true );
120 VERIFY( called[2] == false );
121 VERIFY( called[3] == false );
122 }
123
main()124 int main()
125 {
126 test01();
127 test02();
128 test03();
129 }
130