| 1 | // <condition_variable> -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file include/condition_variable |
| 26 | * This is a Standard C++ Library header. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _GLIBCXX_CONDITION_VARIABLE |
| 30 | #define _GLIBCXX_CONDITION_VARIABLE 1 |
| 31 | |
| 32 | #ifdef _GLIBCXX_SYSHDR |
| 33 | #pragma GCC system_header |
| 34 | #endif |
| 35 | |
| 36 | #include <bits/requires_hosted.h> // threading primitive |
| 37 | |
| 38 | #if __cplusplus < 201103L |
| 39 | # include <bits/c++0x_warning.h> |
| 40 | #else |
| 41 | |
| 42 | #include <bits/chrono.h> |
| 43 | #include <bits/error_constants.h> |
| 44 | #include <bits/std_mutex.h> |
| 45 | #include <bits/unique_lock.h> |
| 46 | #include <bits/alloc_traits.h> |
| 47 | #include <bits/shared_ptr.h> |
| 48 | #include <bits/cxxabi_forced.h> |
| 49 | |
| 50 | #if __cplusplus > 201703L |
| 51 | # include <stop_token> |
| 52 | #endif |
| 53 | |
| 54 | #if defined(_GLIBCXX_HAS_GTHREADS) |
| 55 | |
| 56 | namespace std _GLIBCXX_VISIBILITY(default) |
| 57 | { |
| 58 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 59 | |
| 60 | /** |
| 61 | * @defgroup condition_variables Condition Variables |
| 62 | * @ingroup concurrency |
| 63 | * |
| 64 | * Classes for condition_variable support. |
| 65 | * @{ |
| 66 | */ |
| 67 | |
| 68 | /// cv_status |
| 69 | enum class cv_status { no_timeout, timeout }; |
| 70 | |
| 71 | /// condition_variable |
| 72 | class condition_variable |
| 73 | { |
| 74 | using steady_clock = chrono::steady_clock; |
| 75 | using system_clock = chrono::system_clock; |
| 76 | #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT |
| 77 | using __clock_t = steady_clock; |
| 78 | #else |
| 79 | using __clock_t = system_clock; |
| 80 | #endif |
| 81 | |
| 82 | __condvar _M_cond; |
| 83 | |
| 84 | public: |
| 85 | typedef __gthread_cond_t* native_handle_type; |
| 86 | |
| 87 | condition_variable() noexcept; |
| 88 | ~condition_variable() noexcept; |
| 89 | |
| 90 | condition_variable(const condition_variable&) = delete; |
| 91 | condition_variable& operator=(const condition_variable&) = delete; |
| 92 | |
| 93 | void |
| 94 | notify_one() noexcept; |
| 95 | |
| 96 | void |
| 97 | notify_all() noexcept; |
| 98 | |
| 99 | void |
| 100 | wait(unique_lock<mutex>& __lock); |
| 101 | |
| 102 | template<typename _Predicate> |
| 103 | void |
| 104 | wait(unique_lock<mutex>& __lock, _Predicate __p) |
| 105 | { |
| 106 | while (!__p()) |
| 107 | wait(__lock); |
| 108 | } |
| 109 | |
| 110 | #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT |
| 111 | template<typename _Duration> |
| 112 | cv_status |
| 113 | wait_until(unique_lock<mutex>& __lock, |
| 114 | const chrono::time_point<steady_clock, _Duration>& __atime) |
| 115 | { return __wait_until_impl(__lock, __atime); } |
| 116 | #endif |
| 117 | |
| 118 | template<typename _Duration> |
| 119 | cv_status |
| 120 | wait_until(unique_lock<mutex>& __lock, |
| 121 | const chrono::time_point<system_clock, _Duration>& __atime) |
| 122 | { return __wait_until_impl(__lock, __atime); } |
| 123 | |
| 124 | template<typename _Clock, typename _Duration> |
| 125 | cv_status |
| 126 | wait_until(unique_lock<mutex>& __lock, |
| 127 | const chrono::time_point<_Clock, _Duration>& __atime) |
| 128 | { |
| 129 | #if __cplusplus > 201703L |
| 130 | static_assert(chrono::is_clock_v<_Clock>); |
| 131 | #endif |
| 132 | using __s_dur = typename __clock_t::duration; |
| 133 | const typename _Clock::time_point __c_entry = _Clock::now(); |
| 134 | const __clock_t::time_point __s_entry = __clock_t::now(); |
| 135 | const auto __delta = __atime - __c_entry; |
| 136 | const auto __s_atime = __s_entry + |
| 137 | chrono::__detail::ceil<__s_dur>(__delta); |
| 138 | |
| 139 | if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout) |
| 140 | return cv_status::no_timeout; |
| 141 | // We got a timeout when measured against __clock_t but |
| 142 | // we need to check against the caller-supplied clock |
| 143 | // to tell whether we should return a timeout. |
| 144 | if (_Clock::now() < __atime) |
| 145 | return cv_status::no_timeout; |
| 146 | return cv_status::timeout; |
| 147 | } |
| 148 | |
| 149 | template<typename _Clock, typename _Duration, typename _Predicate> |
| 150 | bool |
| 151 | wait_until(unique_lock<mutex>& __lock, |
| 152 | const chrono::time_point<_Clock, _Duration>& __atime, |
| 153 | _Predicate __p) |
| 154 | { |
| 155 | while (!__p()) |
| 156 | if (wait_until(__lock, __atime) == cv_status::timeout) |
| 157 | return __p(); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | template<typename _Rep, typename _Period> |
| 162 | cv_status |
| 163 | wait_for(unique_lock<mutex>& __lock, |
| 164 | const chrono::duration<_Rep, _Period>& __rtime) |
| 165 | { |
| 166 | using __dur = typename steady_clock::duration; |
| 167 | return wait_until(__lock, |
| 168 | steady_clock::now() + |
| 169 | chrono::__detail::ceil<__dur>(__rtime)); |
| 170 | } |
| 171 | |
| 172 | template<typename _Rep, typename _Period, typename _Predicate> |
| 173 | bool |
| 174 | wait_for(unique_lock<mutex>& __lock, |
| 175 | const chrono::duration<_Rep, _Period>& __rtime, |
| 176 | _Predicate __p) |
| 177 | { |
| 178 | using __dur = typename steady_clock::duration; |
| 179 | return wait_until(__lock, |
| 180 | steady_clock::now() + |
| 181 | chrono::__detail::ceil<__dur>(__rtime), |
| 182 | std::move(__p)); |
| 183 | } |
| 184 | |
| 185 | native_handle_type |
| 186 | native_handle() |
| 187 | { return _M_cond.native_handle(); } |
| 188 | |
| 189 | private: |
| 190 | #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT |
| 191 | template<typename _Dur> |
| 192 | cv_status |
| 193 | __wait_until_impl(unique_lock<mutex>& __lock, |
| 194 | const chrono::time_point<steady_clock, _Dur>& __atime) |
| 195 | { |
| 196 | auto __s = chrono::time_point_cast<chrono::seconds>(__atime); |
| 197 | auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s); |
| 198 | |
| 199 | __gthread_time_t __ts = |
| 200 | { |
| 201 | .tv_sec: static_cast<std::time_t>(__s.time_since_epoch().count()), |
| 202 | .tv_nsec: static_cast<long>(__ns.count()) |
| 203 | }; |
| 204 | |
| 205 | _M_cond.wait_until(m&: *__lock.mutex(), CLOCK_MONOTONIC, abs_time&: __ts); |
| 206 | |
| 207 | return (steady_clock::now() < __atime |
| 208 | ? cv_status::no_timeout : cv_status::timeout); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | template<typename _Dur> |
| 213 | cv_status |
| 214 | __wait_until_impl(unique_lock<mutex>& __lock, |
| 215 | const chrono::time_point<system_clock, _Dur>& __atime) |
| 216 | { |
| 217 | auto __s = chrono::time_point_cast<chrono::seconds>(__atime); |
| 218 | auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s); |
| 219 | |
| 220 | __gthread_time_t __ts = |
| 221 | { |
| 222 | .tv_sec: static_cast<std::time_t>(__s.time_since_epoch().count()), |
| 223 | .tv_nsec: static_cast<long>(__ns.count()) |
| 224 | }; |
| 225 | |
| 226 | _M_cond.wait_until(m&: *__lock.mutex(), abs_time&: __ts); |
| 227 | |
| 228 | return (system_clock::now() < __atime |
| 229 | ? cv_status::no_timeout : cv_status::timeout); |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | void |
| 234 | notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>); |
| 235 | |
| 236 | struct __at_thread_exit_elt |
| 237 | { |
| 238 | __at_thread_exit_elt* _M_next; |
| 239 | void (*_M_cb)(void*); |
| 240 | }; |
| 241 | |
| 242 | _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) |
| 243 | |
| 244 | /// condition_variable_any |
| 245 | // Like above, but mutex is not required to have try_lock. |
| 246 | class condition_variable_any |
| 247 | { |
| 248 | #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT |
| 249 | using __clock_t = chrono::steady_clock; |
| 250 | #else |
| 251 | using __clock_t = chrono::system_clock; |
| 252 | #endif |
| 253 | condition_variable _M_cond; |
| 254 | shared_ptr<mutex> _M_mutex; |
| 255 | |
| 256 | // scoped unlock - unlocks in ctor, re-locks in dtor |
| 257 | template<typename _Lock> |
| 258 | struct _Unlock |
| 259 | { |
| 260 | explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); } |
| 261 | |
| 262 | #pragma GCC diagnostic push |
| 263 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 264 | ~_Unlock() noexcept(false) |
| 265 | { |
| 266 | if (uncaught_exception()) |
| 267 | { |
| 268 | __try |
| 269 | { _M_lock.lock(); } |
| 270 | __catch(const __cxxabiv1::__forced_unwind&) |
| 271 | { __throw_exception_again; } |
| 272 | __catch(...) |
| 273 | { } |
| 274 | } |
| 275 | else |
| 276 | _M_lock.lock(); |
| 277 | } |
| 278 | #pragma GCC diagnostic pop |
| 279 | |
| 280 | _Unlock(const _Unlock&) = delete; |
| 281 | _Unlock& operator=(const _Unlock&) = delete; |
| 282 | |
| 283 | _Lock& _M_lock; |
| 284 | }; |
| 285 | |
| 286 | public: |
| 287 | condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { } |
| 288 | ~condition_variable_any() = default; |
| 289 | |
| 290 | condition_variable_any(const condition_variable_any&) = delete; |
| 291 | condition_variable_any& operator=(const condition_variable_any&) = delete; |
| 292 | |
| 293 | void |
| 294 | notify_one() noexcept |
| 295 | { |
| 296 | lock_guard<mutex> __lock(*_M_mutex); |
| 297 | _M_cond.notify_one(); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | notify_all() noexcept |
| 302 | { |
| 303 | lock_guard<mutex> __lock(*_M_mutex); |
| 304 | _M_cond.notify_all(); |
| 305 | } |
| 306 | |
| 307 | template<typename _Lock> |
| 308 | void |
| 309 | wait(_Lock& __lock) |
| 310 | { |
| 311 | shared_ptr<mutex> __mutex = _M_mutex; |
| 312 | unique_lock<mutex> __my_lock(*__mutex); |
| 313 | _Unlock<_Lock> __unlock(__lock); |
| 314 | // *__mutex must be unlocked before re-locking __lock so move |
| 315 | // ownership of *__mutex lock to an object with shorter lifetime. |
| 316 | unique_lock<mutex> __my_lock2(std::move(__my_lock)); |
| 317 | _M_cond.wait(lock&: __my_lock2); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | template<typename _Lock, typename _Predicate> |
| 322 | void |
| 323 | wait(_Lock& __lock, _Predicate __p) |
| 324 | { |
| 325 | while (!__p()) |
| 326 | wait(__lock); |
| 327 | } |
| 328 | |
| 329 | template<typename _Lock, typename _Clock, typename _Duration> |
| 330 | cv_status |
| 331 | wait_until(_Lock& __lock, |
| 332 | const chrono::time_point<_Clock, _Duration>& __atime) |
| 333 | { |
| 334 | shared_ptr<mutex> __mutex = _M_mutex; |
| 335 | unique_lock<mutex> __my_lock(*__mutex); |
| 336 | _Unlock<_Lock> __unlock(__lock); |
| 337 | // *__mutex must be unlocked before re-locking __lock so move |
| 338 | // ownership of *__mutex lock to an object with shorter lifetime. |
| 339 | unique_lock<mutex> __my_lock2(std::move(__my_lock)); |
| 340 | return _M_cond.wait_until(__my_lock2, __atime); |
| 341 | } |
| 342 | |
| 343 | template<typename _Lock, typename _Clock, |
| 344 | typename _Duration, typename _Predicate> |
| 345 | bool |
| 346 | wait_until(_Lock& __lock, |
| 347 | const chrono::time_point<_Clock, _Duration>& __atime, |
| 348 | _Predicate __p) |
| 349 | { |
| 350 | while (!__p()) |
| 351 | if (wait_until(__lock, __atime) == cv_status::timeout) |
| 352 | return __p(); |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | template<typename _Lock, typename _Rep, typename _Period> |
| 357 | cv_status |
| 358 | wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime) |
| 359 | { return wait_until(__lock, __clock_t::now() + __rtime); } |
| 360 | |
| 361 | template<typename _Lock, typename _Rep, |
| 362 | typename _Period, typename _Predicate> |
| 363 | bool |
| 364 | wait_for(_Lock& __lock, |
| 365 | const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p) |
| 366 | { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } |
| 367 | |
| 368 | #ifdef __glibcxx_jthread |
| 369 | template <class _Lock, class _Predicate> |
| 370 | bool wait(_Lock& __lock, |
| 371 | stop_token __stoken, |
| 372 | _Predicate __p) |
| 373 | { |
| 374 | if (__stoken.stop_requested()) |
| 375 | { |
| 376 | return __p(); |
| 377 | } |
| 378 | |
| 379 | std::stop_callback __cb(__stoken, [this] { notify_all(); }); |
| 380 | shared_ptr<mutex> __mutex = _M_mutex; |
| 381 | while (!__p()) |
| 382 | { |
| 383 | unique_lock<mutex> __my_lock(*__mutex); |
| 384 | if (__stoken.stop_requested()) |
| 385 | { |
| 386 | return false; |
| 387 | } |
| 388 | // *__mutex must be unlocked before re-locking __lock so move |
| 389 | // ownership of *__mutex lock to an object with shorter lifetime. |
| 390 | _Unlock<_Lock> __unlock(__lock); |
| 391 | unique_lock<mutex> __my_lock2(std::move(__my_lock)); |
| 392 | _M_cond.wait(lock&: __my_lock2); |
| 393 | } |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | template <class _Lock, class _Clock, class _Duration, class _Predicate> |
| 398 | bool wait_until(_Lock& __lock, |
| 399 | stop_token __stoken, |
| 400 | const chrono::time_point<_Clock, _Duration>& __abs_time, |
| 401 | _Predicate __p) |
| 402 | { |
| 403 | if (__stoken.stop_requested()) |
| 404 | { |
| 405 | return __p(); |
| 406 | } |
| 407 | |
| 408 | std::stop_callback __cb(__stoken, [this] { notify_all(); }); |
| 409 | shared_ptr<mutex> __mutex = _M_mutex; |
| 410 | while (!__p()) |
| 411 | { |
| 412 | bool __stop; |
| 413 | { |
| 414 | unique_lock<mutex> __my_lock(*__mutex); |
| 415 | if (__stoken.stop_requested()) |
| 416 | { |
| 417 | return false; |
| 418 | } |
| 419 | _Unlock<_Lock> __u(__lock); |
| 420 | unique_lock<mutex> __my_lock2(std::move(__my_lock)); |
| 421 | const auto __status = _M_cond.wait_until(__my_lock2, __abs_time); |
| 422 | __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested(); |
| 423 | } |
| 424 | if (__stop) |
| 425 | { |
| 426 | return __p(); |
| 427 | } |
| 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | template <class _Lock, class _Rep, class _Period, class _Predicate> |
| 433 | bool wait_for(_Lock& __lock, |
| 434 | stop_token __stoken, |
| 435 | const chrono::duration<_Rep, _Period>& __rel_time, |
| 436 | _Predicate __p) |
| 437 | { |
| 438 | auto __abst = std::chrono::steady_clock::now() + __rel_time; |
| 439 | return wait_until(__lock, |
| 440 | std::move(__stoken), |
| 441 | __abst, |
| 442 | std::move(__p)); |
| 443 | } |
| 444 | #endif |
| 445 | }; |
| 446 | |
| 447 | _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) |
| 448 | |
| 449 | /// @} group condition_variables |
| 450 | _GLIBCXX_END_NAMESPACE_VERSION |
| 451 | } // namespace |
| 452 | |
| 453 | #endif // _GLIBCXX_HAS_GTHREADS |
| 454 | #endif // C++11 |
| 455 | #endif // _GLIBCXX_CONDITION_VARIABLE |
| 456 | |