functions_rt.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include "../../include/ecl/time_lite/functions_rt.hpp"
13 #include <errno.h>
14 
15 /*****************************************************************************
16 ** Macros
17 *****************************************************************************/
18 
19 #if defined(ECL_HAS_RT_TIMERS)
20 
21 /*****************************************************************************
22 ** Namespaces
23 *****************************************************************************/
24 
25 namespace ecl {
26 
27 TimeError epoch_time(TimeStructure &time) {
28  // _POSIX_CLOCK_MONOTONIC should have gotten us this far,
29  // just check if we have CLOCK_MONOTONIC and if not, fallback to
30  // CLOCK_REALTIME.
31 #ifdef ECL_HAS_CLOCK_MONOTONIC
32  int result = clock_gettime(CLOCK_MONOTONIC,&time);
33 #else
34  int result = clock_gettime(CLOCK_REALTIME,&time);
35 #endif
36  switch (result) {
37  case(0) : { return TimeError(NoError); }
38  case(EFAULT) : { return TimeError(MemoryError); } // time was not in addressable memory space
39  case(EINVAL) : { return TimeError(ArgNotSupportedError); } // clock id is not supported (actually impossible if cmake detects)
40  case(EPERM) : { return TimeError(PermissionsError); } // user does not have permissions to use the clock.
41  default : { return TimeError(UnknownError); }
42  }
43 }
44 
45 TimeError realtime_epoch_time(TimeStructure &time) {
46  int result = clock_gettime(CLOCK_REALTIME,&time);
47  switch (result) {
48  case(0) : { return TimeError(NoError); }
49  case(EFAULT) : { return TimeError(MemoryError); } // time was not in addressable memory space
50  case(EINVAL) : { return TimeError(ArgNotSupportedError); } // clock id is not supported (actually impossible if cmake detects)
51  case(EPERM) : { return TimeError(PermissionsError); } // user does not have permissions to use the clock.
52  default : { return TimeError(UnknownError); }
53  }
54 }
55 
56 TimeError sleep_until(const TimeStructure &time) {
57  // Last arg is to catch remaining time if interrupted by a signal, not necessary here.
58 #ifdef ECL_HAS_CLOCK_MONOTONIC
59  int result = clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&time,NULL);
60 #else
61  int result = clock_nanosleep(CLOCK_REALTIME,TIMER_ABSTIME,&time,NULL);
62 #endif
63  switch (result) {
64  case(0) : { return TimeError(NoError); }
65  case(EFAULT) : { return TimeError(MemoryError); } // time was not in addressable memory space
66  case(EINTR) : { return TimeError(InterruptedError); } // interrupted by a signal
67  case(EINVAL) : { return TimeError(OutOfRangeError); } // sec/nsec pair specified was out of range
68  default : { return TimeError(UnknownError); }
69  }
70 }
71 TimeError sleep(const TimeStructure &time) {
72  int result = nanosleep(&time, NULL);
73  switch (result) {
74  case(0) : { return TimeError(NoError); }
75  case(EFAULT) : { return TimeError(MemoryError); } // some memory error copying information around
76  case(EINTR) : { return TimeError(InterruptedError); } // interrupted by a signal
77  case(EINVAL) : { return TimeError(OutOfRangeError); } // sec/nsec pair specified was out of range
78  default : { return TimeError(UnknownError); }
79  }
80 }
81 
82 } // namespace ecl
83 
84 #endif
InterruptedError
PermissionsError
UnknownError
OutOfRangeError
MemoryError
ArgNotSupportedError


ecl_time_lite
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:09:07