fix8  version 1.4.0
Open Source C++ FIX Framework
hypersleep.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-----------------------------------------------------------------------------------------
37 #ifndef FIX8_HYPERSLEEP_HPP_
38 #define FIX8_HYPERSLEEP_HPP_
39 
40 namespace FIX8 {
41 
42 //----------------------------------------------------------------------------------------
44 
45 #ifdef _MSC_VER
46 struct timespec
47 {
48  time_t tv_sec; // seconds
49  long tv_nsec; // nanoseconds
50 };
51 extern "C" __declspec(dllimport) void __stdcall Sleep(unsigned long);
52 #endif
53 
54 //----------------------------------------------------------------------------------------
55 namespace
56 {
57  const unsigned thousand(1000);
58  const unsigned million(thousand * thousand);
59  const int billion(thousand * million);
60 
61 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
62  inline int execute_clock_nanosleep(timespec ts)
63  {
64  if (ts.tv_nsec >= billion)
65  {
66  ++ts.tv_sec;
67  ts.tv_nsec -= billion;
68  }
69  return clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, 0);
70  }
71 #endif
72 }
73 
74 //----------------------------------------------------------------------------------------
75 template<hyperunits_t>
76 inline int hypersleep (unsigned amt);
77 
78 //----------------------------------------------------------------------------------------
82 template<>
83 inline int hypersleep<h_seconds>(unsigned amt)
84 {
85 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
86  timespec ts;
87  clock_gettime(CLOCK_MONOTONIC, &ts);
88  ts.tv_sec += amt;
89  ts.tv_nsec += (amt % (billion));
90  return execute_clock_nanosleep(ts);
91 #elif defined _MSC_VER
92  Sleep(amt * thousand);
93  return 0;
94 #else
95  const timespec tspec { amt, amt % billion };
96  return nanosleep(&tspec, 0);
97 #endif
98 }
99 
100 //----------------------------------------------------------------------------------------
104 template<>
105 inline int hypersleep<h_milliseconds>(unsigned amt)
106 {
107 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
108  timespec ts;
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
114  Sleep(amt); // milliseconds
115  return 0;
116 #else
117  const timespec tspec { amt / thousand, million * (amt % thousand) };
118  return nanosleep(&tspec, 0);
119 #endif
120 }
121 
122 //----------------------------------------------------------------------------------------
126 template<>
127 inline int hypersleep<h_microseconds>(unsigned amt)
128 {
129 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
130  timespec ts;
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);
137  return 0;
138 #else
139  const timespec tspec { amt / million, thousand * (amt % million) };
140  return nanosleep(&tspec, 0);
141 #endif
142 }
143 
144 //----------------------------------------------------------------------------------------
148 template<>
149 inline int hypersleep<h_nanoseconds>(unsigned amt)
150 {
151 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
152  timespec ts;
153  clock_gettime(CLOCK_MONOTONIC, &ts);
154  ts.tv_sec += (amt / billion);
155  ts.tv_nsec += amt;
156  return execute_clock_nanosleep(ts);
157 #elif defined _MSC_VER
158  Sleep(amt / billion * million);
159  return 0;
160 #else
161  const timespec tspec { amt / billion, amt };
162  return nanosleep(&tspec, 0);
163 #endif
164 }
165 
166 //----------------------------------------------------------------------------------------
171 inline int hypersleep (unsigned amt, hyperunits_t units)
172 {
173  enum { Div, Mul, Operation };
174  static const unsigned hv[h_count][Operation]
175  {
176  { 1, billion }, // Seconds
177  { thousand, million }, // Milliseconds
178  { million, thousand }, // Microseconds
179  { billion, 1 }, // Nanoseconds
180  };
181 
182 #if defined FIX8_HAVE_CLOCK_NANOSLEEP
183  timespec ts;
184  clock_gettime(CLOCK_MONOTONIC, &ts);
185  ts.tv_sec += (amt / hv[units][Div]); // calculate time to sleep in secs
186  ts.tv_nsec += (hv[units][Mul] * (amt % hv[units][Div])); // calculate time to sleep in nsecs
187  return execute_clock_nanosleep(ts);
188 #elif defined _MSC_VER
189  Sleep(amt); // milliseconds
190  return 0;
191 #else
192  const timespec tspec { amt / hv[units][Div], hv[units][Mul] * (amt % hv[units][Div]) };
193  return nanosleep(&tspec, 0);
194 #endif
195 }
196 
197 //----------------------------------------------------------------------------------------
198 } // namespace FIX8_HYPERSLEEP_HPP_
199 
200 #endif // FIX8_HYPERSLEEP_HPP_
201 
hyperunits_t
Definition: hypersleep.hpp:43
int hypersleep< h_milliseconds >(unsigned amt)
Definition: hypersleep.hpp:105
int hypersleep< h_microseconds >(unsigned amt)
Definition: hypersleep.hpp:127
int hypersleep< h_seconds >(unsigned amt)
Definition: hypersleep.hpp:83
int hypersleep(unsigned amt)
int hypersleep< h_nanoseconds >(unsigned amt)
Definition: hypersleep.hpp:149