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

Side by Side Diff: mojo/edk/system/waitable_event.cc

Issue 1420713006: Revert "EDK: Move //mojo/edk/system/waitable_event* to edk/util." (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month 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/waitable_event.h ('k') | mojo/edk/system/waitable_event_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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/util/waitable_event.h" 5 #include "mojo/edk/system/waitable_event.h"
6 6
7 #include <errno.h> 7 #include "base/logging.h"
8 #include <time.h> 8 #include "base/time/time.h"
9 9
10 #include "mojo/edk/util/logging_internal.h" 10 using mojo::util::CondVar;
11 using mojo::util::Mutex;
12 using mojo::util::MutexLocker;
11 13
12 namespace mojo { 14 namespace mojo {
13 namespace util { 15 namespace system {
14 16
15 namespace { 17 namespace {
16 18
17 // Returns the number of microseconds elapsed since epoch start (according to a
18 // monotonic clock).
19 uint64_t Now() {
20 const uint64_t kMicrosecondsPerSecond = 1000000ULL;
21 const uint64_t kNanosecondsPerMicrosecond = 1000ULL;
22
23 struct timespec now;
24 int error = clock_gettime(CLOCK_MONOTONIC, &now);
25 INTERNAL_DCHECK_WITH_ERRNO(!error, "clock_gettime", errno);
26 INTERNAL_DCHECK(now.tv_sec >= 0);
27 INTERNAL_DCHECK(now.tv_nsec >= 0);
28
29 return static_cast<uint64_t>(now.tv_sec) * kMicrosecondsPerSecond +
30 static_cast<uint64_t>(now.tv_nsec) / kNanosecondsPerMicrosecond;
31 }
32
33 // Waits with a timeout on |condition()|. Returns true on timeout, or false if 19 // Waits with a timeout on |condition()|. Returns true on timeout, or false if
34 // |condition()| ever returns true. |condition()| should have no side effects 20 // |condition()| ever returns true. |condition()| should have no side effects
35 // (and will always be called with |*mutex| held). 21 // (and will always be called with |*mutex| held).
36 template <typename ConditionFn> 22 template <typename ConditionFn>
37 bool WaitWithTimeoutImpl(Mutex* mutex, 23 bool WaitWithTimeoutImpl(Mutex* mutex,
38 CondVar* cv, 24 CondVar* cv,
39 ConditionFn condition, 25 ConditionFn condition,
40 uint64_t timeout_microseconds) 26 uint64_t timeout_microseconds)
41 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex) { 27 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex) {
42 mutex->AssertHeld(); 28 mutex->AssertHeld();
43 29
44 if (condition()) 30 if (condition())
45 return false; 31 return false;
46 32
47 // We may get spurious wakeups. 33 // We may get spurious wakeups.
48 uint64_t wait_remaining = timeout_microseconds; 34 uint64_t wait_remaining = timeout_microseconds;
49 uint64_t start = Now(); 35 auto start = base::TimeTicks::Now();
50 while (true) { 36 while (true) {
51 if (cv->WaitWithTimeout(mutex, wait_remaining)) 37 if (cv->WaitWithTimeout(mutex, wait_remaining))
52 return true; // Definitely timed out. 38 return true; // Definitely timed out.
53 39
54 // We may have been awoken. 40 // We may have been awoken.
55 if (condition()) 41 if (condition())
56 return false; 42 return false;
57 43
58 // Or the wakeup may have been spurious. 44 // Or the wakeup may have been spurious.
59 uint64_t now = Now(); 45 auto now = base::TimeTicks::Now();
60 INTERNAL_DCHECK(now >= start); 46 DCHECK_GE(now, start);
61 uint64_t elapsed = now - start; 47 uint64_t elapsed = static_cast<uint64_t>((now - start).InMicroseconds());
62 // It's possible that we may have timed out anyway. 48 // It's possible that we may have timed out anyway.
63 if (elapsed >= timeout_microseconds) 49 if (elapsed >= timeout_microseconds)
64 return true; 50 return true;
65 51
66 // Otherwise, recalculate the amount that we have left to wait. 52 // Otherwise, recalculate the amount that we have left to wait.
67 wait_remaining = timeout_microseconds - elapsed; 53 wait_remaining = timeout_microseconds - elapsed;
68 } 54 }
69 } 55 }
70 56
71 } // namespace 57 } // namespace
(...skipping 21 matching lines...) Expand all
93 bool AutoResetWaitableEvent::WaitWithTimeout(uint64_t timeout_microseconds) { 79 bool AutoResetWaitableEvent::WaitWithTimeout(uint64_t timeout_microseconds) {
94 MutexLocker locker(&mutex_); 80 MutexLocker locker(&mutex_);
95 81
96 if (signaled_) { 82 if (signaled_) {
97 signaled_ = false; 83 signaled_ = false;
98 return false; 84 return false;
99 } 85 }
100 86
101 // We may get spurious wakeups. 87 // We may get spurious wakeups.
102 uint64_t wait_remaining = timeout_microseconds; 88 uint64_t wait_remaining = timeout_microseconds;
103 uint64_t start = Now(); 89 auto start = base::TimeTicks::Now();
104 while (true) { 90 while (true) {
105 if (cv_.WaitWithTimeout(&mutex_, wait_remaining)) 91 if (cv_.WaitWithTimeout(&mutex_, wait_remaining))
106 return true; // Definitely timed out. 92 return true; // Definitely timed out.
107 93
108 // We may have been awoken. 94 // We may have been awoken.
109 if (signaled_) 95 if (signaled_)
110 break; 96 break;
111 97
112 // Or the wakeup may have been spurious. 98 // Or the wakeup may have been spurious.
113 uint64_t now = Now(); 99 auto now = base::TimeTicks::Now();
114 INTERNAL_DCHECK(now >= start); 100 DCHECK_GE(now, start);
115 uint64_t elapsed = now - start; 101 uint64_t elapsed = static_cast<uint64_t>((now - start).InMicroseconds());
116 // It's possible that we may have timed out anyway. 102 // It's possible that we may have timed out anyway.
117 if (elapsed >= timeout_microseconds) 103 if (elapsed >= timeout_microseconds)
118 return true; 104 return true;
119 105
120 // Otherwise, recalculate the amount that we have left to wait. 106 // Otherwise, recalculate the amount that we have left to wait.
121 wait_remaining = timeout_microseconds - elapsed; 107 wait_remaining = timeout_microseconds - elapsed;
122 } 108 }
123 109
124 signaled_ = false; 110 signaled_ = false;
125 return false; 111 return false;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 auto last_signal_id = signal_id_; 148 auto last_signal_id = signal_id_;
163 // Disable thread-safety analysis for the lambda: We could annotate it with 149 // Disable thread-safety analysis for the lambda: We could annotate it with
164 // |MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_)|, but then the analyzer currently 150 // |MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_)|, but then the analyzer currently
165 // isn't able to figure out that |WaitWithTimeoutImpl()| calls it while 151 // isn't able to figure out that |WaitWithTimeoutImpl()| calls it while
166 // holding |mutex_|. 152 // holding |mutex_|.
167 bool rv = WaitWithTimeoutImpl( 153 bool rv = WaitWithTimeoutImpl(
168 &mutex_, &cv_, [this, last_signal_id]() MOJO_NO_THREAD_SAFETY_ANALYSIS { 154 &mutex_, &cv_, [this, last_signal_id]() MOJO_NO_THREAD_SAFETY_ANALYSIS {
169 // Also check |signaled_| in case we're already signaled. 155 // Also check |signaled_| in case we're already signaled.
170 return signaled_ || signal_id_ != last_signal_id; 156 return signaled_ || signal_id_ != last_signal_id;
171 }, timeout_microseconds); 157 }, timeout_microseconds);
172 INTERNAL_DCHECK(rv || signaled_ || signal_id_ != last_signal_id); 158 DCHECK(rv || signaled_ || signal_id_ != last_signal_id);
173 return rv; 159 return rv;
174 } 160 }
175 161
176 bool ManualResetWaitableEvent::IsSignaledForTest() { 162 bool ManualResetWaitableEvent::IsSignaledForTest() {
177 MutexLocker locker(&mutex_); 163 MutexLocker locker(&mutex_);
178 return signaled_; 164 return signaled_;
179 } 165 }
180 166
181 } // namespace util 167 } // namespace system
182 } // namespace mojo 168 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/system/waitable_event.h ('k') | mojo/edk/system/waitable_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698