• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ctime>
19 
20 #include "internal_macros.h"
21 
22 #ifdef BENCHMARK_OS_WINDOWS
23 #include <Windows.h>
24 #endif
25 
26 namespace benchmark {
27 #ifdef BENCHMARK_OS_WINDOWS
28 // Window's Sleep takes milliseconds argument.
SleepForMilliseconds(int milliseconds)29 void SleepForMilliseconds(int milliseconds) { Sleep(milliseconds); }
SleepForSeconds(double seconds)30 void SleepForSeconds(double seconds) {
31   SleepForMilliseconds(static_cast<int>(kNumMillisPerSecond * seconds));
32 }
33 #else   // BENCHMARK_OS_WINDOWS
34 void SleepForMicroseconds(int microseconds) {
35   struct timespec sleep_time;
36   sleep_time.tv_sec = microseconds / kNumMicrosPerSecond;
37   sleep_time.tv_nsec = (microseconds % kNumMicrosPerSecond) * kNumNanosPerMicro;
38   while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR)
39     ;  // Ignore signals and wait for the full interval to elapse.
40 }
41 
42 void SleepForMilliseconds(int milliseconds) {
43   SleepForMicroseconds(static_cast<int>(milliseconds) * kNumMicrosPerMilli);
44 }
45 
46 void SleepForSeconds(double seconds) {
47   SleepForMicroseconds(static_cast<int>(seconds * kNumMicrosPerSecond));
48 }
49 #endif  // BENCHMARK_OS_WINDOWS
50 }  // end namespace benchmark
51