OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/common/message_pump_mojo.h" | 5 #include "mojo/common/message_pump_mojo.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "mojo/common/message_pump_mojo_handler.h" | 13 #include "mojo/common/message_pump_mojo_handler.h" |
14 #include "mojo/common/time_helper.h" | 14 #include "mojo/common/time_helper.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 namespace common { | 17 namespace common { |
18 namespace { | |
19 | |
20 MojoDeadline TimeTicksToMojoDeadline(base::TimeTicks time_ticks, | |
21 base::TimeTicks now) { | |
22 // The is_null() check matches that of HandleWatcher. | |
23 if (time_ticks.is_null()) | |
24 return MOJO_DEADLINE_INDEFINITE; | |
25 const int64_t delta = (time_ticks - now).InMicroseconds(); | |
26 return delta < 0 ? static_cast<MojoDeadline>(0) : | |
27 static_cast<MojoDeadline>(delta); | |
28 } | |
29 | |
30 } // namespace | |
18 | 31 |
19 // State needed for one iteration of WaitMany. The first handle and flags | 32 // State needed for one iteration of WaitMany. The first handle and flags |
20 // corresponds to that of the control pipe. | 33 // corresponds to that of the control pipe. |
21 struct MessagePumpMojo::WaitState { | 34 struct MessagePumpMojo::WaitState { |
22 std::vector<Handle> handles; | 35 std::vector<Handle> handles; |
23 std::vector<MojoHandleSignals> wait_signals; | 36 std::vector<MojoHandleSignals> wait_signals; |
24 }; | 37 }; |
25 | 38 |
26 struct MessagePumpMojo::RunState { | 39 struct MessagePumpMojo::RunState { |
27 RunState() : should_quit(false) { | 40 RunState() : should_quit(false) { |
(...skipping 17 matching lines...) Expand all Loading... | |
45 | 58 |
46 // static | 59 // static |
47 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 60 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
48 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 61 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
49 } | 62 } |
50 | 63 |
51 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 64 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, |
52 const Handle& handle, | 65 const Handle& handle, |
53 MojoHandleSignals wait_signals, | 66 MojoHandleSignals wait_signals, |
54 base::TimeTicks deadline) { | 67 base::TimeTicks deadline) { |
55 DCHECK(handler); | 68 CHECK(handler); |
56 DCHECK(handle.is_valid()); | 69 DCHECK(handle.is_valid()); |
57 // Assume it's an error if someone tries to reregister an existing handle. | 70 // Assume it's an error if someone tries to reregister an existing handle. |
58 DCHECK_EQ(0u, handlers_.count(handle)); | 71 CHECK_EQ(0u, handlers_.count(handle)); |
59 Handler handler_data; | 72 Handler handler_data; |
60 handler_data.handler = handler; | 73 handler_data.handler = handler; |
61 handler_data.wait_signals = wait_signals; | 74 handler_data.wait_signals = wait_signals; |
62 handler_data.deadline = deadline; | 75 handler_data.deadline = deadline; |
63 handler_data.id = next_handler_id_++; | 76 handler_data.id = next_handler_id_++; |
64 handlers_[handle] = handler_data; | 77 handlers_[handle] = handler_data; |
65 } | 78 } |
66 | 79 |
67 void MessagePumpMojo::RemoveHandler(const Handle& handle) { | 80 void MessagePumpMojo::RemoveHandler(const Handle& handle) { |
68 handlers_.erase(handle); | 81 handlers_.erase(handle); |
(...skipping 28 matching lines...) Expand all Loading... | |
97 if (run_state_) | 110 if (run_state_) |
98 SignalControlPipe(*run_state_); | 111 SignalControlPipe(*run_state_); |
99 } | 112 } |
100 | 113 |
101 void MessagePumpMojo::ScheduleDelayedWork( | 114 void MessagePumpMojo::ScheduleDelayedWork( |
102 const base::TimeTicks& delayed_work_time) { | 115 const base::TimeTicks& delayed_work_time) { |
103 base::AutoLock auto_lock(run_state_lock_); | 116 base::AutoLock auto_lock(run_state_lock_); |
104 if (!run_state_) | 117 if (!run_state_) |
105 return; | 118 return; |
106 run_state_->delayed_work_time = delayed_work_time; | 119 run_state_->delayed_work_time = delayed_work_time; |
107 SignalControlPipe(*run_state_); | 120 SignalControlPipe(*run_state_); |
sky
2014/08/13 17:06:33
This isn't needed as ScheduleDelayedWork is only i
| |
108 } | 121 } |
109 | 122 |
110 void MessagePumpMojo::DoRunLoop(RunState* run_state, Delegate* delegate) { | 123 void MessagePumpMojo::DoRunLoop(RunState* run_state, Delegate* delegate) { |
111 bool more_work_is_plausible = true; | 124 bool more_work_is_plausible = true; |
112 for (;;) { | 125 for (;;) { |
113 const bool block = !more_work_is_plausible; | 126 const bool block = !more_work_is_plausible; |
114 DoInternalWork(*run_state, block); | 127 DoInternalWork(*run_state, block); |
115 | 128 |
116 // There isn't a good way to know if there are more handles ready, we assume | 129 // There isn't a good way to know if there are more handles ready, we assume |
117 // not. | 130 // not. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 if (!i->second.deadline.is_null() && i->second.deadline < now && | 192 if (!i->second.deadline.is_null() && i->second.deadline < now && |
180 handlers_.find(i->first) != handlers_.end() && | 193 handlers_.find(i->first) != handlers_.end() && |
181 handlers_[i->first].id == i->second.id) { | 194 handlers_[i->first].id == i->second.id) { |
182 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); | 195 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); |
183 } | 196 } |
184 } | 197 } |
185 } | 198 } |
186 | 199 |
187 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { | 200 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { |
188 // TODO(sky): deal with control pipe going bad. | 201 // TODO(sky): deal with control pipe going bad. |
189 for (size_t i = 1; i < wait_state.handles.size(); ++i) { | 202 for (size_t i = 0; i < wait_state.handles.size(); ++i) { |
190 const MojoResult result = | 203 const MojoResult result = |
191 Wait(wait_state.handles[i], wait_state.wait_signals[i], 0); | 204 Wait(wait_state.handles[i], wait_state.wait_signals[i], 0); |
192 if (result == MOJO_RESULT_INVALID_ARGUMENT) { | 205 if (result == MOJO_RESULT_INVALID_ARGUMENT) { |
193 // We should never have an invalid argument. If we do it indicates | 206 // We should never have an invalid argument. If we do it indicates |
194 // RemoveHandler() was not invoked and is likely to cause problems else | 207 // RemoveHandler() was not invoked and is likely to cause problems else |
195 // where in the stack if we ignore it. | 208 // where in the stack if we ignore it. |
196 CHECK(false); | 209 CHECK(false); |
197 } else if (result == MOJO_RESULT_FAILED_PRECONDITION || | 210 } else if (result == MOJO_RESULT_FAILED_PRECONDITION || |
198 result == MOJO_RESULT_CANCELLED) { | 211 result == MOJO_RESULT_CANCELLED) { |
212 CHECK_NE(i, 0u); // Indicates the control pipe went bad. | |
213 | |
199 // Remove the handle first, this way if OnHandleError() tries to remove | 214 // Remove the handle first, this way if OnHandleError() tries to remove |
200 // the handle our iterator isn't invalidated. | 215 // the handle our iterator isn't invalidated. |
201 DCHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); | 216 CHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); |
202 MessagePumpMojoHandler* handler = | 217 MessagePumpMojoHandler* handler = |
203 handlers_[wait_state.handles[i]].handler; | 218 handlers_[wait_state.handles[i]].handler; |
204 handlers_.erase(wait_state.handles[i]); | 219 handlers_.erase(wait_state.handles[i]); |
205 handler->OnHandleError(wait_state.handles[i], result); | 220 handler->OnHandleError(wait_state.handles[i], result); |
206 return; | 221 return; |
207 } | 222 } |
208 } | 223 } |
209 } | 224 } |
210 | 225 |
211 void MessagePumpMojo::SignalControlPipe(const RunState& run_state) { | 226 void MessagePumpMojo::SignalControlPipe(const RunState& run_state) { |
212 // TODO(sky): deal with error? | 227 const MojoResult result = |
213 WriteMessageRaw(run_state.write_handle.get(), NULL, 0, NULL, 0, | 228 WriteMessageRaw(run_state.write_handle.get(), NULL, 0, NULL, 0, |
214 MOJO_WRITE_MESSAGE_FLAG_NONE); | 229 MOJO_WRITE_MESSAGE_FLAG_NONE); |
230 // If we can't write we likely won't wake up the thread and there is a strong | |
231 // chance we'll deadlock. | |
232 CHECK_EQ(MOJO_RESULT_OK, result); | |
215 } | 233 } |
216 | 234 |
217 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( | 235 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( |
218 const RunState& run_state) const { | 236 const RunState& run_state) const { |
219 WaitState wait_state; | 237 WaitState wait_state; |
220 wait_state.handles.push_back(run_state.read_handle.get()); | 238 wait_state.handles.push_back(run_state.read_handle.get()); |
221 wait_state.wait_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); | 239 wait_state.wait_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); |
222 | 240 |
223 for (HandleToHandler::const_iterator i = handlers_.begin(); | 241 for (HandleToHandler::const_iterator i = handlers_.begin(); |
224 i != handlers_.end(); ++i) { | 242 i != handlers_.end(); ++i) { |
225 wait_state.handles.push_back(i->first); | 243 wait_state.handles.push_back(i->first); |
226 wait_state.wait_signals.push_back(i->second.wait_signals); | 244 wait_state.wait_signals.push_back(i->second.wait_signals); |
227 } | 245 } |
228 return wait_state; | 246 return wait_state; |
229 } | 247 } |
230 | 248 |
231 MojoDeadline MessagePumpMojo::GetDeadlineForWait( | 249 MojoDeadline MessagePumpMojo::GetDeadlineForWait( |
232 const RunState& run_state) const { | 250 const RunState& run_state) const { |
233 base::TimeTicks min_time = run_state.delayed_work_time; | 251 const base::TimeTicks now(internal::NowTicks()); |
252 MojoDeadline deadline = static_cast<MojoDeadline>(0); | |
253 if (!run_state.delayed_work_time.is_null()) | |
254 deadline = TimeTicksToMojoDeadline(run_state.delayed_work_time, now); | |
234 for (HandleToHandler::const_iterator i = handlers_.begin(); | 255 for (HandleToHandler::const_iterator i = handlers_.begin(); |
235 i != handlers_.end(); ++i) { | 256 i != handlers_.end(); ++i) { |
236 if (min_time.is_null() && i->second.deadline < min_time) | 257 deadline = std::min( |
237 min_time = i->second.deadline; | 258 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); |
238 } | 259 } |
239 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : | 260 return deadline; |
240 std::max(static_cast<MojoDeadline>(0), | |
241 static_cast<MojoDeadline>( | |
242 (min_time - internal::NowTicks()).InMicroseconds())); | |
243 } | 261 } |
244 | 262 |
245 } // namespace common | 263 } // namespace common |
246 } // namespace mojo | 264 } // namespace mojo |
OLD | NEW |