Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: mojo/edk/system/test/sleep.cc

Issue 1639093002: Add //mojo/edk/platform/thread_utils.* containing "yield" and "sleep". (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/system/test/sleep.h ('k') | mojo/edk/system/test/sleep_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/edk/system/test/sleep.h"
6
7 #include <errno.h>
8 #include <stdint.h>
9 #include <time.h>
10
11 #include <limits>
12
13 #include "base/logging.h"
14 #include "mojo/edk/system/test/timeouts.h"
15
16 namespace mojo {
17 namespace system {
18 namespace test {
19
20 void Sleep(MojoDeadline duration) {
21 // TODO(vtl): This doesn't handle |MOJO_DEADLINE_INDEFINITE|. Should it?
22 DCHECK_NE(duration, MOJO_DEADLINE_INDEFINITE);
23
24 const uint64_t kMicrosecondsPerSecond = 1000000ULL;
25 const uint64_t kNanosecondsPerMicrosecond = 1000ULL;
26
27 uint64_t sleep_time_seconds = duration / kMicrosecondsPerSecond;
28 // |sleep_time.tv_sec| is a |time_t|.
29 DCHECK_LE(sleep_time_seconds,
30 static_cast<uint64_t>(std::numeric_limits<time_t>::max()));
31 uint64_t sleep_time_nanoseconds =
32 (duration % kMicrosecondsPerSecond) * kNanosecondsPerMicrosecond;
33
34 struct timespec sleep_time;
35 sleep_time.tv_sec = static_cast<time_t>(sleep_time_seconds);
36 sleep_time.tv_nsec = static_cast<long>(sleep_time_nanoseconds);
37
38 struct timespec sleep_time_remaining;
39 while (nanosleep(&sleep_time, &sleep_time_remaining) == -1) {
40 PCHECK(errno == EINTR) << "nanosleep";
41 sleep_time = sleep_time_remaining;
42 }
43 }
44
45 void SleepMilliseconds(unsigned duration_milliseconds) {
46 Sleep(DeadlineFromMilliseconds(duration_milliseconds));
47 }
48
49 } // namespace test
50 } // namespace system
51 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/test/sleep.h ('k') | mojo/edk/system/test/sleep_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698