functions_mac.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include "../../include/ecl/time_lite/functions_mac.hpp"
13 #include <errno.h>
14 
15 /*****************************************************************************
16 ** Macros
17 *****************************************************************************/
18 
19 #if defined(ECL_HAS_MACH_TIMERS)
20 
21 /*****************************************************************************
22 ** Namespaces
23 *****************************************************************************/
24 
25 namespace ecl {
26 
27 TimeError epoch_time(TimeStructure &time) {
28  struct timeval tv;
29  int result = gettimeofday(&tv, NULL);
30  if ( result == 0 ) {
31  time.tv_sec = tv.tv_sec;
32  time.tv_nsec = tv.tv_usec*1000;
33  return TimeError(NoError);
34  } else {
35  switch (errno) {
36  case(EFAULT) : { return TimeError(MemoryError); } // time was not in addressable memory space
37  case(EINVAL) : { return TimeError(ArgNotSupportedError); } // timezone (or something else) is invalid
38  default : { return TimeError(UnknownError); }
39  }
40  }
41 }
42 
43 TimeError sleep_until(const TimeStructure &time) {
44  TimeStructure current_time, sleep_time;
45 
46  TimeError error = epoch_time(current_time);
47  if ( error.flag() != NoError ) { return error; }
48 
49  /*********************
50  ** current > time?
51  **********************/
52  if ( current_time.tv_sec > time.tv_sec ) {
53  return TimeError(NoError); // return immediately
54  } else if ( current_time.tv_sec == time.tv_sec ) {
55  if ( current_time.tv_nsec > time.tv_nsec ) {
56  return TimeError(NoError); // return immediately
57  }
58  }
59  sleep_time.tv_sec = time.tv_sec - current_time.tv_sec;
60  if ( current_time.tv_nsec <= time.tv_nsec ) {
61  sleep_time.tv_nsec = time.tv_nsec - current_time.tv_nsec;
62  } else {
63  sleep_time.tv_sec -= 1;
64  sleep_time.tv_nsec = 1000000000L - current_time.tv_nsec + time.tv_nsec;
65  }
66  int result = nanosleep(&sleep_time, NULL);
67  switch (result) {
68  case(0) : { return TimeError(NoError); }
69  case(EFAULT) : { return TimeError(MemoryError); } // some memory error copying information around
70  case(EINTR) : { return TimeError(InterruptedError); } // interrupted by a signal
71  case(EINVAL) : { return TimeError(OutOfRangeError); } // sec/nsec pair specified was out of range
72  default : { return TimeError(UnknownError); }
73  }
74 }
75 
76 TimeError sleep(const TimeStructure &time) {
77  int result = nanosleep(&time, NULL);
78  switch (result) {
79  case(0) : { return TimeError(NoError); }
80  case(EFAULT) : { return TimeError(MemoryError); } // some memory error copying information around
81  case(EINTR) : { return TimeError(InterruptedError); } // interrupted by a signal
82  case(EINVAL) : { return TimeError(OutOfRangeError); } // sec/nsec pair specified was out of range
83  default : { return TimeError(UnknownError); }
84  }
85 }
86 } // namespace ecl
87 #endif
InterruptedError
UnknownError
OutOfRangeError
MemoryError
ArgNotSupportedError


ecl_time_lite
Author(s): Daniel Stonier
autogenerated on Fri Jun 7 2019 21:52:47