OLD | NEW |
| (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 | |
OLD | NEW |