diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/platform_thread.cc | 18 | ||||
-rw-r--r-- | base/platform_thread.h | 3 |
2 files changed, 21 insertions, 0 deletions
diff --git a/base/platform_thread.cc b/base/platform_thread.cc index 6b24b3d..6668f9c 100644 --- a/base/platform_thread.cc +++ b/base/platform_thread.cc @@ -30,6 +30,7 @@ #include "base/platform_thread.h" #if defined(OS_POSIX) +#include <errno.h> #include <sched.h> #endif @@ -55,6 +56,23 @@ void PlatformThread::YieldCurrentThread() { #endif } +// static +void PlatformThread::Sleep(int duration_ms) { +#if defined(OS_WIN) + ::Sleep(duration_ms); +#elif defined (OS_POSIX) + struct timespec sleep_time, remaining; + // Contains the portion of duration_ms >= 1 sec. + sleep_time.tv_sec = duration_ms / 1000; + duration_ms -= sleep_time.tv_sec * 1000; + // Contains the portion of duration_ms < 1 sec. + sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds. + while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) { + sleep_time = remaining; + } +#endif +} + bool PlatformThread::operator==(const PlatformThread& other_thread) { #if defined(OS_WIN) return thread_ == other_thread.thread_; diff --git a/base/platform_thread.h b/base/platform_thread.h index 2dca402..82f3362 100644 --- a/base/platform_thread.h +++ b/base/platform_thread.h @@ -52,6 +52,9 @@ class PlatformThread { // Yield the current thread so another thread can be scheduled. static void YieldCurrentThread(); + // Sleeps for the specified duration (units are milliseconds). + static void Sleep(int duration_ms); + bool operator==(const PlatformThread& other_thread); private: |