| 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 | |
| 31 | 18 |
| 32 // State needed for one iteration of WaitMany. The first handle and flags | 19 // State needed for one iteration of WaitMany. The first handle and flags |
| 33 // corresponds to that of the control pipe. | 20 // corresponds to that of the control pipe. |
| 34 struct MessagePumpMojo::WaitState { | 21 struct MessagePumpMojo::WaitState { |
| 35 std::vector<Handle> handles; | 22 std::vector<Handle> handles; |
| 36 std::vector<MojoHandleSignals> wait_signals; | 23 std::vector<MojoHandleSignals> wait_signals; |
| 37 }; | 24 }; |
| 38 | 25 |
| 39 struct MessagePumpMojo::RunState { | 26 struct MessagePumpMojo::RunState { |
| 40 RunState() : should_quit(false) { | 27 RunState() : should_quit(false) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 58 | 45 |
| 59 // static | 46 // static |
| 60 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 47 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
| 61 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 48 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
| 62 } | 49 } |
| 63 | 50 |
| 64 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 51 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, |
| 65 const Handle& handle, | 52 const Handle& handle, |
| 66 MojoHandleSignals wait_signals, | 53 MojoHandleSignals wait_signals, |
| 67 base::TimeTicks deadline) { | 54 base::TimeTicks deadline) { |
| 68 CHECK(handler); | 55 DCHECK(handler); |
| 69 DCHECK(handle.is_valid()); | 56 DCHECK(handle.is_valid()); |
| 70 // Assume it's an error if someone tries to reregister an existing handle. | 57 // Assume it's an error if someone tries to reregister an existing handle. |
| 71 CHECK_EQ(0u, handlers_.count(handle)); | 58 DCHECK_EQ(0u, handlers_.count(handle)); |
| 72 Handler handler_data; | 59 Handler handler_data; |
| 73 handler_data.handler = handler; | 60 handler_data.handler = handler; |
| 74 handler_data.wait_signals = wait_signals; | 61 handler_data.wait_signals = wait_signals; |
| 75 handler_data.deadline = deadline; | 62 handler_data.deadline = deadline; |
| 76 handler_data.id = next_handler_id_++; | 63 handler_data.id = next_handler_id_++; |
| 77 handlers_[handle] = handler_data; | 64 handlers_[handle] = handler_data; |
| 78 } | 65 } |
| 79 | 66 |
| 80 void MessagePumpMojo::RemoveHandler(const Handle& handle) { | 67 void MessagePumpMojo::RemoveHandler(const Handle& handle) { |
| 81 handlers_.erase(handle); | 68 handlers_.erase(handle); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (!i->second.deadline.is_null() && i->second.deadline < now && | 179 if (!i->second.deadline.is_null() && i->second.deadline < now && |
| 193 handlers_.find(i->first) != handlers_.end() && | 180 handlers_.find(i->first) != handlers_.end() && |
| 194 handlers_[i->first].id == i->second.id) { | 181 handlers_[i->first].id == i->second.id) { |
| 195 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); | 182 i->second.handler->OnHandleError(i->first, MOJO_RESULT_DEADLINE_EXCEEDED); |
| 196 } | 183 } |
| 197 } | 184 } |
| 198 } | 185 } |
| 199 | 186 |
| 200 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { | 187 void MessagePumpMojo::RemoveFirstInvalidHandle(const WaitState& wait_state) { |
| 201 // TODO(sky): deal with control pipe going bad. | 188 // TODO(sky): deal with control pipe going bad. |
| 202 for (size_t i = 0; i < wait_state.handles.size(); ++i) { | 189 for (size_t i = 1; i < wait_state.handles.size(); ++i) { |
| 203 const MojoResult result = | 190 const MojoResult result = |
| 204 Wait(wait_state.handles[i], wait_state.wait_signals[i], 0); | 191 Wait(wait_state.handles[i], wait_state.wait_signals[i], 0); |
| 205 if (result == MOJO_RESULT_INVALID_ARGUMENT) { | 192 if (result == MOJO_RESULT_INVALID_ARGUMENT) { |
| 206 // We should never have an invalid argument. If we do it indicates | 193 // We should never have an invalid argument. If we do it indicates |
| 207 // RemoveHandler() was not invoked and is likely to cause problems else | 194 // RemoveHandler() was not invoked and is likely to cause problems else |
| 208 // where in the stack if we ignore it. | 195 // where in the stack if we ignore it. |
| 209 CHECK(false); | 196 CHECK(false); |
| 210 } else if (result == MOJO_RESULT_FAILED_PRECONDITION || | 197 } else if (result == MOJO_RESULT_FAILED_PRECONDITION || |
| 211 result == MOJO_RESULT_CANCELLED) { | 198 result == MOJO_RESULT_CANCELLED) { |
| 212 CHECK_NE(i, 0u); // Indicates the control pipe went bad. | |
| 213 | |
| 214 // Remove the handle first, this way if OnHandleError() tries to remove | 199 // Remove the handle first, this way if OnHandleError() tries to remove |
| 215 // the handle our iterator isn't invalidated. | 200 // the handle our iterator isn't invalidated. |
| 216 CHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); | 201 DCHECK(handlers_.find(wait_state.handles[i]) != handlers_.end()); |
| 217 MessagePumpMojoHandler* handler = | 202 MessagePumpMojoHandler* handler = |
| 218 handlers_[wait_state.handles[i]].handler; | 203 handlers_[wait_state.handles[i]].handler; |
| 219 handlers_.erase(wait_state.handles[i]); | 204 handlers_.erase(wait_state.handles[i]); |
| 220 handler->OnHandleError(wait_state.handles[i], result); | 205 handler->OnHandleError(wait_state.handles[i], result); |
| 221 return; | 206 return; |
| 222 } | 207 } |
| 223 } | 208 } |
| 224 } | 209 } |
| 225 | 210 |
| 226 void MessagePumpMojo::SignalControlPipe(const RunState& run_state) { | 211 void MessagePumpMojo::SignalControlPipe(const RunState& run_state) { |
| 227 const MojoResult result = | 212 // TODO(sky): deal with error? |
| 228 WriteMessageRaw(run_state.write_handle.get(), NULL, 0, NULL, 0, | 213 WriteMessageRaw(run_state.write_handle.get(), NULL, 0, NULL, 0, |
| 229 MOJO_WRITE_MESSAGE_FLAG_NONE); | 214 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); | |
| 233 } | 215 } |
| 234 | 216 |
| 235 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( | 217 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( |
| 236 const RunState& run_state) const { | 218 const RunState& run_state) const { |
| 237 WaitState wait_state; | 219 WaitState wait_state; |
| 238 wait_state.handles.push_back(run_state.read_handle.get()); | 220 wait_state.handles.push_back(run_state.read_handle.get()); |
| 239 wait_state.wait_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); | 221 wait_state.wait_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); |
| 240 | 222 |
| 241 for (HandleToHandler::const_iterator i = handlers_.begin(); | 223 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 242 i != handlers_.end(); ++i) { | 224 i != handlers_.end(); ++i) { |
| 243 wait_state.handles.push_back(i->first); | 225 wait_state.handles.push_back(i->first); |
| 244 wait_state.wait_signals.push_back(i->second.wait_signals); | 226 wait_state.wait_signals.push_back(i->second.wait_signals); |
| 245 } | 227 } |
| 246 return wait_state; | 228 return wait_state; |
| 247 } | 229 } |
| 248 | 230 |
| 249 MojoDeadline MessagePumpMojo::GetDeadlineForWait( | 231 MojoDeadline MessagePumpMojo::GetDeadlineForWait( |
| 250 const RunState& run_state) const { | 232 const RunState& run_state) const { |
| 251 const base::TimeTicks now(internal::NowTicks()); | 233 base::TimeTicks min_time = run_state.delayed_work_time; |
| 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); | |
| 255 for (HandleToHandler::const_iterator i = handlers_.begin(); | 234 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 256 i != handlers_.end(); ++i) { | 235 i != handlers_.end(); ++i) { |
| 257 deadline = std::min( | 236 if (min_time.is_null() && i->second.deadline < min_time) |
| 258 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); | 237 min_time = i->second.deadline; |
| 259 } | 238 } |
| 260 return deadline; | 239 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : |
| 240 std::max(static_cast<MojoDeadline>(0), |
| 241 static_cast<MojoDeadline>( |
| 242 (min_time - internal::NowTicks()).InMicroseconds())); |
| 261 } | 243 } |
| 262 | 244 |
| 263 } // namespace common | 245 } // namespace common |
| 264 } // namespace mojo | 246 } // namespace mojo |
| OLD | NEW |