29 #ifndef _GLIBCXX_CONDITION_VARIABLE
30 #define _GLIBCXX_CONDITION_VARIABLE 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
42 #include <bits/shared_ptr.h>
45 #if __cplusplus > 201703L
49 #if defined(_GLIBCXX_HAS_GTHREADS)
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
80 typedef __gthread_cond_t* native_handle_type;
89 notify_one() noexcept;
92 notify_all() noexcept;
97 template<
typename _Predicate>
105 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
106 template<
typename _Duration>
110 {
return __wait_until_impl(__lock, __atime); }
113 template<
typename _Duration>
117 {
return __wait_until_impl(__lock, __atime); }
119 template<
typename _Clock,
typename _Duration>
124 #if __cplusplus > 201703L
125 static_assert(chrono::is_clock_v<_Clock>);
127 using __s_dur =
typename __clock_t::duration;
128 const typename _Clock::time_point __c_entry = _Clock::now();
129 const __clock_t::time_point __s_entry = __clock_t::now();
130 const auto __delta = __atime - __c_entry;
131 const auto __s_atime = __s_entry +
132 chrono::__detail::ceil<__s_dur>(__delta);
134 if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
135 return cv_status::no_timeout;
139 if (_Clock::now() < __atime)
140 return cv_status::no_timeout;
141 return cv_status::timeout;
144 template<
typename _Clock,
typename _Duration,
typename _Predicate>
151 if (wait_until(__lock, __atime) == cv_status::timeout)
156 template<
typename _Rep,
typename _Period>
161 using __dur =
typename steady_clock::duration;
162 return wait_until(__lock,
163 steady_clock::now() +
164 chrono::__detail::ceil<__dur>(__rtime));
167 template<
typename _Rep,
typename _Period,
typename _Predicate>
173 using __dur =
typename steady_clock::duration;
174 return wait_until(__lock,
175 steady_clock::now() +
176 chrono::__detail::ceil<__dur>(__rtime),
182 {
return _M_cond.native_handle(); }
185 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
186 template<
typename _Dur>
191 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
192 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
194 __gthread_time_t __ts =
196 static_cast<std::time_t
>(__s.time_since_epoch().count()),
197 static_cast<long>(__ns.count())
200 _M_cond.wait_until(*__lock.mutex(), CLOCK_MONOTONIC, __ts);
202 return (steady_clock::now() < __atime
203 ? cv_status::no_timeout : cv_status::timeout);
207 template<
typename _Dur>
212 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
213 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
215 __gthread_time_t __ts =
217 static_cast<std::time_t
>(__s.time_since_epoch().count()),
218 static_cast<long>(__ns.count())
221 _M_cond.wait_until(*__lock.mutex(), __ts);
223 return (system_clock::now() < __atime
224 ? cv_status::no_timeout : cv_status::timeout);
231 struct __at_thread_exit_elt
233 __at_thread_exit_elt* _M_next;
234 void (*_M_cb)(
void*);
237 inline namespace _V2 {
243 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
252 template<
typename _Lock>
255 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
257 #pragma GCC diagnostic push
258 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
259 ~_Unlock() noexcept(
false)
266 { __throw_exception_again; }
273 #pragma GCC diagnostic pop
275 _Unlock(
const _Unlock&) =
delete;
276 _Unlock& operator=(
const _Unlock&) =
delete;
289 notify_one() noexcept
292 _M_cond.notify_one();
296 notify_all() noexcept
299 _M_cond.notify_all();
302 template<
typename _Lock>
308 _Unlock<_Lock> __unlock(__lock);
312 _M_cond.wait(__my_lock2);
316 template<
typename _Lock,
typename _Predicate>
318 wait(_Lock& __lock, _Predicate __p)
324 template<
typename _Lock,
typename _Clock,
typename _Duration>
326 wait_until(_Lock& __lock,
331 _Unlock<_Lock> __unlock(__lock);
335 return _M_cond.wait_until(__my_lock2, __atime);
338 template<
typename _Lock,
typename _Clock,
339 typename _Duration,
typename _Predicate>
341 wait_until(_Lock& __lock,
346 if (wait_until(__lock, __atime) == cv_status::timeout)
351 template<
typename _Lock,
typename _Rep,
typename _Period>
354 {
return wait_until(__lock, __clock_t::now() + __rtime); }
356 template<
typename _Lock,
typename _Rep,
357 typename _Period,
typename _Predicate>
359 wait_for(_Lock& __lock,
361 {
return wait_until(__lock, __clock_t::now() + __rtime,
std::move(__p)); }
363 #ifdef __cpp_lib_jthread
364 template <
class _Lock,
class _Predicate>
365 bool wait(_Lock& __lock,
369 if (__stoken.stop_requested())
379 if (__stoken.stop_requested())
385 _Unlock<_Lock> __unlock(__lock);
387 _M_cond.wait(__my_lock2);
392 template <
class _Lock,
class _Clock,
class _Duration,
class _Predicate>
393 bool wait_until(_Lock& __lock,
398 if (__stoken.stop_requested())
410 if (__stoken.stop_requested())
414 _Unlock<_Lock> __u(__lock);
416 const auto __status = _M_cond.wait_until(__my_lock2, __abs_time);
417 __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested();
427 template <
class _Lock,
class _Rep,
class _Period,
class _Predicate>
428 bool wait_for(_Lock& __lock,
433 auto __abst = std::chrono::steady_clock::now() + __rel_time;
434 return wait_until(__lock,
445 _GLIBCXX_END_NAMESPACE_VERSION
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
bool uncaught_exception() noexcept
ISO C++ entities toplevel namespace is std.
Allow testing whether a stop request has been made on a stop_source.
A wrapper for callbacks to be run when a stop request is made.
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock
Thrown as part of forced unwinding.
A smart pointer with reference-counted copy semantics.
A simple scoped lock type.
A movable scoped lock type.