1 /*
2 Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #ifndef NDBSLEEP_H
26 #define NDBSLEEP_H
27
28 #include <ndb_global.h>
29
30 static inline void NdbSleep_MilliSleep(int milliseconds);
31
32 static inline
NdbSleep_MicroSleep(int microseconds)33 void NdbSleep_MicroSleep(int microseconds)
34 {
35 assert(0 < microseconds);
36 #ifdef _WIN32
37 // Waitable timer use 100ns time unit, negative for relative time periods
38 LARGE_INTEGER liDueTime;
39 liDueTime.QuadPart = -10LL * microseconds;
40
41 HANDLE hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
42 if (NULL == hTimer ||
43 !SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0) ||
44 WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
45 {
46 #ifndef NDEBUG
47 // Error code for crash analysis
48 DWORD winerr = GetLastError();
49 #endif
50 assert(false);
51 // Fallback to millisleep in release
52 NdbSleep_MilliSleep(1 + (microseconds - 1) / 1000);
53 }
54 if (NULL != hTimer)
55 {
56 CloseHandle(hTimer);
57 }
58 #elif defined(HAVE_NANOSLEEP)
59 struct timespec t;
60 t.tv_sec = microseconds / 1000000;
61 t.tv_nsec = 1000 * (microseconds % 1000000);
62 while (nanosleep(&t, &t) == -1)
63 {
64 if (errno != EINTR)
65 {
66 assert(false);
67 // Fallback to millisleep in release
68 NdbSleep_MilliSleep(1 + (microseconds - 1) / 1000);
69 return ;
70 }
71 }
72 #else
73 // Fallback to millisleep
74 NdbSleep_MilliSleep(1 + (microseconds - 1) / 1000);
75 #endif
76 }
77
78 static inline
NdbSleep_MilliSleep(int milliseconds)79 void NdbSleep_MilliSleep(int milliseconds)
80 {
81 #ifdef _WIN32
82 Sleep(milliseconds);
83 #elif defined(HAVE_SELECT)
84 struct timeval t;
85 t.tv_sec = milliseconds / 1000L;
86 t.tv_usec = (milliseconds % 1000L) * 1000L;
87 select(0,0,0,0,&t);
88 #else
89 #error No suitable function found to implement millisecond sleep.
90 #endif
91 }
92
93 static inline
NdbSleep_SecSleep(int seconds)94 void NdbSleep_SecSleep(int seconds)
95 {
96 NdbSleep_MilliSleep(seconds*1000);
97 }
98
99 #endif
100