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_)
56 ~lock_on_exit() BOOST_NOEXCEPT_IF(false)
66 inline void condition_variable::wait(unique_lock<mutex>& m)
68 #if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
71 boost::throw_exception(condition_error(-1, "boost::condition_variable::wait() failed precondition mutex not owned"));
76 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
77 thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
78 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
79 pthread_mutex_t* the_mutex = &internal_mutex;
81 res = pthread_cond_wait(&cond,the_mutex);
82 check_for_interruption.unlock_if_locked();
85 pthread_mutex_t* the_mutex = m.mutex()->native_handle();
86 res = pthread_cond_wait(&cond,the_mutex);
89 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
90 this_thread::interruption_point();
92 if(res && res != EINTR)
94 boost::throw_exception(condition_error(res, "boost::condition_variable::wait failed in pthread_cond_wait"));
98 inline bool condition_variable::do_wait_until(
99 unique_lock<mutex>& m,
100 struct timespec const &timeout)
102 #if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
105 boost::throw_exception(condition_error(EPERM, "boost::condition_variable::do_wait_until() failed precondition mutex not owned"));
110 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
111 thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard;
112 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
113 pthread_mutex_t* the_mutex = &internal_mutex;
115 cond_res=pthread_cond_timedwait(&cond,the_mutex,&timeout);
116 check_for_interruption.unlock_if_locked();
119 pthread_mutex_t* the_mutex = m.mutex()->native_handle();
120 cond_res=pthread_cond_timedwait(&cond,the_mutex,&timeout);
123 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
124 this_thread::interruption_point();
126 if(cond_res==ETIMEDOUT)
132 boost::throw_exception(condition_error(cond_res, "boost::condition_variable::do_wait_until failed in pthread_cond_timedwait"));
137 inline void condition_variable::notify_one() BOOST_NOEXCEPT
139 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
140 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
142 BOOST_VERIFY(!pthread_cond_signal(&cond));
145 inline void condition_variable::notify_all() BOOST_NOEXCEPT
147 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
148 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
150 BOOST_VERIFY(!pthread_cond_broadcast(&cond));
153 class condition_variable_any
155 pthread_mutex_t internal_mutex;
159 BOOST_THREAD_NO_COPYABLE(condition_variable_any)
160 condition_variable_any()
162 int const res=pthread_mutex_init(&internal_mutex,NULL);
165 boost::throw_exception(thread_resource_error(res, "boost::condition_variable_any::condition_variable_any() failed in pthread_mutex_init"));
167 int const res2 = detail::monotonic_pthread_cond_init(cond);
170 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
171 boost::throw_exception(thread_resource_error(res2, "boost::condition_variable_any::condition_variable_any() failed in detail::monotonic_pthread_cond_init"));
174 ~condition_variable_any()
176 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
177 BOOST_VERIFY(!pthread_cond_destroy(&cond));
180 template<typename lock_type>
181 void wait(lock_type& m)
185 thread_cv_detail::lock_on_exit<lock_type> guard;
186 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
187 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
189 boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
192 res=pthread_cond_wait(&cond,&internal_mutex);
193 check_for_interruption.unlock_if_locked();
196 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
197 this_thread::interruption_point();
201 boost::throw_exception(condition_error(res, "boost::condition_variable_any::wait() failed in pthread_cond_wait"));
205 template<typename lock_type,typename predicate_type>
206 void wait(lock_type& m,predicate_type pred)
208 while(!pred()) wait(m);
211 #if defined BOOST_THREAD_USES_DATETIME
212 template<typename lock_type>
213 bool timed_wait(lock_type& m,boost::system_time const& abs_time)
215 struct timespec const timeout=detail::to_timespec(abs_time);
216 return do_wait_until(m, timeout);
218 template<typename lock_type>
219 bool timed_wait(lock_type& m,xtime const& abs_time)
221 return timed_wait(m,system_time(abs_time));
224 template<typename lock_type,typename duration_type>
225 bool timed_wait(lock_type& m,duration_type const& wait_duration)
227 return timed_wait(m,get_system_time()+wait_duration);
230 template<typename lock_type,typename predicate_type>
231 bool timed_wait(lock_type& m,boost::system_time const& abs_time, predicate_type pred)
235 if(!timed_wait(m, abs_time))
241 template<typename lock_type,typename predicate_type>
242 bool timed_wait(lock_type& m,xtime const& abs_time, predicate_type pred)
244 return timed_wait(m,system_time(abs_time),pred);
247 template<typename lock_type,typename duration_type,typename predicate_type>
248 bool timed_wait(lock_type& m,duration_type const& wait_duration,predicate_type pred)
250 return timed_wait(m,get_system_time()+wait_duration,pred);
253 #ifndef BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
255 #ifdef BOOST_THREAD_USES_CHRONO
256 template <class lock_type,class Duration>
260 const chrono::time_point<chrono::system_clock, Duration>& t)
262 using namespace chrono;
263 typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
265 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
266 return system_clock::now() < t ? cv_status::no_timeout :
270 template <class lock_type, class Clock, class Duration>
274 const chrono::time_point<Clock, Duration>& t)
276 using namespace chrono;
277 system_clock::time_point s_now = system_clock::now();
278 typename Clock::time_point c_now = Clock::now();
279 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
280 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
283 template <class lock_type, class Rep, class Period>
287 const chrono::duration<Rep, Period>& d)
289 using namespace chrono;
290 system_clock::time_point s_now = system_clock::now();
291 steady_clock::time_point c_now = steady_clock::now();
292 wait_until(lock, s_now + ceil<nanoseconds>(d));
293 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
298 template <class lock_type>
299 cv_status wait_until(
301 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
303 using namespace chrono;
304 nanoseconds d = tp.time_since_epoch();
305 timespec ts = boost::detail::to_timespec(d);
306 if (do_wait_until(lk, ts)) return cv_status::no_timeout;
307 else return cv_status::timeout;
310 #else // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
311 #ifdef BOOST_THREAD_USES_CHRONO
313 template <class lock_type, class Duration>
317 const chrono::time_point<chrono::steady_clock, Duration>& t)
319 using namespace chrono;
320 typedef time_point<steady_clock, nanoseconds> nano_sys_tmpt;
322 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
323 return steady_clock::now() < t ? cv_status::no_timeout :
327 template <class lock_type, class Clock, class Duration>
331 const chrono::time_point<Clock, Duration>& t)
333 using namespace chrono;
334 steady_clock::time_point s_now = steady_clock::now();
335 typename Clock::time_point c_now = Clock::now();
336 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
337 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
340 template <class lock_type, class Rep, class Period>
344 const chrono::duration<Rep, Period>& d)
346 using namespace chrono;
347 steady_clock::time_point c_now = steady_clock::now();
348 wait_until(lock, c_now + ceil<nanoseconds>(d));
349 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
353 template <class lock_type>
354 inline cv_status wait_until(
356 chrono::time_point<chrono::steady_clock, chrono::nanoseconds> tp)
358 using namespace chrono;
359 nanoseconds d = tp.time_since_epoch();
360 timespec ts = boost::detail::to_timespec(d);
361 if (do_wait_until(lock, ts)) return cv_status::no_timeout;
362 else return cv_status::timeout;
366 #endif // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
368 #ifdef BOOST_THREAD_USES_CHRONO
369 template <class lock_type, class Clock, class Duration, class Predicate>
373 const chrono::time_point<Clock, Duration>& t,
378 if (wait_until(lock, t) == cv_status::timeout)
384 template <class lock_type, class Rep, class Period, class Predicate>
388 const chrono::duration<Rep, Period>& d,
391 return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
395 void notify_one() BOOST_NOEXCEPT
397 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
398 BOOST_VERIFY(!pthread_cond_signal(&cond));
401 void notify_all() BOOST_NOEXCEPT
403 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
404 BOOST_VERIFY(!pthread_cond_broadcast(&cond));
406 private: // used by boost::thread::try_join_until
408 template <class lock_type>
411 struct timespec const &timeout)
415 thread_cv_detail::lock_on_exit<lock_type> guard;
416 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
417 detail::interruption_checker check_for_interruption(&internal_mutex,&cond);
419 boost::pthread::pthread_mutex_scoped_lock check_for_interruption(&internal_mutex);
422 res=pthread_cond_timedwait(&cond,&internal_mutex,&timeout);
423 check_for_interruption.unlock_if_locked();
426 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
427 this_thread::interruption_point();
435 boost::throw_exception(condition_error(res, "boost::condition_variable_any::do_wait_until() failed in pthread_cond_timedwait"));
443 #include <boost/config/abi_suffix.hpp>