Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef TIMINGTOOLS_HPP_
00035 #define TIMINGTOOLS_HPP_
00036 #include <boost/date_time/posix_time/posix_time.hpp>
00037 #include <boost/thread/thread.hpp>
00038 #include <boost/function.hpp>
00039 #include <boost/noncopyable.hpp>
00040
00041 namespace labust
00042 {
00043 namespace tools
00044 {
00050 inline double unix_time()
00051 {
00052 using namespace boost::posix_time;
00053 using namespace boost::gregorian;
00054 ptime epoch(date(1970,1,1));
00055 ptime t(microsec_clock::local_time());
00056 return (t-epoch).total_microseconds()/1000000.;
00057 }
00064 inline std::string time_signature()
00065 {
00066 using namespace boost::posix_time;
00067 using namespace boost::gregorian;
00068 std::stringstream out;
00069 ptime t(second_clock::local_time());
00070 out<<t.date().year();
00071 out<<"_"<<t.date().month();
00072 out<<"_"<<t.date().day();
00073 out<<"_"<<t.time_of_day().hours();
00074 out<<"_"<<t.time_of_day().minutes();
00075 out<<"_"<<t.time_of_day().seconds();
00076
00077 return out.str();
00078 }
00085 inline std::string unix_time_string(bool microseconds = false)
00086 {
00087 std::stringstream out;
00088 out.precision(6);
00089 out<<std::fixed<<(microseconds ? unix_time() : long(unix_time()));
00090 return out.str();
00091 }
00097 inline void sleep(size_t ms)
00098 {
00099 boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
00100 };
00106 struct wait_until_ms
00107 {
00113 wait_until_ms(size_t ms):
00114 #ifdef unix
00115 time(ms*1000){};
00116 #else
00117 time(ms){};
00118 #endif
00119
00123 inline void operator() ()
00124 {
00125 using namespace boost;
00126 using namespace posix_time;
00127 #ifdef unix
00128 this_thread::sleep(microseconds(time - microsec_clock::local_time().time_of_day().total_microseconds()%time));
00129 #else
00130 this_thread::sleep(milliseconds(time - microsec_clock::local_time().time_of_day().total_milliseconds()%time));
00131 #endif
00132 }
00133 private:
00137 size_t time;
00138 };
00143 struct watchdog : boost::noncopyable
00144 {
00152 watchdog(const boost::function<void (void)>& func, size_t timeout_ms = 500, size_t loop_ms = 100):
00153 func(func),
00154 counter(0),
00155 timeout_ms(timeout_ms),
00156 loop_ms(loop_ms),
00157 runner(boost::bind(&watchdog::run,this)){};
00161 ~watchdog()
00162 {
00163 runner.interrupt();
00164 runner.join();
00165 }
00166
00170 void reset(){boost::mutex::scoped_lock lock(cntMutex); this->counter = 0;};
00171
00172 private:
00176 void run()
00177 try
00178 {
00179 while (true)
00180 {
00181 using namespace boost;
00182 using namespace posix_time;
00183 sleep(loop_ms);
00184 boost::mutex::scoped_lock lock(cntMutex);
00185 counter += loop_ms;
00186 if (counter >= timeout_ms) func();
00187 }
00188 }
00189 catch (boost::thread_interrupted&){};
00190
00194 boost::function<void (void)> func;
00198 volatile size_t counter;
00202 size_t timeout_ms, loop_ms;
00206 boost::thread runner;
00210 boost::mutex cntMutex;
00211 };
00212 }
00213 }
00214
00215 #endif