1 #ifndef BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP 2 #define BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP 10 #define USING_BACKPORTED_BOOST_CONDITION_VARIABLE 1 12 #include <boost/assert.hpp> 13 #include <boost/throw_exception.hpp> 15 #include <boost/thread/cv_status.hpp> 16 #include <boost/thread/mutex.hpp> 17 #include <boost/thread/lock_types.hpp> 18 #include <boost/thread/thread_time.hpp> 19 #include <boost/thread/pthread/timespec.hpp> 20 #if defined BOOST_THREAD_USES_DATETIME 21 #include <boost/thread/xtime.hpp> 24 #ifdef BOOST_THREAD_USES_CHRONO 25 #include <boost/chrono/system_clocks.hpp> 26 #include <boost/chrono/ceil.hpp> 28 #include <boost/thread/detail/delete.hpp> 29 #include <boost/date_time/posix_time/posix_time_duration.hpp> 31 #include <boost/config/abi_prefix.hpp> 38 #ifdef BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC 39 pthread_condattr_t attr;
40 int res = pthread_condattr_init(&attr);
45 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
46 res=pthread_cond_init(&cond,&attr);
47 pthread_condattr_destroy(&attr);
50 return pthread_cond_init(&cond,NULL);
59 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS 60 pthread_mutex_t internal_mutex;
67 inline bool do_wait_until(
68 unique_lock<mutex>& lock,
69 struct timespec
const &timeout);
72 unique_lock<mutex>& lock,
73 struct timespec
const &timeout)
75 #if ! defined BOOST_THREAD_USEFIXES_TIMESPEC 76 return do_wait_until(lock, boost::detail::timespec_plus(timeout, boost::detail::timespec_now()));
77 #elif ! defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC 81 struct timespec ts = boost::detail::timespec_now_realtime();
84 return do_wait_until(lock, boost::detail::timespec_plus(timeout, ts));
87 return do_wait_until(lock, boost::detail::timespec_plus(timeout, boost::detail::timespec_now_realtime()));
96 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS 97 res=pthread_mutex_init(&internal_mutex,NULL);
100 boost::throw_exception(thread_resource_error(res,
"boost::condition_variable::condition_variable() constructor failed in pthread_mutex_init"));
106 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS 107 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
109 boost::throw_exception(thread_resource_error(res,
"boost::condition_variable::condition_variable() constructor failed in detail::monotonic_pthread_cond_init"));
115 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS 117 ret = pthread_mutex_destroy(&internal_mutex);
118 }
while (ret == EINTR);
122 ret = pthread_cond_destroy(&cond);
123 }
while (ret == EINTR);
127 void wait(unique_lock<mutex>& m);
129 template<
typename predicate_type>
130 void wait(unique_lock<mutex>& m,predicate_type pred)
132 while(!pred()) wait(m);
135 #if defined BOOST_THREAD_USES_DATETIME 136 inline bool timed_wait(
137 unique_lock<mutex>& m,
138 boost::system_time
const& abs_time)
140 #if defined BOOST_THREAD_WAIT_BUG 141 struct timespec const timeout=detail::to_timespec(abs_time + BOOST_THREAD_WAIT_BUG);
142 return do_wait_until(m, timeout);
144 struct timespec const timeout=detail::to_timespec(abs_time);
145 return do_wait_until(m, timeout);
149 unique_lock<mutex>& m,
150 xtime
const& abs_time)
152 return timed_wait(m,system_time(abs_time));
155 template<
typename duration_type>
157 unique_lock<mutex>& m,
158 duration_type
const& wait_duration)
160 if (wait_duration.is_pos_infinity())
165 if (wait_duration.is_special())
169 return timed_wait(m,get_system_time()+wait_duration);
172 template<
typename predicate_type>
174 unique_lock<mutex>& m,
175 boost::system_time
const& abs_time,predicate_type pred)
179 if(!timed_wait(m, abs_time))
185 template<
typename predicate_type>
187 unique_lock<mutex>& m,
188 xtime
const& abs_time,predicate_type pred)
190 return timed_wait(m,system_time(abs_time),pred);
193 template<
typename duration_type,
typename predicate_type>
195 unique_lock<mutex>& m,
196 duration_type
const& wait_duration,predicate_type pred)
198 if (wait_duration.is_pos_infinity())
206 if (wait_duration.is_special())
210 return timed_wait(m,get_system_time()+wait_duration,pred);
214 #ifndef BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC 216 #ifdef BOOST_THREAD_USES_CHRONO 218 template <
class Duration>
221 unique_lock<mutex>& lock,
222 const chrono::time_point<chrono::system_clock, Duration>& t)
224 using namespace chrono;
225 typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
227 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
228 return system_clock::now() < t ? cv_status::no_timeout :
232 template <
class Clock,
class Duration>
235 unique_lock<mutex>& lock,
236 const chrono::time_point<Clock, Duration>& t)
238 using namespace chrono;
239 system_clock::time_point s_now = system_clock::now();
240 typename Clock::time_point c_now = Clock::now();
241 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
242 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
247 template <
class Rep,
class Period>
250 unique_lock<mutex>& lock,
251 const chrono::duration<Rep, Period>&
d)
253 using namespace chrono;
254 system_clock::time_point s_now = system_clock::now();
255 steady_clock::time_point c_now = steady_clock::now();
256 wait_until(lock, s_now + ceil<nanoseconds>(d));
257 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
262 inline cv_status wait_until(
263 unique_lock<mutex>& lk,
264 chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
266 using namespace chrono;
267 nanoseconds d = tp.time_since_epoch();
268 timespec ts = boost::detail::to_timespec(d);
269 if (do_wait_until(lk, ts))
return cv_status::no_timeout;
270 else return cv_status::timeout;
274 #else // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC 275 #ifdef BOOST_THREAD_USES_CHRONO 277 template <
class Duration>
280 unique_lock<mutex>& lock,
281 const chrono::time_point<chrono::steady_clock, Duration>& t)
283 using namespace chrono;
284 typedef time_point<steady_clock, nanoseconds> nano_sys_tmpt;
286 nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
287 return steady_clock::now() < t ? cv_status::no_timeout :
291 template <
class Clock,
class Duration>
294 unique_lock<mutex>& lock,
295 const chrono::time_point<Clock, Duration>& t)
297 using namespace chrono;
298 steady_clock::time_point s_now = steady_clock::now();
299 typename Clock::time_point c_now = Clock::now();
300 wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
301 return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
304 template <
class Rep,
class Period>
307 unique_lock<mutex>& lock,
308 const chrono::duration<Rep, Period>& d)
310 using namespace chrono;
311 steady_clock::time_point c_now = steady_clock::now();
312 wait_until(lock, c_now + ceil<nanoseconds>(d));
313 return steady_clock::now() - c_now < d ? cv_status::no_timeout :
317 inline cv_status wait_until(
318 unique_lock<mutex>& lk,
319 chrono::time_point<chrono::steady_clock, chrono::nanoseconds> tp)
321 using namespace chrono;
322 nanoseconds d = tp.time_since_epoch();
323 timespec ts = boost::detail::to_timespec(d);
324 if (do_wait_until(lk, ts))
return cv_status::no_timeout;
325 else return cv_status::timeout;
329 #endif // defined BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC 331 #ifdef BOOST_THREAD_USES_CHRONO 332 template <
class Clock,
class Duration,
class Predicate>
335 unique_lock<mutex>& lock,
336 const chrono::time_point<Clock, Duration>& t,
341 if (wait_until(lock, t) == cv_status::timeout)
347 template <
class Rep,
class Period,
class Predicate>
350 unique_lock<mutex>& lock,
351 const chrono::duration<Rep, Period>& d,
354 return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
358 #define BOOST_THREAD_DEFINES_CONDITION_VARIABLE_NATIVE_HANDLE 365 void notify_one() BOOST_NOEXCEPT;
366 void notify_all() BOOST_NOEXCEPT;
376 #include <boost/config/abi_suffix.hpp>
BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable &cond, unique_lock< mutex > lk)
bool do_wait_for(unique_lock< mutex > &lock, struct timespec const &timeout)
native_handle_type native_handle()
int monotonic_pthread_cond_init(pthread_cond_t &cond)
pthread_cond_t * native_handle_type
void wait(unique_lock< mutex > &m, predicate_type pred)