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


ecl_time_lite
Author(s): Daniel Stonier
autogenerated on Thu Jun 16 2016 09:47:18