functions_pos.cpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Includes
00012 *****************************************************************************/
00013 
00014 #include "../../include/ecl/time_lite/functions_pos.hpp"
00015 #include <errno.h>
00016 
00017 /*****************************************************************************
00018 ** Macros
00019 *****************************************************************************/
00020 
00021 #if defined(ECL_HAS_POSIX_TIMERS)
00022 
00023 /*****************************************************************************
00024 ** Namespaces
00025 *****************************************************************************/
00026 
00027 namespace ecl {
00028 
00029 TimeError epoch_time(TimeStructure &time) {
00030         struct timeval tv;
00031         int result = gettimeofday(&tv, NULL);
00032     if ( result == 0 ) {
00033             time.tv_sec = tv.tv_sec;
00034             time.tv_nsec = tv.tv_usec*1000;
00035             return TimeError(NoError);
00036     } else {
00037         switch (errno) {
00038                         case(EFAULT) : { return TimeError(MemoryError); }          // time was not in addressable memory space
00039                         case(EINVAL) : { return TimeError(ArgNotSupportedError); } // timezone (or something else) is invalid
00040                         default : { return TimeError(UnknownError); }
00041                 }
00042     }
00043 }
00044 TimeError sleep_until(const TimeStructure &time) {
00045         TimeStructure current_time, sleep_time;
00046 
00047         TimeError error = epoch_time(current_time);
00048         if ( error.flag() != NoError ) { return error; }
00049 
00050         /*********************
00051         ** current > time?
00052         **********************/
00053         if ( current_time.tv_sec > time.tv_sec ) {
00054                 return TimeError(NoError); // return immediately
00055         } else if ( current_time.tv_sec == time.tv_sec ) {
00056                 if ( current_time.tv_nsec > time.tv_nsec ) {
00057                         return TimeError(NoError);  // return immediately
00058                 }
00059         }
00060         sleep_time.tv_sec = time.tv_sec - current_time.tv_sec;
00061         if ( current_time.tv_nsec <= time.tv_nsec ) {
00062                 sleep_time.tv_nsec = time.tv_nsec - current_time.tv_nsec;
00063         } else {
00064                 sleep_time.tv_sec -= 1;
00065                 sleep_time.tv_nsec = 1000000000L - current_time.tv_nsec + time.tv_nsec;
00066         }
00067         int result = nanosleep(&sleep_time, NULL);
00068         switch (result) {
00069                 case(0) : { return TimeError(NoError); }
00070                 case(EFAULT) : { return TimeError(MemoryError); }     // some memory error copying information around
00071                 case(EINTR) : { return TimeError(InterruptedError); } // interrupted by a signal
00072                 case(EINVAL) : { return TimeError(OutOfRangeError); } // sec/nsec pair specified was out of range
00073                 default : { return TimeError(UnknownError); }
00074         }
00075 }
00076 TimeError sleep(const TimeStructure &time) {
00077         int result = nanosleep(&time, NULL);
00078         switch (result) {
00079                 case(0) : { return TimeError(NoError); }
00080                 case(EFAULT) : { return TimeError(MemoryError); }     // some memory error copying information around
00081                 case(EINTR) : { return TimeError(InterruptedError); } // interrupted by a signal
00082                 case(EINVAL) : { return TimeError(OutOfRangeError); } // sec/nsec pair specified was out of range
00083                 default : { return TimeError(UnknownError); }
00084         }
00085 }
00086 
00087 } // namespace ecl
00088 
00089 #endif


ecl_time_lite
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:16