1 #ifndef BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
2 #define BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // (C) Copyright 2007-8 Anthony Williams
7 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
9 #include <boost/assert.hpp>
10 #include <boost/throw_exception.hpp>
12 #include <boost/thread/cv_status.hpp>
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/lock_types.hpp>
15 #include <boost/thread/thread_time.hpp>
16 #include <boost/thread/pthread/timespec.hpp>
17 #if defined BOOST_THREAD_USES_DATETIME
18 #include <boost/thread/xtime.hpp>
20 #ifdef BOOST_THREAD_USES_CHRONO
21 #include <boost/chrono/system_clocks.hpp>
22 #include <boost/chrono/ceil.hpp>
24 #include <boost/thread/detail/delete.hpp>
25 #include <boost/date_time/posix_time/posix_time_duration.hpp>
27 #include <boost/config/abi_prefix.hpp>
32 inline int monotonic_pthread_cond_init(pthread_cond_t& cond) {
34 #ifdef BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
35 pthread_condattr_t attr;
36 int res = pthread_condattr_init(&attr);
41 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
42 res=pthread_cond_init(&cond,&attr);
43 pthread_condattr_destroy(&attr);
46 return pthread_cond_init(&cond,NULL);
52 class condition_variable
55 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
56 pthread_mutex_t internal_mutex;
61 //private: // used by boost::thread::try_join_until
63 inline bool do_wait_until(
64 unique_lock<mutex>& lock,
65 struct timespec const &timeout);
68 unique_lock<mutex>& lock,
69 struct timespec const &timeout)
71 return do_wait_until(lock, boost::detail::timespec_plus(timeout, boost::detail::timespec_now()));
75 BOOST_THREAD_NO_COPYABLE(condition_variable)
79 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
80 res=pthread_mutex_init(&internal_mutex,NULL);
83 boost::throw_exception(thread_resource_error(res, "boost::condition_variable::condition_variable() constructor failed in pthread_mutex_init"));
86 res = detail::monotonic_pthread_cond_init(cond);
89 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
90 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
92 boost::throw_exception(thread_resource_error(res, "boost::condition_variable::condition_variable() constructor failed in detail::monotonic_pthread_cond_init"));
98 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
100 ret = pthread_mutex_destroy(&internal_mutex);
101 } while (ret == EINTR);
105 ret = pthread_cond_destroy(&cond);
106 } while (ret == EINTR);
110 void wait(unique_lock<mutex>& m);
112 template<typename predicate_type>
113 void wait(unique_lock<mutex>& m,predicate_type pred)
115 while(!pred()) wait(m);
118 #if defined BOOST_THREAD_USES_DATETIME
119 inline bool timed_wait(
120 unique_lock<mutex>& m,
121 boost::system_time const& abs_time)
123 #if defined BOOST_THREAD_WAIT_BUG
124 struct timespec const timeout=detail::to_timespec(abs_time + BOOST_THREAD_WAIT_BUG);
125 return do_wait_until(m, timeout);
127 struct timespec const timeout=detail::to_timespec(abs_time);
128 return do_wait_until(m, timeout);
132 unique_lock<mutex>& m,
133 xtime const& abs_time)
135 return timed_wait(m,system_time(abs_time));
138 template<typename duration_type>
140 unique_lock<mutex>& m,
141 duration_type const& wait_duration)
143 if (wait_duration.is_pos_infinity())
145 wait(m); // or do_wait(m,detail::timeout::sentinel());
148 if (wait_duration.is_special())
152 return timed_wait(m,get_system_time()+wait_duration);
155 template<typename predicate_type>
157 unique_lock<mutex>& m,
158 boost::system_time const& abs_time,predicate_type pred)
162 if(!timed_wait(m, abs_time))
168 template<typename predicate_type>
170 unique_lock<mutex>& m,
171 xtime const& abs_time,predicate_type pred)
173 return timed_wait(m,system_time(abs_time),pred);
176 template<typename duration_type,typename predicate_type>
178 unique_lock<mutex>& m,
179 duration_type const& wait_duration,predicate_type pred)
181 if (wait_duration.is_pos_infinity())
185 wait(m); // or do_wait(m,detail::timeout::sentinel());
189 if (wait_duration.is_special())
193 return timed_wait(m,get_system_time()+wait_duration,pred);
197 #ifndef BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
199 #ifdef BOOST_THREAD_USES_CHRONO
201 template <class Duration>
204 unique_lock<mutex>& lock,
205 const chrono::time_point<chrono::system_clock, Duration>& t)
207 using namespace chrono;
208 typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
210 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
211 return system_clock::now() < t ? cv_status::no_timeout :
215 template <class Clock, class Duration>
218 unique_lock<mutex>& lock,
219 const chrono::time_point<Clock, Duration>& t)
221 using namespace chrono;
222 system_clock::time_point s_now = system_clock::now();
223 typename Clock::time_point c_now = Clock::now();
224 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
225 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
230 template <class Rep, class Period>
233 unique_lock<mutex>& lock,
234 const chrono::duration<Rep, Period>& d)
236 using namespace chrono;
237 system_clock::time_point s_now = system_clock::now();
238 steady_clock::time_point c_now = steady_clock::now();
239 wait_until(lock, s_now + ceil<nanoseconds>(d));
240 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
245 inline cv_status wait_until(
246 unique_lock<mutex>& lk,
247 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
249 using namespace chrono;
250 nanoseconds d = tp.time_since_epoch();
251 timespec ts = boost::detail::to_timespec(d);
252 if (do_wait_until(lk, ts)) return cv_status::no_timeout;
253 else return cv_status::timeout;
257 #else // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
258 #ifdef BOOST_THREAD_USES_CHRONO
260 template <class Duration>
263 unique_lock<mutex>& lock,
264 const chrono::time_point<chrono::steady_clock, Duration>& t)
266 using namespace chrono;
267 typedef time_point<steady_clock, nanoseconds> nano_sys_tmpt;
269 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
270 return steady_clock::now() < t ? cv_status::no_timeout :
274 template <class Clock, class Duration>
277 unique_lock<mutex>& lock,
278 const chrono::time_point<Clock, Duration>& t)
280 using namespace chrono;
281 steady_clock::time_point s_now = steady_clock::now();
282 typename Clock::time_point c_now = Clock::now();
283 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
284 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
287 template <class Rep, class Period>
290 unique_lock<mutex>& lock,
291 const chrono::duration<Rep, Period>& d)
293 using namespace chrono;
294 steady_clock::time_point c_now = steady_clock::now();
295 wait_until(lock, c_now + ceil<nanoseconds>(d));
296 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
300 inline cv_status wait_until(
301 unique_lock<mutex>& lk,
302 chrono::time_point<chrono::steady_clock, chrono::nanoseconds> tp)
304 using namespace chrono;
305 nanoseconds d = tp.time_since_epoch();
306 timespec ts = boost::detail::to_timespec(d);
307 if (do_wait_until(lk, ts)) return cv_status::no_timeout;
308 else return cv_status::timeout;
312 #endif // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
314 #ifdef BOOST_THREAD_USES_CHRONO
315 template <class Clock, class Duration, class Predicate>
318 unique_lock<mutex>& lock,
319 const chrono::time_point<Clock, Duration>& t,
324 if (wait_until(lock, t) == cv_status::timeout)
330 template <class Rep, class Period, class Predicate>
333 unique_lock<mutex>& lock,
334 const chrono::duration<Rep, Period>& d,
337 return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
341 #define BOOST_THREAD_DEFINES_CONDITION_VARIABLE_NATIVE_HANDLE
342 typedef pthread_cond_t* native_handle_type;
343 native_handle_type native_handle()
348 void notify_one() BOOST_NOEXCEPT;
349 void notify_all() BOOST_NOEXCEPT;
354 BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
359 #include <boost/config/abi_suffix.hpp>