]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright 2015 Google Inc. All rights reserved. |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | #include "sleep.h" | |
16 | ||
17 | #include <cerrno> | |
18 | #include <cstdlib> | |
19 | #include <ctime> | |
20 | ||
21 | #include "internal_macros.h" | |
22 | ||
23 | #ifdef BENCHMARK_OS_WINDOWS | |
24 | #include <windows.h> | |
25 | #endif | |
26 | ||
27 | #ifdef BENCHMARK_OS_ZOS | |
28 | #include <unistd.h> | |
29 | #endif | |
30 | ||
31 | namespace benchmark { | |
32 | #ifdef BENCHMARK_OS_WINDOWS | |
33 | // Window's Sleep takes milliseconds argument. | |
34 | void SleepForMilliseconds(int milliseconds) { Sleep(milliseconds); } | |
35 | void SleepForSeconds(double seconds) { | |
36 | SleepForMilliseconds(static_cast<int>(kNumMillisPerSecond * seconds)); | |
37 | } | |
38 | #else // BENCHMARK_OS_WINDOWS | |
39 | void SleepForMicroseconds(int microseconds) { | |
40 | #ifdef BENCHMARK_OS_ZOS | |
41 | // z/OS does not support nanosleep. Instead call sleep() and then usleep() to | |
42 | // sleep for the remaining microseconds because usleep() will fail if its | |
43 | // argument is greater than 1000000. | |
44 | div_t sleepTime = div(microseconds, kNumMicrosPerSecond); | |
45 | int seconds = sleepTime.quot; | |
46 | while (seconds != 0) | |
47 | seconds = sleep(seconds); | |
48 | while (usleep(sleepTime.rem) == -1 && errno == EINTR) | |
49 | ; | |
50 | #else | |
51 | struct timespec sleep_time; | |
52 | sleep_time.tv_sec = microseconds / kNumMicrosPerSecond; | |
53 | sleep_time.tv_nsec = (microseconds % kNumMicrosPerSecond) * kNumNanosPerMicro; | |
54 | while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR) | |
55 | ; // Ignore signals and wait for the full interval to elapse. | |
56 | #endif | |
57 | } | |
58 | ||
59 | void SleepForMilliseconds(int milliseconds) { | |
60 | SleepForMicroseconds(milliseconds * kNumMicrosPerMilli); | |
61 | } | |
62 | ||
63 | void SleepForSeconds(double seconds) { | |
64 | SleepForMicroseconds(static_cast<int>(seconds * kNumMicrosPerSecond)); | |
65 | } | |
66 | #endif // BENCHMARK_OS_WINDOWS | |
67 | } // end namespace benchmark |