| 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/message_pump/message_pump_mojo.h" | 5 #include "mojo/message_pump/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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 const int64_t delta = (time_ticks - now).InMicroseconds(); | 31 const int64_t delta = (time_ticks - now).InMicroseconds(); |
| 32 return delta < 0 ? static_cast<MojoDeadline>(0) : | 32 return delta < 0 ? static_cast<MojoDeadline>(0) : |
| 33 static_cast<MojoDeadline>(delta); | 33 static_cast<MojoDeadline>(delta); |
| 34 } | 34 } |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 // State needed for one iteration of WaitMany. The first handle and flags | 38 // State needed for one iteration of WaitMany. The first handle and flags |
| 39 // corresponds to that of the control pipe. | 39 // corresponds to that of the control pipe. |
| 40 struct MessagePumpMojo::WaitState { | 40 struct MessagePumpMojo::WaitState { |
| 41 // This uses individual vectors for better use with WaitMany(). |
| 41 std::vector<Handle> handles; | 42 std::vector<Handle> handles; |
| 42 std::vector<MojoHandleSignals> wait_signals; | 43 std::vector<MojoHandleSignals> wait_signals; |
| 44 std::vector<int> locations; |
| 43 }; | 45 }; |
| 44 | 46 |
| 45 struct MessagePumpMojo::RunState { | 47 struct MessagePumpMojo::RunState { |
| 46 RunState() : should_quit(false) { | 48 RunState() : should_quit(false) { |
| 47 CreateMessagePipe(NULL, &read_handle, &write_handle); | 49 CreateMessagePipe(NULL, &read_handle, &write_handle); |
| 48 } | 50 } |
| 49 | 51 |
| 50 base::TimeTicks delayed_work_time; | 52 base::TimeTicks delayed_work_time; |
| 51 | 53 |
| 52 // Used to wake up WaitForWork(). | 54 // Used to wake up WaitForWork(). |
| (...skipping 17 matching lines...) Expand all Loading... |
| 70 // static | 72 // static |
| 71 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { | 73 scoped_ptr<base::MessagePump> MessagePumpMojo::Create() { |
| 72 return scoped_ptr<MessagePump>(new MessagePumpMojo()); | 74 return scoped_ptr<MessagePump>(new MessagePumpMojo()); |
| 73 } | 75 } |
| 74 | 76 |
| 75 // static | 77 // static |
| 76 MessagePumpMojo* MessagePumpMojo::current() { | 78 MessagePumpMojo* MessagePumpMojo::current() { |
| 77 return g_tls_current_pump.Pointer()->Get(); | 79 return g_tls_current_pump.Pointer()->Get(); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void MessagePumpMojo::AddHandler(MessagePumpMojoHandler* handler, | 82 void MessagePumpMojo::AddHandler(int location, |
| 83 MessagePumpMojoHandler* handler, |
| 81 const Handle& handle, | 84 const Handle& handle, |
| 82 MojoHandleSignals wait_signals, | 85 MojoHandleSignals wait_signals, |
| 83 base::TimeTicks deadline) { | 86 base::TimeTicks deadline) { |
| 84 CHECK(handler); | 87 CHECK(handler); |
| 85 DCHECK(handle.is_valid()); | 88 DCHECK(handle.is_valid()); |
| 86 // Assume it's an error if someone tries to reregister an existing handle. | 89 // Assume it's an error if someone tries to reregister an existing handle. |
| 87 CHECK_EQ(0u, handlers_.count(handle)); | 90 CHECK_EQ(0u, handlers_.count(handle)); |
| 88 Handler handler_data; | 91 Handler handler_data; |
| 92 handler_data.location = location; |
| 89 handler_data.handler = handler; | 93 handler_data.handler = handler; |
| 90 handler_data.wait_signals = wait_signals; | 94 handler_data.wait_signals = wait_signals; |
| 91 handler_data.deadline = deadline; | 95 handler_data.deadline = deadline; |
| 92 handler_data.id = next_handler_id_++; | 96 handler_data.id = next_handler_id_++; |
| 93 handlers_[handle] = handler_data; | 97 handlers_[handle] = handler_data; |
| 94 } | 98 } |
| 95 | 99 |
| 96 void MessagePumpMojo::RemoveHandler(const Handle& handle) { | 100 void MessagePumpMojo::RemoveHandler(const Handle& handle) { |
| 97 handlers_.erase(handle); | 101 handlers_.erase(handle); |
| 98 } | 102 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 switch (result) { | 199 switch (result) { |
| 196 case MOJO_RESULT_CANCELLED: | 200 case MOJO_RESULT_CANCELLED: |
| 197 case MOJO_RESULT_FAILED_PRECONDITION: | 201 case MOJO_RESULT_FAILED_PRECONDITION: |
| 198 RemoveInvalidHandle(wait_state, result, wait_many_result.index); | 202 RemoveInvalidHandle(wait_state, result, wait_many_result.index); |
| 199 break; | 203 break; |
| 200 case MOJO_RESULT_DEADLINE_EXCEEDED: | 204 case MOJO_RESULT_DEADLINE_EXCEEDED: |
| 201 did_work = false; | 205 did_work = false; |
| 202 break; | 206 break; |
| 203 default: | 207 default: |
| 204 base::debug::Alias(&result); | 208 base::debug::Alias(&result); |
| 209 base::debug::Alias(&wait_many_result.index); |
| 210 if (wait_many_result.IsIndexValid()) { |
| 211 const int location = wait_state.locations[wait_many_result.index]; |
| 212 base::debug::Alias(&location); |
| 213 } |
| 214 |
| 205 // Unexpected result is likely fatal, crash so we can determine cause. | 215 // Unexpected result is likely fatal, crash so we can determine cause. |
| 206 CHECK(false); | 216 CHECK(false); |
| 207 } | 217 } |
| 208 } | 218 } |
| 209 | 219 |
| 210 // Notify and remove any handlers whose time has expired. Make a copy in case | 220 // Notify and remove any handlers whose time has expired. Make a copy in case |
| 211 // someone tries to add/remove new handlers from notification. | 221 // someone tries to add/remove new handlers from notification. |
| 212 const HandleToHandler cloned_handlers(handlers_); | 222 const HandleToHandler cloned_handlers(handlers_); |
| 213 const base::TimeTicks now(internal::NowTicks()); | 223 const base::TimeTicks now(internal::NowTicks()); |
| 214 for (HandleToHandler::const_iterator i = cloned_handlers.begin(); | 224 for (HandleToHandler::const_iterator i = cloned_handlers.begin(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 // If we can't write we likely won't wake up the thread and there is a strong | 264 // If we can't write we likely won't wake up the thread and there is a strong |
| 255 // chance we'll deadlock. | 265 // chance we'll deadlock. |
| 256 CHECK_EQ(MOJO_RESULT_OK, result); | 266 CHECK_EQ(MOJO_RESULT_OK, result); |
| 257 } | 267 } |
| 258 | 268 |
| 259 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( | 269 MessagePumpMojo::WaitState MessagePumpMojo::GetWaitState( |
| 260 const RunState& run_state) const { | 270 const RunState& run_state) const { |
| 261 WaitState wait_state; | 271 WaitState wait_state; |
| 262 wait_state.handles.push_back(run_state.read_handle.get()); | 272 wait_state.handles.push_back(run_state.read_handle.get()); |
| 263 wait_state.wait_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); | 273 wait_state.wait_signals.push_back(MOJO_HANDLE_SIGNAL_READABLE); |
| 274 wait_state.locations.push_back(6); |
| 264 | 275 |
| 265 for (HandleToHandler::const_iterator i = handlers_.begin(); | 276 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 266 i != handlers_.end(); ++i) { | 277 i != handlers_.end(); ++i) { |
| 267 wait_state.handles.push_back(i->first); | 278 wait_state.handles.push_back(i->first); |
| 268 wait_state.wait_signals.push_back(i->second.wait_signals); | 279 wait_state.wait_signals.push_back(i->second.wait_signals); |
| 280 wait_state.locations.push_back(i->second.location); |
| 269 } | 281 } |
| 270 return wait_state; | 282 return wait_state; |
| 271 } | 283 } |
| 272 | 284 |
| 273 MojoDeadline MessagePumpMojo::GetDeadlineForWait( | 285 MojoDeadline MessagePumpMojo::GetDeadlineForWait( |
| 274 const RunState& run_state) const { | 286 const RunState& run_state) const { |
| 275 const base::TimeTicks now(internal::NowTicks()); | 287 const base::TimeTicks now(internal::NowTicks()); |
| 276 MojoDeadline deadline = TimeTicksToMojoDeadline(run_state.delayed_work_time, | 288 MojoDeadline deadline = TimeTicksToMojoDeadline(run_state.delayed_work_time, |
| 277 now); | 289 now); |
| 278 for (HandleToHandler::const_iterator i = handlers_.begin(); | 290 for (HandleToHandler::const_iterator i = handlers_.begin(); |
| 279 i != handlers_.end(); ++i) { | 291 i != handlers_.end(); ++i) { |
| 280 deadline = std::min( | 292 deadline = std::min( |
| 281 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); | 293 TimeTicksToMojoDeadline(i->second.deadline, now), deadline); |
| 282 } | 294 } |
| 283 return deadline; | 295 return deadline; |
| 284 } | 296 } |
| 285 | 297 |
| 286 void MessagePumpMojo::WillSignalHandler() { | 298 void MessagePumpMojo::WillSignalHandler() { |
| 287 FOR_EACH_OBSERVER(Observer, observers_, WillSignalHandler()); | 299 FOR_EACH_OBSERVER(Observer, observers_, WillSignalHandler()); |
| 288 } | 300 } |
| 289 | 301 |
| 290 void MessagePumpMojo::DidSignalHandler() { | 302 void MessagePumpMojo::DidSignalHandler() { |
| 291 FOR_EACH_OBSERVER(Observer, observers_, DidSignalHandler()); | 303 FOR_EACH_OBSERVER(Observer, observers_, DidSignalHandler()); |
| 292 } | 304 } |
| 293 | 305 |
| 294 } // namespace common | 306 } // namespace common |
| 295 } // namespace mojo | 307 } // namespace mojo |
| OLD | NEW |