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 class condition_variable
35 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
36 pthread_mutex_t internal_mutex;
41 //private: // used by boost::thread::try_join_until
43 inline bool do_wait_until(
44 unique_lock<mutex>& lock,
45 struct timespec const &timeout);
48 unique_lock<mutex>& lock,
49 struct timespec const &timeout)
51 return do_wait_until(lock, boost::detail::timespec_plus(timeout, boost::detail::timespec_now()));
55 BOOST_THREAD_NO_COPYABLE(condition_variable)
58 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
59 int const res=pthread_mutex_init(&internal_mutex,NULL);
62 boost::throw_exception(thread_resource_error(res, "boost::condition_variable::condition_variable() constructor failed in pthread_mutex_init"));
65 int const res2=pthread_cond_init(&cond,NULL);
68 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
69 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
71 boost::throw_exception(thread_resource_error(res2, "boost::condition_variable::condition_variable() constructor failed in pthread_cond_init"));
77 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
79 ret = pthread_mutex_destroy(&internal_mutex);
80 } while (ret == EINTR);
84 ret = pthread_cond_destroy(&cond);
85 } while (ret == EINTR);
89 void wait(unique_lock<mutex>& m);
91 template<typename predicate_type>
92 void wait(unique_lock<mutex>& m,predicate_type pred)
94 while(!pred()) wait(m);
98 #if defined BOOST_THREAD_USES_DATETIME
99 inline bool timed_wait(
100 unique_lock<mutex>& m,
101 boost::system_time const& abs_time)
103 #if defined BOOST_THREAD_WAIT_BUG
104 struct timespec const timeout=detail::to_timespec(abs_time + BOOST_THREAD_WAIT_BUG);
105 return do_wait_until(m, timeout);
107 struct timespec const timeout=detail::to_timespec(abs_time);
108 return do_wait_until(m, timeout);
112 unique_lock<mutex>& m,
113 xtime const& abs_time)
115 return timed_wait(m,system_time(abs_time));
118 template<typename duration_type>
120 unique_lock<mutex>& m,
121 duration_type const& wait_duration)
123 return timed_wait(m,get_system_time()+wait_duration);
126 template<typename predicate_type>
128 unique_lock<mutex>& m,
129 boost::system_time const& abs_time,predicate_type pred)
133 if(!timed_wait(m, abs_time))
139 template<typename predicate_type>
141 unique_lock<mutex>& m,
142 xtime const& abs_time,predicate_type pred)
144 return timed_wait(m,system_time(abs_time),pred);
147 template<typename duration_type,typename predicate_type>
149 unique_lock<mutex>& m,
150 duration_type const& wait_duration,predicate_type pred)
152 return timed_wait(m,get_system_time()+wait_duration,pred);
156 #ifdef BOOST_THREAD_USES_CHRONO
158 template <class Duration>
161 unique_lock<mutex>& lock,
162 const chrono::time_point<chrono::system_clock, Duration>& t)
164 using namespace chrono;
165 typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
167 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
168 return system_clock::now() < t ? cv_status::no_timeout :
172 template <class Clock, class Duration>
175 unique_lock<mutex>& lock,
176 const chrono::time_point<Clock, Duration>& t)
178 using namespace chrono;
179 system_clock::time_point s_now = system_clock::now();
180 typename Clock::time_point c_now = Clock::now();
181 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
182 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
185 template <class Clock, class Duration, class Predicate>
188 unique_lock<mutex>& lock,
189 const chrono::time_point<Clock, Duration>& t,
194 if (wait_until(lock, t) == cv_status::timeout)
201 template <class Rep, class Period>
204 unique_lock<mutex>& lock,
205 const chrono::duration<Rep, Period>& d)
207 using namespace chrono;
208 system_clock::time_point s_now = system_clock::now();
209 steady_clock::time_point c_now = steady_clock::now();
210 wait_until(lock, s_now + ceil<nanoseconds>(d));
211 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
217 template <class Rep, class Period, class Predicate>
220 unique_lock<mutex>& lock,
221 const chrono::duration<Rep, Period>& d,
224 return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
228 // if (wait_for(lock, d) == cv_status::timeout)
235 #define BOOST_THREAD_DEFINES_CONDITION_VARIABLE_NATIVE_HANDLE
236 typedef pthread_cond_t* native_handle_type;
237 native_handle_type native_handle()
242 void notify_one() BOOST_NOEXCEPT;
243 void notify_all() BOOST_NOEXCEPT;
245 #ifdef BOOST_THREAD_USES_CHRONO
246 inline cv_status wait_until(
247 unique_lock<mutex>& lk,
248 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
250 using namespace chrono;
251 nanoseconds d = tp.time_since_epoch();
252 timespec ts = boost::detail::to_timespec(d);
253 if (do_wait_until(lk, ts)) return cv_status::no_timeout;
254 else return cv_status::timeout;
259 BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
264 #include <boost/config/abi_suffix.hpp>