summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpaulg@google.com <paulg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-13 01:20:26 +0000
committerpaulg@google.com <paulg@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-13 01:20:26 +0000
commitf4ed29583452e1e1d62c05aa886586956f8d2cb9 (patch)
tree7af74b5055e1710a59f9457f979fb5b3bac56bde
parent703f427ed4c2e067d95890f8edbbbdc29fc62e1d (diff)
downloadchromium_src-f4ed29583452e1e1d62c05aa886586956f8d2cb9.zip
chromium_src-f4ed29583452e1e1d62c05aa886586956f8d2cb9.tar.gz
chromium_src-f4ed29583452e1e1d62c05aa886586956f8d2cb9.tar.bz2
Add a cross-platform sleep API. We don't use TimeDelta
here to avoid linking to winmm.dll. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@772 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/platform_thread.cc18
-rw-r--r--base/platform_thread.h3
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: