summaryrefslogtreecommitdiffstats
path: root/base/threading/platform_thread_posix.cc
diff options
context:
space:
mode:
authortedvessenes@gmail.com <tedvessenes@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-30 01:04:36 +0000
committertedvessenes@gmail.com <tedvessenes@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-30 01:04:36 +0000
commit76784292d9ca82be8da21583d1e323dff2e513bc (patch)
treed0c706ff1a85b293eefba812e5b40360cab6b2d6 /base/threading/platform_thread_posix.cc
parent46adf7ff93ebbbfdaf6f8ad7b7253018f5cff3dc (diff)
downloadchromium_src-76784292d9ca82be8da21583d1e323dff2e513bc.zip
chromium_src-76784292d9ca82be8da21583d1e323dff2e513bc.tar.gz
chromium_src-76784292d9ca82be8da21583d1e323dff2e513bc.tar.bz2
Add function support for Sleep with TimeDelta input.
This is the first step of fixing issue 108171 (converting calls of Sleep() to use TimeDelta instead of ints in milliseconds). I checked platform_thread_unittests.cc for any tests explicitly for Sleep but found none, so I didn't add any for this interface. There will be a bit more implementation juggling here once the Sleep(int ms) interface is removed, but that's coming in a later CL. BUG=108171 TEST= Review URL: http://codereview.chromium.org/8965072 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116012 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/threading/platform_thread_posix.cc')
-rw-r--r--base/threading/platform_thread_posix.cc19
1 files changed, 13 insertions, 6 deletions
diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc
index ea1f037..1d72f07 100644
--- a/base/threading/platform_thread_posix.cc
+++ b/base/threading/platform_thread_posix.cc
@@ -152,14 +152,21 @@ void PlatformThread::YieldCurrentThread() {
// static
void PlatformThread::Sleep(int duration_ms) {
- struct timespec sleep_time, remaining;
+ // NOTE: This function will be supplanted by the other version of Sleep
+ // in the future. See issue 108171 for more information.
+ Sleep(TimeDelta::FromMilliseconds(duration_ms));
+}
- // Contains the portion of duration_ms >= 1 sec.
- sleep_time.tv_sec = duration_ms / 1000;
- duration_ms -= sleep_time.tv_sec * 1000;
+// static
+void PlatformThread::Sleep(TimeDelta duration) {
+ struct timespec sleep_time, remaining;
- // Contains the portion of duration_ms < 1 sec.
- sleep_time.tv_nsec = duration_ms * 1000 * 1000; // nanoseconds.
+ // Break the duration into seconds and nanoseconds.
+ // NOTE: TimeDelta's microseconds are int64s while timespec's
+ // nanoseconds are longs, so this unpacking must prevent overflow.
+ sleep_time.tv_sec = duration.InSeconds();
+ duration -= TimeDelta::FromSeconds(sleep_time.tv_sec);
+ sleep_time.tv_nsec = duration.InMicroseconds() * 1000; // nanoseconds
while (nanosleep(&sleep_time, &remaining) == -1 && errno == EINTR)
sleep_time = remaining;