Debian Codegraph @

libstdc++-v3/testsuite/util/atomic/wait_notify_util.h

raw
1 // Copyright (C) 2020-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 #include <atomic>
19 #include <chrono>
20 #include <condition_variable>
21 #include <concepts>
22 #include <mutex>
23 #include <thread>
24 #include <cstring>
25
26 #include <testsuite_hooks.h>
27
28 template<typename Tp>
29 Tp check_wait_notify(Tp val1, Tp val2)
30 requires std::equality_comparable<Tp>
31 {
32 using namespace std::literals::chrono_literals;
33
34 std::mutex m;
35 std::condition_variable cv;
36 std::unique_lock<std::mutex> l(m);
37
38 std::atomic<Tp> a(val1);
39 std::thread t([&]
40 {
41 {
42 // This ensures we block until cv.wait(l) starts.
43 std::lock_guard<std::mutex> ll(m);
44 }
45 cv.notify_one();
46 a.wait(val1);
47 if (a.load() != val2)
48 a = val1;
49 });
50 cv.wait(l);
51 std::this_thread::sleep_for(100ms);
52 a.store(val2);
53 a.notify_one();
54 t.join();
55 return a.load();
56 }
57
58 template<typename Tp>
59 Tp check_wait_notify(Tp val1, Tp val2)
60 {
61 using namespace std::literals::chrono_literals;
62
63 std::mutex m;
64 std::condition_variable cv;
65 std::unique_lock<std::mutex> l(m);
66
67 std::atomic<Tp> a(val1);
68 std::thread t([&]
69 {
70 {
71 // This ensures we block until cv.wait(l) starts.
72 std::lock_guard<std::mutex> ll(m);
73 }
74 cv.notify_one();
75 a.wait(val1);
76 auto v = a.load();
77 // TODO this needs to zero padding bits when we can do that
78 if (std::memcmp(&v, &val2, sizeof(Tp)) != 0)
79 a = val1;
80 });
81 cv.wait(l);
82 std::this_thread::sleep_for(100ms);
83 a.store(val2);
84 a.notify_one();
85 t.join();
86 return a.load();
87 }
88
89 template<typename Tp>
90 Tp check_atomic_wait_notify(Tp val1, Tp val2)
91 requires std::equality_comparable<Tp>
92 {
93 using namespace std::literals::chrono_literals;
94
95 std::mutex m;
96 std::condition_variable cv;
97 std::unique_lock<std::mutex> l(m);
98
99 std::atomic<Tp> a(val1);
100 std::thread t([&]
101 {
102 {
103 // This ensures we block until cv.wait(l) starts.
104 std::lock_guard<std::mutex> ll(m);
105 }
106 cv.notify_one();
107 std::atomic_wait(&a, val1);
108 if (a.load() != val2)
109 a = val1;
110 });
111 cv.wait(l);
112 std::this_thread::sleep_for(100ms);
113 a.store(val2);
114 std::atomic_notify_one(&a);
115 t.join();
116 return a.load();
117 }
118
119 template<typename Tp>
120 Tp check_atomic_wait_notify(Tp val1, Tp val2)
121 {
122 using namespace std::literals::chrono_literals;
123
124 std::mutex m;
125 std::condition_variable cv;
126 std::unique_lock<std::mutex> l(m);
127
128 std::atomic<Tp> a(val1);
129 std::thread t([&]
130 {
131 {
132 // This ensures we block until cv.wait(l) starts.
133 std::lock_guard<std::mutex> ll(m);
134 }
135 cv.notify_one();
136 std::atomic_wait(&a, val1);
137 auto v = a.load();
138 // TODO this needs to zero padding bits when we can do that
139 if (std::memcmp(&v, &val2, sizeof(Tp)) != 0)
140 a = val1;
141 });
142 cv.wait(l);
143 std::this_thread::sleep_for(100ms);
144 a.store(val2);
145 std::atomic_notify_one(&a);
146 t.join();
147 return a.load();
148 }
149
150 template<typename Tp>
151 struct check
152 {
153 check(Tp a = 0, Tp b = 42)
154 {
155 if constexpr (std::equality_comparable<Tp>)
156 {
157 VERIFY( check_wait_notify(a, b) == b);
158 VERIFY( check_atomic_wait_notify(a, b) == b);
159 }
160 else
161 {
162 {
163 // TODO this needs to zero padding bits when we can do that
164 auto v = check_wait_notify(a, b);
165 VERIFY( std::memcmp(&v, &b, sizeof(Tp)) == 0 );
166 }
167
168 {
169 // TODO this needs to zero padding bits when we can do that
170 auto v = check_atomic_wait_notify(a, b);
171 VERIFY( std::memcmp(&v, &b, sizeof(Tp)) == 0);
172 }
173 }
174 }
175 };