sleep_pos.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Platform Check
10 *****************************************************************************/
11 
12 #include <ecl/config.hpp>
13 #if defined(ECL_IS_POSIX)
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <ecl/errors/macros.hpp>
20 #include "../../include/ecl/time/duration.hpp"
21 #include "../../include/ecl/time/sleep_pos.hpp"
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace ecl {
28 
29 /*****************************************************************************
30 ** Implementation [Sleep]
31 *****************************************************************************/
32 Sleep::Sleep(const Duration &duration) {
33  required.tv_sec = duration.sec();
34  required.tv_nsec = duration.nsec();
35 }
36 
37 Sleep::Sleep(const unsigned long &seconds) {
38  required.tv_sec = seconds;
39  required.tv_nsec = 0;
40 }
41 
42 void Sleep::operator ()() {
43  int result = nanosleep(&required, &remaining);
44  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
45 }
46 
47 void Sleep::operator ()(const Duration &duration) {
48  required.tv_sec = duration.sec();
49  required.tv_nsec = duration.nsec();
50  int result = nanosleep(&required, &remaining);
51  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
52 }
53 
54 void Sleep::operator ()(const unsigned long &seconds) {
55  required.tv_sec = seconds;
56  required.tv_nsec = 0;
57  int result = nanosleep(&required, &remaining);
58  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
59 }
60 
61 /*****************************************************************************
62 ** Implementation [MilliSleep]
63 *****************************************************************************/
64 
65 MilliSleep::MilliSleep(const unsigned long &milliseconds) {
66  required.tv_sec = milliseconds/1000; // integer division
67  required.tv_nsec = (milliseconds%1000)*1000000;
68 }
69 
70 void MilliSleep::operator ()() {
71  int result = nanosleep(&required, &remaining);
72  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
73 }
74 
75 void MilliSleep::operator ()(const unsigned long &milliseconds) {
76  required.tv_sec = milliseconds/1000; // integer division
77  required.tv_nsec = (milliseconds%1000)*1000000;
78 // required.tv_nsec = 1000000*milli_seconds;
79  int result = nanosleep(&required, &remaining);
80  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
81 }
82 
83 /*****************************************************************************
84 ** Implementation [MicroSleep]
85 *****************************************************************************/
86 
87 MicroSleep::MicroSleep(const unsigned long &microseconds) {
88  required.tv_sec = microseconds/1000000; // integer division
89  required.tv_nsec = (microseconds%1000000)*1000;
90 }
91 
92 void MicroSleep::operator ()() {
93  int result = nanosleep(&required, &remaining);
94  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
95 }
96 
97 void MicroSleep::operator ()(const unsigned long &micro_seconds) {
98  required.tv_nsec = 1000*micro_seconds;
99  int result = nanosleep(&required, &remaining);
100  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
101 }
102 
103 /*****************************************************************************
104 ** Implementation [NanoSleep]
105 *****************************************************************************/
106 
107 NanoSleep::NanoSleep(const unsigned long &nanoseconds) {
108  required.tv_sec = nanoseconds/1000000000; // integer division
109  required.tv_nsec = nanoseconds%1000000000;
110 }
111 
112 void NanoSleep::operator ()() {
113  int result = nanosleep(&required, &remaining);
114  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
115 }
116 
117 void NanoSleep::operator ()(const unsigned long &nanoseconds) {
118  required.tv_nsec = nanoseconds;
119  int result = nanosleep(&required, &remaining);
120  ecl_assert_throw( result == 0, time::throwSleepException(LOC) );
121 }
122 
123 } // namespace ecl
124 
125 #endif /* ECL_IS_POSIX */
126 
ecl::Duration
TimeStamp Duration
Convenience typedef to associate timestamps with the concept of durations.
Definition: duration.hpp:47
config.hpp
ecl_assert_throw
#define ecl_assert_throw(expression, exception)
macros.hpp
ecl
Embedded control libraries.


ecl_time
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:19