| 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 as well as how |
| 23 // |delayed_work_time| is used. |
| 24 if (time_ticks.is_null()) |
| 25 return MOJO_DEADLINE_INDEFINITE; |
| 26 const int64_t delta = (time_ticks - now).InMicroseconds(); |
| 27 return delta < 0 ? static_cast<MojoDeadline>(0) : |
| 28 static_cast<MojoDeadline>(delta); |
| 29 } |
| 30 |
| 31 } // namespace |
| 18 | 32 |
| 19 // State needed for one iteration of WaitMany. The first handle and flags | 33 // State needed for one iteration of WaitMany. The first handle and flags |
| 20 // corresponds to that of the control pipe. | 34 // corresponds to that of the control pipe. |
| 21 struct MessagePumpMojo::WaitState { | 35 struct MessagePumpMojo::WaitState { |
| 22 std::vector<Handle> handles; | 36 std::vector<Handle> handles; |
| 23 std::vector<MojoHandleSignals> wait_signals; | 37 std::vector<MojoHandleSignals> wait_signals; |
| 24 }; | 38 }; |
| 25 | 39 |
| 26 struct MessagePumpMojo::RunState { | 40 struct MessagePumpMojo::RunState { |
| 27 RunState() : should_quit(false) { | 41 RunState() : should_quit(false) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 45 | 59 |
| 46 // static | 60 // static |
| 47 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 61 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
| 48 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 62 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
| 49 } | 63 } |
| 50 | 64 |
| 51 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 65 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, |
| 52 const Handle& handle, | 66 const Handle& handle, |
| 53 MojoHandleSignals wait_signals, | 67 MojoHandleSignals wait_signals, |
| 54 base::TimeTicks deadline) { | 68 base::TimeTicks deadline) { |
| 55 DCHECK(handler); | 69 CHECK(handler); |
| 56 DCHECK(handle.is_valid()); | 70 DCHECK(handle.is_valid()); |
| 57 // Assume it's an error if someone tries to reregister an existing handle. | 71 // Assume it's an error if someone tries to reregister an existing handle. |
| 58 DCHECK_EQ(0u, handlers_.count(handle)); | 72 CHECK_EQ(0u, handlers_.count(handle)); |
| 59 Handler handler_data; | 73 Handler handler_data; |
| 60 handler_data.handler = handler; | 74 handler_data.handler = handler; |
| 61 handler_data.wait_signals = wait_signals; | 75 handler_data.wait_signals = wait_signals; |
| 62 handler_data.deadline = deadline; | 76 handler_data.deadline = deadline; |
| 63 handler_data.id = next_handler_id_++; | 77 handler_data.id = next_handler_id_++; |
| 64 handlers_[handle] = handler_data; | 78 handlers_[handle] = handler_data; |
| 65 } | 79 } |
| 66 | 80 |
| 67 void MessagePumpMojo::RemoveHandler(const Handle& handle) { | 81 void MessagePumpMojo::RemoveHandler(const Handle& handle) { |
| 68 handlers_.erase(handle); | 82 handlers_.erase(handle); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 97 if (run_state_) | 111 if (run_state_) |
| 98 SignalControlPipe(*run_state_); | 112 SignalControlPipe(*run_state_); |
| 99 } | 113 } |
| 100 | 114 |
| 101 void MessagePumpMojo::ScheduleDelayedWork( | 115 void MessagePumpMojo::ScheduleDelayedWork( |
| 102 const base::TimeTicks& delayed_work_time) { | 116 const base::TimeTicks& delayed_work_time) { |
| 103 base::AutoLock auto_lock(run_state_lock_); | 117 base::AutoLock auto_lock(run_state_lock_); |
| 104 if (!run_state_) | 118 if (!run_state_) |
| 105 return; | 119 return; |
| 106 run_state_->delayed_work_time = delayed_work_time; | 120 run_state_->delayed_work_time = delayed_work_time; |
| 107 SignalControlPipe(*run_state_); | |
| 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 = TimeTicksToMojoDeadline(run_state.delayed_work_time, |
| 253 now); |
| 234 for (HandleToHandler::const_iterator i = handlers_.begin(); | 254 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 235 i != handlers_.end(); ++i) { | 255 i != handlers_.end(); ++i) { |
| 236 if (min_time.is_null() && i->second.deadline < min_time) | 256 deadline = std::min( |
| 237 min_time = i->second.deadline; | 257 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); |
| 238 } | 258 } |
| 239 return min_time.is_null() ? MOJO_DEADLINE_INDEFINITE : | 259 return deadline; |
| 240 std::max(static_cast<MojoDeadline>(0), | |
| 241 static_cast<MojoDeadline>( | |
| 242 (min_time - internal::NowTicks()).InMicroseconds())); | |
| 243 } | 260 } |
| 244 | 261 |
| 245 } // namespace common | 262 } // namespace common |
| 246 } // namespace mojo | 263 } // namespace mojo |
| OLD | NEW |