2015-03-07 06:01:05 +08:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
2014-01-10 04:19:02 +08:00
|
|
|
//
|
2014-01-10 00:01:34 +08:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-01-10 04:19:02 +08:00
|
|
|
//
|
2014-01-10 00:01:34 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-01-10 04:19:02 +08:00
|
|
|
//
|
2014-01-10 00:01:34 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-12-19 08:55:45 +08:00
|
|
|
#include "sleep.h"
|
|
|
|
|
2015-03-07 06:01:05 +08:00
|
|
|
#include <cerrno>
|
2017-03-14 10:30:19 +08:00
|
|
|
#include <cstdlib>
|
2015-03-07 06:01:05 +08:00
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include "internal_macros.h"
|
2013-12-19 08:55:45 +08:00
|
|
|
|
2015-10-05 19:58:35 +08:00
|
|
|
#ifdef BENCHMARK_OS_WINDOWS
|
2018-09-18 16:42:20 +08:00
|
|
|
#include <windows.h>
|
2015-03-27 21:38:13 +08:00
|
|
|
#endif
|
|
|
|
|
2020-10-29 16:49:02 +08:00
|
|
|
#ifdef BENCHMARK_OS_ZOS
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 08:18:09 +08:00
|
|
|
namespace benchmark {
|
2015-10-05 19:58:35 +08:00
|
|
|
#ifdef BENCHMARK_OS_WINDOWS
|
2015-03-27 21:38:13 +08:00
|
|
|
// Window's Sleep takes milliseconds argument.
|
|
|
|
void SleepForMilliseconds(int milliseconds) { Sleep(milliseconds); }
|
2013-12-19 08:55:45 +08:00
|
|
|
void SleepForSeconds(double seconds) {
|
2013-12-20 09:16:40 +08:00
|
|
|
SleepForMilliseconds(static_cast<int>(kNumMillisPerSecond * seconds));
|
2013-12-19 08:55:45 +08:00
|
|
|
}
|
2021-11-11 00:04:32 +08:00
|
|
|
#else // BENCHMARK_OS_WINDOWS
|
2015-02-19 14:21:39 +08:00
|
|
|
void SleepForMicroseconds(int microseconds) {
|
2020-10-29 16:49:02 +08:00
|
|
|
#ifdef BENCHMARK_OS_ZOS
|
|
|
|
// z/OS does not support nanosleep. Instead call sleep() and then usleep() to
|
|
|
|
// sleep for the remaining microseconds because usleep() will fail if its
|
|
|
|
// argument is greater than 1000000.
|
|
|
|
div_t sleepTime = div(microseconds, kNumMicrosPerSecond);
|
|
|
|
int seconds = sleepTime.quot;
|
2021-11-11 00:04:32 +08:00
|
|
|
while (seconds != 0) seconds = sleep(seconds);
|
2020-10-29 16:49:02 +08:00
|
|
|
while (usleep(sleepTime.rem) == -1 && errno == EINTR)
|
|
|
|
;
|
|
|
|
#else
|
2013-12-19 08:55:45 +08:00
|
|
|
struct timespec sleep_time;
|
|
|
|
sleep_time.tv_sec = microseconds / kNumMicrosPerSecond;
|
|
|
|
sleep_time.tv_nsec = (microseconds % kNumMicrosPerSecond) * kNumNanosPerMicro;
|
|
|
|
while (nanosleep(&sleep_time, &sleep_time) != 0 && errno == EINTR)
|
|
|
|
; // Ignore signals and wait for the full interval to elapse.
|
2020-10-29 16:49:02 +08:00
|
|
|
#endif
|
2013-12-19 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SleepForMilliseconds(int milliseconds) {
|
2017-03-28 08:32:12 +08:00
|
|
|
SleepForMicroseconds(milliseconds * kNumMicrosPerMilli);
|
2013-12-19 08:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SleepForSeconds(double seconds) {
|
2015-02-19 14:21:39 +08:00
|
|
|
SleepForMicroseconds(static_cast<int>(seconds * kNumMicrosPerSecond));
|
2013-12-19 08:55:45 +08:00
|
|
|
}
|
2015-10-13 00:50:01 +08:00
|
|
|
#endif // BENCHMARK_OS_WINDOWS
|
2013-12-20 08:18:09 +08:00
|
|
|
} // end namespace benchmark
|