1 #ifndef BOOST_THREAD_CONDITION_VARIABLE_PTHREAD_HPP
2 #define BOOST_THREAD_CONDITION_VARIABLE_PTHREAD_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-10 Anthony Williams
7 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
9 #include <boost/thread/pthread/timespec.hpp>
10 #include <boost/thread/pthread/pthread_mutex_scoped_lock.hpp>
11 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
12 #include <boost/thread/pthread/thread_data.hpp>
14 #include <boost/thread/pthread/condition_variable_fwd.hpp>
15 #ifdef BOOST_THREAD_USES_CHRONO
16 #include <boost/chrono/system_clocks.hpp>
17 #include <boost/chrono/ceil.hpp>
19 #include <boost/thread/detail/delete.hpp>
21 #include <boost/config/abi_prefix.hpp>
25 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
28 void BOOST_THREAD_DECL interruption_point();
32 namespace thread_cv_detail
34 template<typename MutexType>
43 void activate(MutexType& m_)
58 inline void condition_variable::wait(unique_lock<mutex>& m)
60 #if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
63 boost::throw_exception(condition_error(-1, "boost::condition_variable::wait() failed precondition mutex not owned"));
68 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
69 thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
70 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
71 pthread_mutex_t* the_mutex = &internal_mutex;
74 pthread_mutex_t* the_mutex = m.mutex()->native_handle();
76 res = pthread_cond_wait(&cond,the_mutex);
78 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
79 this_thread::interruption_point();
81 if(res && res != EINTR)
83 boost::throw_exception(condition_error(res, "boost::condition_variable::wait failed in pthread_cond_wait"));
87 inline bool condition_variable::do_wait_until(
88 unique_lock<mutex>& m,
89 struct timespec const &timeout)
91 #if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
94 boost::throw_exception(condition_error(EPERM, "boost::condition_variable::do_wait_until() failed precondition mutex not owned"));
99 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
100 thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
101 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
102 pthread_mutex_t* the_mutex = &internal_mutex;
105 pthread_mutex_t* the_mutex = m.mutex()->native_handle();
107 cond_res=pthread_cond_timedwait(&cond,the_mutex,&timeout);
109 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
110 this_thread::interruption_point();
112 if(cond_res==ETIMEDOUT)
118 boost::throw_exception(condition_error(cond_res, "boost::condition_variable::do_wait_until failed in pthread_cond_timedwait"));
123 inline void condition_variable::notify_one() BOOST_NOEXCEPT
125 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
126 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
128 BOOST_VERIFY(!pthread_cond_signal(&cond));
131 inline void condition_variable::notify_all() BOOST_NOEXCEPT
133 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
134 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
136 BOOST_VERIFY(!pthread_cond_broadcast(&cond));
139 class condition_variable_any
141 pthread_mutex_t internal_mutex;
145 BOOST_THREAD_NO_COPYABLE(condition_variable_any)
146 condition_variable_any()
148 int const res=pthread_mutex_init(&internal_mutex,NULL);
151 boost::throw_exception(thread_resource_error(res, "boost::condition_variable_any::condition_variable_any() failed in pthread_mutex_init"));
153 int const res2 = detail::monotonic_pthread_cond_init(cond);
156 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
157 boost::throw_exception(thread_resource_error(res2, "boost::condition_variable_any::condition_variable_any() failed in detail::monotonic_pthread_cond_init"));
160 ~condition_variable_any()
162 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
163 BOOST_VERIFY(!pthread_cond_destroy(&cond));
166 template<typename lock_type>
167 void wait(lock_type& m)
171 thread_cv_detail::lock_on_exit<lock_type> guard;
172 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
173 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
175 boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
178 res=pthread_cond_wait(&cond,&internal_mutex);
180 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
181 this_thread::interruption_point();
185 boost::throw_exception(condition_error(res, "boost::condition_variable_any::wait() failed in pthread_cond_wait"));
189 template<typename lock_type,typename predicate_type>
190 void wait(lock_type& m,predicate_type pred)
192 while(!pred()) wait(m);
195 #if defined BOOST_THREAD_USES_DATETIME
196 template<typename lock_type>
197 bool timed_wait(lock_type& m,boost::system_time const& abs_time)
199 struct timespec const timeout=detail::to_timespec(abs_time);
200 return do_wait_until(m, timeout);
202 template<typename lock_type>
203 bool timed_wait(lock_type& m,xtime const& abs_time)
205 return timed_wait(m,system_time(abs_time));
208 template<typename lock_type,typename duration_type>
209 bool timed_wait(lock_type& m,duration_type const& wait_duration)
211 return timed_wait(m,get_system_time()+wait_duration);
214 template<typename lock_type,typename predicate_type>
215 bool timed_wait(lock_type& m,boost::system_time const& abs_time, predicate_type pred)
219 if(!timed_wait(m, abs_time))
225 template<typename lock_type,typename predicate_type>
226 bool timed_wait(lock_type& m,xtime const& abs_time, predicate_type pred)
228 return timed_wait(m,system_time(abs_time),pred);
231 template<typename lock_type,typename duration_type,typename predicate_type>
232 bool timed_wait(lock_type& m,duration_type const& wait_duration,predicate_type pred)
234 return timed_wait(m,get_system_time()+wait_duration,pred);
237 #ifndef BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
239 #ifdef BOOST_THREAD_USES_CHRONO
240 template <class lock_type,class Duration>
244 const chrono::time_point<chrono::system_clock, Duration>& t)
246 using namespace chrono;
247 typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
249 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
250 return system_clock::now() < t ? cv_status::no_timeout :
254 template <class lock_type, class Clock, class Duration>
258 const chrono::time_point<Clock, Duration>& t)
260 using namespace chrono;
261 system_clock::time_point s_now = system_clock::now();
262 typename Clock::time_point c_now = Clock::now();
263 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
264 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
267 template <class lock_type, class Rep, class Period>
271 const chrono::duration<Rep, Period>& d)
273 using namespace chrono;
274 system_clock::time_point s_now = system_clock::now();
275 steady_clock::time_point c_now = steady_clock::now();
276 wait_until(lock, s_now + ceil<nanoseconds>(d));
277 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
282 template <class lock_type>
283 cv_status wait_until(
285 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
287 using namespace chrono;
288 nanoseconds d = tp.time_since_epoch();
289 timespec ts = boost::detail::to_timespec(d);
290 if (do_wait_until(lk, ts)) return cv_status::no_timeout;
291 else return cv_status::timeout;
294 #else // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
295 #ifdef BOOST_THREAD_USES_CHRONO
297 template <class lock_type, class Duration>
301 const chrono::time_point<chrono::steady_clock, Duration>& t)
303 using namespace chrono;
304 typedef time_point<steady_clock, nanoseconds> nano_sys_tmpt;
306 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
307 return steady_clock::now() < t ? cv_status::no_timeout :
311 template <class lock_type, class Clock, class Duration>
315 const chrono::time_point<Clock, Duration>& t)
317 using namespace chrono;
318 steady_clock::time_point s_now = steady_clock::now();
319 typename Clock::time_point c_now = Clock::now();
320 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
321 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
324 template <class lock_type, class Rep, class Period>
328 const chrono::duration<Rep, Period>& d)
330 using namespace chrono;
331 steady_clock::time_point c_now = steady_clock::now();
332 wait_until(lock, c_now + ceil<nanoseconds>(d));
333 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
337 template <class lock_type>
338 inline cv_status wait_until(
340 chrono::time_point<chrono::steady_clock, chrono::nanoseconds> tp)
342 using namespace chrono;
343 nanoseconds d = tp.time_since_epoch();
344 timespec ts = boost::detail::to_timespec(d);
345 if (do_wait_until(lock, ts)) return cv_status::no_timeout;
346 else return cv_status::timeout;
350 #endif // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
352 #ifdef BOOST_THREAD_USES_CHRONO
353 template <class lock_type, class Clock, class Duration, class Predicate>
357 const chrono::time_point<Clock, Duration>& t,
362 if (wait_until(lock, t) == cv_status::timeout)
368 template <class lock_type, class Rep, class Period, class Predicate>
372 const chrono::duration<Rep, Period>& d,
375 return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
379 void notify_one() BOOST_NOEXCEPT
381 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
382 BOOST_VERIFY(!pthread_cond_signal(&cond));
385 void notify_all() BOOST_NOEXCEPT
387 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
388 BOOST_VERIFY(!pthread_cond_broadcast(&cond));
390 private: // used by boost::thread::try_join_until
392 template <class lock_type>
395 struct timespec const &timeout)
399 thread_cv_detail::lock_on_exit<lock_type> guard;
400 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
401 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
403 boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
406 res=pthread_cond_timedwait(&cond,&internal_mutex,&timeout);
408 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
409 this_thread::interruption_point();
417 boost::throw_exception(condition_error(res, "boost::condition_variable_any::do_wait_until() failed in pthread_cond_timedwait"));
425 #include <boost/config/abi_suffix.hpp>