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>
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include "internal_macros.h"
|
2013-12-19 08:55:45 +08:00
|
|
|
|
2013-12-20 08:18:09 +08:00
|
|
|
namespace benchmark {
|
2013-12-19 08:55:45 +08:00
|
|
|
#ifdef OS_WINDOWS
|
|
|
|
// Window's _sleep takes milliseconds argument.
|
2014-01-10 04:12:11 +08:00
|
|
|
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
|
|
|
}
|
2014-01-10 04:12:11 +08:00
|
|
|
#else // OS_WINDOWS
|
2015-02-19 14:21:39 +08:00
|
|
|
void SleepForMicroseconds(int microseconds) {
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
void SleepForMilliseconds(int milliseconds) {
|
2015-02-19 14:21:39 +08:00
|
|
|
SleepForMicroseconds(static_cast<int>(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
|
|
|
}
|
|
|
|
#endif // OS_WINDOWS
|
2013-12-20 08:18:09 +08:00
|
|
|
} // end namespace benchmark
|