|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
3 | 3 |
|
4 | 4 | // Test GH-685 "wait_for in condition_variable_any should unlock and lock" |
| 5 | +// Test LWG-4301 "condition_variable{_any}::wait_{for, until} should take timeout by value" |
5 | 6 |
|
| 7 | +#include <atomic> |
6 | 8 | #include <cassert> |
7 | 9 | #include <chrono> |
8 | 10 | #include <condition_variable> |
| 11 | +#include <cstdio> |
9 | 12 | #include <mutex> |
| 13 | +#include <thread> |
10 | 14 | #include <type_traits> |
11 | 15 |
|
| 16 | +#if _HAS_CXX20 |
| 17 | +#include <stop_token> |
| 18 | +#endif // _HAS_CXX20 |
| 19 | + |
12 | 20 | using namespace std; |
13 | 21 | using namespace std::chrono; |
14 | 22 |
|
@@ -79,9 +87,135 @@ namespace { |
79 | 87 | assert(m.num_locks() == 4); |
80 | 88 | #endif // _HAS_CXX20 |
81 | 89 | } |
| 90 | + |
| 91 | + // Minimal example inspired by LWG-4301, modified due to missing std::latch before C++20 |
| 92 | + // and generalized to test all overloads of condition_variable{_any}::wait_{for, until}. |
| 93 | + // Idea: Make the main thread wait for a CV with a short timeout and modify it from another thread in the meantime. |
| 94 | + // If the main thread wait times out after a short time, the modification did not influence the ongoing wait. |
| 95 | + template <typename CV> |
| 96 | + void test_timeout_immutable(const int test_number, const int retries_remaining = 5) { |
| 97 | + printf("\ntest %d\n", test_number); |
| 98 | + |
| 99 | + mutex m; |
| 100 | + CV cv; |
| 101 | + unique_lock<mutex> main_lock(m); // Prevent other thread from modifying timeout too early |
| 102 | + |
| 103 | + // Start with very short timeout and let other_thread change it to very large while main thread is waiting |
| 104 | + constexpr auto short_timeout = 1s; |
| 105 | + constexpr auto long_timeout = 10s; |
| 106 | + |
| 107 | + atomic_flag waiting_for_other_thread{}; |
| 108 | + waiting_for_other_thread.test_and_set(); |
| 109 | + |
| 110 | + const auto wait_start = steady_clock::now(); |
| 111 | + auto timeout_duration = short_timeout; |
| 112 | + auto timeout = wait_start + timeout_duration; |
| 113 | + |
| 114 | + const auto set_timeout = [&](const auto new_timeout) { |
| 115 | + timeout_duration = new_timeout; |
| 116 | + timeout = steady_clock::now() + new_timeout; |
| 117 | + }; |
| 118 | + |
| 119 | + thread other_thread([&] { |
| 120 | + printf( |
| 121 | + "thread start after %lld ms\n", duration_cast<milliseconds>(steady_clock::now() - wait_start).count()); |
| 122 | + waiting_for_other_thread.clear(); |
| 123 | + // Immediately blocks since the main thread owns the mutex m. |
| 124 | + lock_guard<mutex> other_lock(m); |
| 125 | + puts("thread lock"); |
| 126 | + |
| 127 | + // If the timeout provided to condition_variable{_any}::wait_{for, until} was mutable, |
| 128 | + // we will get timeout in the main thread after much longer time |
| 129 | + set_timeout(long_timeout); |
| 130 | + puts("thread end"); |
| 131 | + }); |
| 132 | + |
| 133 | + while (waiting_for_other_thread.test_and_set()) { |
| 134 | + this_thread::yield(); // freeze the main thread from proceeding until other thread is started |
| 135 | + } |
| 136 | + printf("main resumed after %lld ms\n", duration_cast<milliseconds>(steady_clock::now() - wait_start).count()); |
| 137 | + set_timeout(short_timeout); |
| 138 | + |
| 139 | + puts("main waiting"); |
| 140 | + const bool cv_wait_timed_out = [&] { |
| 141 | + switch (test_number) { |
| 142 | + case 0: |
| 143 | + return cv.wait_until(main_lock, timeout) == cv_status::timeout; |
| 144 | + |
| 145 | + case 1: |
| 146 | + return cv.wait_until(main_lock, timeout, [] { return false; }) == false; |
| 147 | + |
| 148 | + case 2: |
| 149 | + return cv.wait_for(main_lock, timeout_duration) == cv_status::timeout; |
| 150 | + |
| 151 | + case 3: |
| 152 | + return cv.wait_for(main_lock, timeout_duration, [] { return false; }) == false; |
| 153 | + |
| 154 | +#if _HAS_CXX20 // because of stop_token |
| 155 | + case 4: |
| 156 | + if constexpr (is_same_v<CV, condition_variable_any>) { |
| 157 | + stop_source source; |
| 158 | + return cv.wait_until(main_lock, source.get_token(), timeout, [] { return false; }) == false; |
| 159 | + } else { |
| 160 | + assert(false); // test not supported for std::condition_variable |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + case 5: |
| 165 | + if constexpr (is_same_v<CV, condition_variable_any>) { |
| 166 | + stop_source source; |
| 167 | + return cv.wait_for(main_lock, source.get_token(), timeout_duration, [] { return false; }) == false; |
| 168 | + } else { |
| 169 | + assert(false); // test not supported for std::condition_variable |
| 170 | + return false; |
| 171 | + } |
| 172 | +#endif // _HAS_CXX20 |
| 173 | + |
| 174 | + default: |
| 175 | + assert(false); |
| 176 | + return false; |
| 177 | + } |
| 178 | + }(); |
| 179 | + |
| 180 | + const auto elapsed = steady_clock::now() - wait_start; |
| 181 | + |
| 182 | + if (!cv_wait_timed_out) { |
| 183 | + if (retries_remaining > 0) { |
| 184 | + printf("unexpected wakeup after %lld ms, retry %d...\n", duration_cast<milliseconds>(elapsed).count(), |
| 185 | + retries_remaining); |
| 186 | + test_timeout_immutable<CV>(test_number, retries_remaining - 1); // recurse to try the test again |
| 187 | + } else { |
| 188 | + puts("Too many unexpected wakeups"); |
| 189 | + assert(false); |
| 190 | + } |
| 191 | + } else { |
| 192 | + assert(elapsed < long_timeout / 2); |
| 193 | + printf("wait end after %lld ms\n", duration_cast<milliseconds>(elapsed).count()); |
| 194 | + } |
| 195 | + |
| 196 | + // Make sure the child thread has indeed finished (so the next join does not block) |
| 197 | + assert(timeout_duration == long_timeout); |
| 198 | + other_thread.join(); |
| 199 | + } |
82 | 200 | } // unnamed namespace |
83 | 201 |
|
84 | 202 | int main() { |
85 | 203 | test_condition_variable_any(); |
86 | 204 | test_condition_variable_any_already_timed_out(); |
| 205 | + |
| 206 | + puts("condition_variable"); |
| 207 | + test_timeout_immutable<condition_variable>(0); |
| 208 | + test_timeout_immutable<condition_variable>(1); |
| 209 | + test_timeout_immutable<condition_variable>(2); |
| 210 | + test_timeout_immutable<condition_variable>(3); |
| 211 | + |
| 212 | + puts("condition_variable_any"); |
| 213 | + test_timeout_immutable<condition_variable_any>(0); |
| 214 | + test_timeout_immutable<condition_variable_any>(1); |
| 215 | + test_timeout_immutable<condition_variable_any>(2); |
| 216 | + test_timeout_immutable<condition_variable_any>(3); |
| 217 | +#if _HAS_CXX20 |
| 218 | + test_timeout_immutable<condition_variable_any>(4); |
| 219 | + test_timeout_immutable<condition_variable_any>(5); |
| 220 | +#endif // _HAS_CXX20 |
87 | 221 | } |
0 commit comments