Debian Codegraph @

libstdc++-v3/testsuite/30_threads/condition_variable_any/stop_token/wait_on.cc

raw
1 // Copyright (C) 2019-2025 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 // { dg-additional-options "-pthread" { target pthread } }
21
22 #include <condition_variable>
23 #include <thread>
24 #include <mutex>
25 #include <chrono>
26 #include <testsuite_hooks.h>
27
28 using namespace::std::literals;
29
30 void test_wait_stop()
31 {
32 bool ready = false;
33 std::mutex mtx;
34 std::condition_variable_any cv;
35
36 std::stop_source src;
37
38 auto tok = src.get_token();
39 std::thread t([&ready, &mtx, &cv, tok]
40 {
41 std::unique_lock lck(mtx);
42 auto res = cv.wait(lck, tok, [&ready] { return ready; });
43 if (!res)
44 {
45 VERIFY(tok.stop_requested());
46 }
47 });
48
49 std::this_thread::sleep_for(0.5s);
50 VERIFY(!src.stop_requested());
51 src.request_stop();
52 t.join();
53 VERIFY(src.stop_requested());
54 }
55
56 void test_wait_until(bool ck = true)
57 {
58 bool ready = false;
59 std::mutex mtx;
60 std::condition_variable_any cv;
61
62 std::stop_source src;
63
64 auto abst = std::chrono::steady_clock::now() + 1.0s;
65 auto tok = src.get_token();
66 std::thread t([ck, &ready, &mtx, &cv, abst, tok]
67 {
68 std::unique_lock lck(mtx);
69 auto res = cv.wait_until(lck, tok, abst, [&ready] { return ready; });
70 if (!res && ck)
71 {
72 VERIFY(tok.stop_requested());
73 }
74 });
75
76 if (ck)
77 {
78 std::this_thread::sleep_for(0.5s);
79 VERIFY(!src.stop_requested());
80 src.request_stop();
81 t.join();
82 VERIFY(src.stop_requested());
83 }
84 else
85 {
86 std::this_thread::sleep_for(1.5s);
87 t.join();
88 VERIFY(!src.stop_requested());
89 }
90 }
91
92 void test_wait_for(bool ck = true)
93 {
94 bool ready = false;
95 std::mutex mtx;
96 std::condition_variable_any cv;
97
98 std::stop_source src;
99
100 auto tok = src.get_token();
101 std::thread t([ck, &ready, &mtx, &cv, tok]
102 {
103 std::unique_lock lck(mtx);
104 auto res = cv.wait_for(lck, tok, 1.0s, [&ready] { return ready; });
105 if (!res && ck)
106 {
107 VERIFY(tok.stop_requested());
108 }
109 });
110
111 if (ck)
112 {
113 std::this_thread::sleep_for(0.5s);
114 VERIFY(!src.stop_requested());
115 src.request_stop();
116 t.join();
117 VERIFY(src.stop_requested());
118 }
119 else
120 {
121 std::this_thread::sleep_for(1.5s);
122 t.join();
123 VERIFY(!src.stop_requested());
124 };
125 }
126
127 int main()
128 {
129 test_wait_stop();
130 test_wait_until(false);
131 test_wait_until();
132 test_wait_for();
133 test_wait_for(false);
134 return 0;
135 }