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