1 // Copyright (C) 2001-2003
3 // Copyright (C) 2007-8 Anthony Williams
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #if !defined(UTIL_INL_WEK01242003)
9 #define UTIL_INL_WEK01242003
11 #include <boost/thread/xtime.hpp>
12 #include <boost/thread/mutex.hpp>
13 #include <boost/thread/condition.hpp>
14 #include <boost/thread/thread.hpp>
16 #ifndef DEFAULT_EXECUTION_MONITOR_TYPE
17 # define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_condition
20 // boostinspect:nounnamed
26 inline boost::xtime delay(int secs, int msecs=0, int nsecs=0)
28 const int MILLISECONDS_PER_SECOND = 1000;
29 const int NANOSECONDS_PER_SECOND = 1000000000;
30 const int NANOSECONDS_PER_MILLISECOND = 1000000;
33 if (boost::TIME_UTC_ != boost::xtime_get (&xt, boost::TIME_UTC_))
34 BOOST_ERROR ("boost::xtime_get != boost::TIME_UTC_");
37 msecs += nsecs / NANOSECONDS_PER_MILLISECOND;
38 secs += msecs / MILLISECONDS_PER_SECOND;
39 nsecs += (msecs % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
40 xt.nsec = nsecs % NANOSECONDS_PER_SECOND;
41 xt.sec += secs + (nsecs / NANOSECONDS_PER_SECOND);
53 inline bool in_range(const boost::xtime& xt, int secs=1)
55 boost::xtime min = delay(-secs);
56 boost::xtime max = delay(0);
57 return (boost::xtime_cmp(xt, min) >= 0) &&
58 (boost::xtime_cmp(xt, max) <= 0);
67 class execution_monitor
70 enum wait_type { use_sleep_only, use_mutex, use_condition };
72 execution_monitor(wait_type type, int secs)
73 : done(false), type(type), secs(secs) { }
76 if (type != use_sleep_only) {
77 boost::unique_lock<boost::mutex> lock(mutex); done = false;
84 if (type != use_sleep_only) {
85 boost::unique_lock<boost::mutex> lock(mutex);
87 if (type == use_condition)
95 boost::xtime xt = delay(secs);
96 if (type == use_sleep_only) {
97 boost::thread::sleep(xt);
100 if (type == use_condition) {
101 boost::unique_lock<boost::mutex> lock(mutex);
103 if (!cond.timed_wait(lock, xt))
108 for (int i = 0; ; ++i) {
110 boost::unique_lock<boost::mutex> lock(mutex);
113 else if (i > secs * 2)
116 boost::thread::sleep(delay(0, 500));
122 boost::condition cond;
128 namespace thread_detail_anon
130 template <typename F>
131 class indirect_adapter
134 indirect_adapter(F func, execution_monitor& monitor)
135 : func(func), monitor(monitor) { }
136 void operator()() const
140 boost::thread thrd(func);
153 execution_monitor& monitor;
154 void operator=(indirect_adapter&);
158 // boostinspect:nounnamed
162 template <typename F>
163 void timed_test(F func, int secs,
164 execution_monitor::wait_type type=DEFAULT_EXECUTION_MONITOR_TYPE)
166 execution_monitor monitor(type, secs);
167 thread_detail_anon::indirect_adapter<F> ifunc(func, monitor);
169 boost::thread thrd(ifunc);
170 BOOST_REQUIRE_MESSAGE(monitor.wait(),
171 "Timed test didn't complete in time, possible deadlock.");
176 namespace thread_detail_anon
179 template <typename F, typename T>
183 thread_binder(const F& func, const T& param)
184 : func(func), param(param) { }
185 void operator()() const { func(param); }
194 // boostinspect:nounnamed
197 template <typename F, typename T>
198 thread_detail_anon::thread_binder<F, T> bind(const F& func, const T& param)
200 return thread_detail_anon::thread_binder<F, T>(func, param);
204 namespace thread_detail_anon
207 template <typename R, typename T>
208 class thread_member_binder
211 thread_member_binder(R (T::*func)(), T& param)
212 : func(func), param(param) { }
213 void operator()() const { (param.*func)(); }
216 void operator=(thread_member_binder&);
224 // boostinspect:nounnamed
227 template <typename R, typename T>
228 thread_detail_anon::thread_member_binder<R, T> bind(R (T::*func)(), T& param)
230 return thread_detail_anon::thread_member_binder<R, T>(func, param);