snooze_pos.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Platform Check
00010 *****************************************************************************/
00011 
00012 #include <ecl/config.hpp>
00013 #if defined(ECL_IS_POSIX)
00014 
00015 /*****************************************************************************
00016 ** Includes
00017 *****************************************************************************/
00018 
00019 #include <ecl/time_lite/functions.hpp>
00020 #include "../../include/ecl/time/snooze_pos.hpp"
00021 
00022 /*****************************************************************************
00023 ** Namespaces
00024 *****************************************************************************/
00025 
00026 namespace ecl {
00027 
00028 /*****************************************************************************
00029 ** Implementation [Snooze]
00030 *****************************************************************************/
00031 
00032 Snooze::Snooze() {
00033         period(Duration(0,10000000));
00034 }
00035 
00036 Snooze::Snooze(const Duration &time, const bool& validate) { period(time, validate); }
00037 
00038 void Snooze::period(const Duration &time, const bool& validate) {
00039         validate_times = validate;
00040         time_period.tv_sec = time.sec();
00041         time_period.tv_nsec = time.nsec();
00042     wrap_value_ns = 1000000000L-time_period.tv_nsec;
00043     initialise();
00044 }
00045 
00046 void Snooze::initialise() {
00047         epoch_time(time_value);
00048 }
00049 
00050 void Snooze::operator()() {
00051 
00052     add_period();
00053     validate();
00054     sleep_until(time_value);
00055 }
00056 
00057 void Snooze::add_period() {
00058 
00059         // Nanoseconds
00060     if ( time_value.tv_nsec > wrap_value_ns ) { // wrap
00061         time_value.tv_nsec = time_value.tv_nsec - wrap_value_ns;
00062         time_value.tv_sec += 1;
00063     } else {
00064         time_value.tv_nsec += time_period.tv_nsec;
00065     }
00066     // Seconds
00067     time_value.tv_sec += time_period.tv_sec;
00068 
00069     if ( validate_times ) {
00070         validate();
00071     }
00072 }
00073 
00074 void Snooze::validate() {
00075 
00076         TimeStructure time_current;
00077     epoch_time(time_current);
00078 
00079     if ( time_current.tv_sec > time_value.tv_sec ) {
00080         // Revalidate
00081         time_value.tv_sec = time_current.tv_sec;
00082         time_value.tv_nsec = time_current.tv_nsec;
00083         add_period();
00084     }  else if ( time_current.tv_sec == time_value.tv_sec ) {
00085         if ( time_current.tv_nsec > time_value.tv_nsec ) {
00086                 // Revalidate
00087             time_value.tv_sec = time_current.tv_sec;
00088             time_value.tv_nsec = time_current.tv_nsec;
00089             add_period();
00090         }
00091     }
00092 }
00093 
00094 } // namespace ecl
00095 
00096 #endif /* ECL_IS_POSIX */


ecl_time
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:32