37 #ifndef FIX8_HYPERSLEEP_HPP_
38 #define FIX8_HYPERSLEEP_HPP_
51 extern "C" __declspec(dllimport) void __stdcall Sleep(
unsigned long);
57 const unsigned thousand(1000);
58 const unsigned million(thousand * thousand);
59 const int billion(thousand * million);
61 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
62 inline int execute_clock_nanosleep(timespec ts)
64 if (ts.tv_nsec >= billion)
67 ts.tv_nsec -= billion;
69 return clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, 0);
75 template<hyperunits_t>
85 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
87 clock_gettime(CLOCK_MONOTONIC, &ts);
89 ts.tv_nsec += (amt % (billion));
90 return execute_clock_nanosleep(ts);
91 #elif defined _MSC_VER
92 Sleep(amt * thousand);
95 const timespec tspec { amt, amt % billion };
96 return nanosleep(&tspec, 0);
107 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
109 clock_gettime(CLOCK_MONOTONIC, &ts);
110 ts.tv_sec += (amt / thousand);
111 ts.tv_nsec += (million * (amt % thousand));
112 return execute_clock_nanosleep(ts);
113 #elif defined _MSC_VER
117 const timespec tspec { amt / thousand, million * (amt % thousand) };
118 return nanosleep(&tspec, 0);
129 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
131 clock_gettime(CLOCK_MONOTONIC, &ts);
132 ts.tv_sec += (amt / million);
133 ts.tv_nsec += (thousand * (amt % million));
134 return execute_clock_nanosleep(ts);
135 #elif defined _MSC_VER
136 Sleep(amt / million * thousand);
139 const timespec tspec { amt / million, thousand * (amt % million) };
140 return nanosleep(&tspec, 0);
151 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
153 clock_gettime(CLOCK_MONOTONIC, &ts);
154 ts.tv_sec += (amt / billion);
156 return execute_clock_nanosleep(ts);
157 #elif defined _MSC_VER
158 Sleep(amt / billion * million);
161 const timespec tspec { amt / billion, amt };
162 return nanosleep(&tspec, 0);
173 enum { Div, Mul, Operation };
174 static const unsigned hv[
h_count][Operation]
177 { thousand, million },
178 { million, thousand },
182 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
184 clock_gettime(CLOCK_MONOTONIC, &ts);
185 ts.tv_sec += (amt / hv[units][Div]);
186 ts.tv_nsec += (hv[units][Mul] * (amt % hv[units][Div]));
187 return execute_clock_nanosleep(ts);
188 #elif defined _MSC_VER
192 const timespec tspec { amt / hv[units][Div], hv[units][Mul] * (amt % hv[units][Div]) };
193 return nanosleep(&tspec, 0);
200 #endif // FIX8_HYPERSLEEP_HPP_
int hypersleep< h_milliseconds >(unsigned amt)
int hypersleep< h_microseconds >(unsigned amt)
int hypersleep< h_seconds >(unsigned amt)
int hypersleep(unsigned amt)
int hypersleep< h_nanoseconds >(unsigned amt)