diff options
author | paulg@google.com <paulg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-12 22:37:12 +0000 |
---|---|---|
committer | paulg@google.com <paulg@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-12 22:37:12 +0000 |
commit | 2141858324fd45ece7d3e96c60145a71e493fbe0 (patch) | |
tree | e65285a0a5694f82a3363c863ae198ff8578f789 /base | |
parent | 69d868a6ff2192096874277fd89aa9977ab202a8 (diff) | |
download | chromium_src-2141858324fd45ece7d3e96c60145a71e493fbe0.zip chromium_src-2141858324fd45ece7d3e96c60145a71e493fbe0.tar.gz chromium_src-2141858324fd45ece7d3e96c60145a71e493fbe0.tar.bz2 |
Provide a cross platform sleep API.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@749 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/platform_thread.cc | 23 | ||||
-rw-r--r-- | base/platform_thread.h | 5 |
2 files changed, 28 insertions, 0 deletions
diff --git a/base/platform_thread.cc b/base/platform_thread.cc index 6b24b3d..4b6c34f 100644 --- a/base/platform_thread.cc +++ b/base/platform_thread.cc @@ -28,8 +28,10 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "base/platform_thread.h" +#include "base/time.h" #if defined(OS_POSIX) +#include <errno.h> #include <sched.h> #endif @@ -55,6 +57,27 @@ void PlatformThread::YieldCurrentThread() { #endif } +// static +void PlatformThread::Sleep(TimeDelta sleep_duration) { +#if defined(OS_WIN) + ::Sleep(static_cast<DWORD>(sleep_duration.InMilliseconds())); +#elif defined(OS_POSIX) + struct timespec sleep_time, remaining; + // Contains the portion of sleep_duration >= 1 sec. + sleep_time.tv_sec = sleep_duration.InSeconds(); + sleep_duration -= TimeDelta::FromSeconds(sleep_time.tv_sec); + // Contains the portion of sleep_duration < 1 sec. + sleep_time.tv_nsec = sleep_duration.InMicroseconds() * + Time::kNanosecondsPerMicrosecond; + while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR) { + // Interrupted by a signal, we'll resume sleeping for the remainder of the + // original duration. + 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..4b3cb39 100644 --- a/base/platform_thread.h +++ b/base/platform_thread.h @@ -32,6 +32,8 @@ #include "build/build_config.h" +class TimeDelta; + #if defined(OS_WIN) #include <windows.h> @@ -51,6 +53,9 @@ class PlatformThread { // Yield the current thread so another thread can be scheduled. static void YieldCurrentThread(); + + // Sleep for the specified duration. + static void Sleep(TimeDelta sleep_duration); bool operator==(const PlatformThread& other_thread); |