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


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