license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_sync_channel.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 7 | #include "base/lazy_instance.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 9 | #include "base/thread_local.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 10 | #include "base/message_loop.h" |
| 11 | #include "base/waitable_event.h" |
| 12 | #include "base/waitable_event_watcher.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 13 | #include "ipc/ipc_sync_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 15 | using base::TimeDelta; |
| 16 | using base::TimeTicks; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 17 | using base::WaitableEvent; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
| 19 | namespace IPC { |
| 20 | // When we're blocked in a Send(), we need to process incoming synchronous |
| 21 | // messages right away because it could be blocking our reply (either |
| 22 | // directly from the same object we're calling, or indirectly through one or |
| 23 | // more other channels). That means that in SyncContext's OnMessageReceived, |
| 24 | // we need to process sync message right away if we're blocked. However a |
| 25 | // simple check isn't sufficient, because the listener thread can be in the |
| 26 | // process of calling Send. |
| 27 | // To work around this, when SyncChannel filters a sync message, it sets |
| 28 | // an event that the listener thread waits on during its Send() call. This |
| 29 | // allows us to dispatch incoming sync messages when blocked. The race |
| 30 | // condition is handled because if Send is in the process of being called, it |
| 31 | // will check the event. In case the listener thread isn't sending a message, |
| 32 | // we queue a task on the listener thread to dispatch the received messages. |
| 33 | // The messages are stored in this queue object that's shared among all |
| 34 | // SyncChannel objects on the same thread (since one object can receive a |
| 35 | // sync message while another one is blocked). |
| 36 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | class SyncChannel::ReceivedSyncMsgQueue : |
| 38 | public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> { |
| 39 | public: |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 40 | // Returns the ReceivedSyncMsgQueue instance for this thread, creating one |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 41 | // if necessary. Call RemoveContext on the same thread when done. |
| 42 | static ReceivedSyncMsgQueue* AddContext() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 43 | // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple |
| 44 | // SyncChannel objects can block the same thread). |
| 45 | ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get(); |
| 46 | if (!rv) { |
| 47 | rv = new ReceivedSyncMsgQueue(); |
| 48 | ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv); |
| 49 | } |
| 50 | rv->listener_count_++; |
| 51 | return rv; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | } |
| 53 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | // Called on IPC thread when a synchronous message or reply arrives. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 55 | void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | bool was_task_pending; |
| 57 | { |
| 58 | AutoLock auto_lock(message_lock_); |
| 59 | |
| 60 | was_task_pending = task_pending_; |
| 61 | task_pending_ = true; |
| 62 | |
| 63 | // We set the event in case the listener thread is blocked (or is about |
| 64 | // to). In case it's not, the PostTask dispatches the messages. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 65 | message_queue_.push_back(QueuedMessage(new Message(msg), context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 68 | dispatch_event_.Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | if (!was_task_pending) { |
| 70 | listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 71 | this, &ReceivedSyncMsgQueue::DispatchMessagesTask)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void QueueReply(const Message &msg, SyncChannel::SyncContext* context) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 76 | received_replies_.push_back(QueuedMessage(new Message(msg), context)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 79 | // Called on the listener's thread to process any queues synchronous |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | // messages. |
| 81 | void DispatchMessagesTask() { |
| 82 | { |
| 83 | AutoLock auto_lock(message_lock_); |
| 84 | task_pending_ = false; |
| 85 | } |
| 86 | DispatchMessages(); |
| 87 | } |
| 88 | |
| 89 | void DispatchMessages() { |
| 90 | while (true) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 91 | Message* message; |
| 92 | scoped_refptr<SyncChannel::SyncContext> context; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | { |
| 94 | AutoLock auto_lock(message_lock_); |
| 95 | if (message_queue_.empty()) |
| 96 | break; |
| 97 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 98 | message = message_queue_.front().message; |
| 99 | context = message_queue_.front().context; |
| 100 | message_queue_.pop_front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 827ab81 | 2009-03-12 07:17:17 | [diff] [blame] | 103 | context->OnDispatchMessage(*message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | delete message; |
| 105 | } |
| 106 | } |
| 107 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | // SyncChannel calls this in its destructor. |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 109 | void RemoveContext(SyncContext* context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | AutoLock auto_lock(message_lock_); |
| 111 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 112 | SyncMessageQueue::iterator iter = message_queue_.begin(); |
| 113 | while (iter != message_queue_.end()) { |
| 114 | if (iter->context == context) { |
| 115 | delete iter->message; |
| 116 | iter = message_queue_.erase(iter); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | } else { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 118 | iter++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 121 | |
| 122 | if (--listener_count_ == 0) { |
| 123 | DCHECK(lazy_tls_ptr_.Pointer()->Get()); |
| 124 | lazy_tls_ptr_.Pointer()->Set(NULL); |
| 125 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | } |
| 127 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 128 | WaitableEvent* dispatch_event() { return &dispatch_event_; } |
[email protected] | 8fd8de9 | 2008-08-12 23:50:30 | [diff] [blame] | 129 | MessageLoop* listener_message_loop() { return listener_message_loop_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 131 | // Holds a pointer to the per-thread ReceivedSyncMsgQueue object. |
| 132 | static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> > |
| 133 | lazy_tls_ptr_; |
| 134 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | // Called on the ipc thread to check if we can unblock any current Send() |
| 136 | // calls based on a queued reply. |
| 137 | void DispatchReplies() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | for (size_t i = 0; i < received_replies_.size(); ++i) { |
| 139 | Message* message = received_replies_[i].message; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 140 | if (received_replies_[i].context->TryToUnblockListener(message)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | delete message; |
| 142 | received_replies_.erase(received_replies_.begin() + i); |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 148 | base::WaitableEventWatcher* top_send_done_watcher() { |
| 149 | return top_send_done_watcher_; |
| 150 | } |
| 151 | |
| 152 | void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) { |
| 153 | top_send_done_watcher_ = watcher; |
| 154 | } |
| 155 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 156 | private: |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 157 | friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>; |
| 158 | |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 159 | // See the comment in SyncChannel::SyncChannel for why this event is created |
| 160 | // as manual reset. |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 161 | ReceivedSyncMsgQueue() : |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 162 | dispatch_event_(true, false), |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 163 | listener_message_loop_(MessageLoop::current()), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 164 | task_pending_(false), |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 165 | listener_count_(0), |
| 166 | top_send_done_watcher_(NULL) { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 877d55d | 2009-11-05 21:53:08 | [diff] [blame] | 169 | ~ReceivedSyncMsgQueue() {} |
| 170 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 171 | // Holds information about a queued synchronous message or reply. |
| 172 | struct QueuedMessage { |
| 173 | QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | Message* message; |
| 175 | scoped_refptr<SyncChannel::SyncContext> context; |
| 176 | }; |
| 177 | |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 178 | typedef std::deque<QueuedMessage> SyncMessageQueue; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 179 | SyncMessageQueue message_queue_; |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 180 | |
| 181 | std::vector<QueuedMessage> received_replies_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 182 | |
| 183 | // Set when we got a synchronous message that we must respond to as the |
| 184 | // sender needs its reply before it can reply to our original synchronous |
| 185 | // message. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 186 | WaitableEvent dispatch_event_; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 187 | MessageLoop* listener_message_loop_; |
| 188 | Lock message_lock_; |
| 189 | bool task_pending_; |
| 190 | int listener_count_; |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 191 | |
| 192 | // The current send done event watcher for this thread. Used to maintain |
| 193 | // a local global stack of send done watchers to ensure that nested sync |
| 194 | // message loops complete correctly. |
| 195 | base::WaitableEventWatcher* top_send_done_watcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | }; |
| 197 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 198 | base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> > |
| 199 | SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_(base::LINKER_INITIALIZED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | |
| 201 | SyncChannel::SyncContext::SyncContext( |
| 202 | Channel::Listener* listener, |
| 203 | MessageFilter* filter, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 204 | MessageLoop* ipc_thread, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 205 | WaitableEvent* shutdown_event) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | : ChannelProxy::Context(listener, filter, ipc_thread), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 207 | received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()), |
| 208 | shutdown_event_(shutdown_event) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | SyncChannel::SyncContext::~SyncContext() { |
| 212 | while (!deserializers_.empty()) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 213 | Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Adds information about an outgoing sync message to the context so that |
| 217 | // we know how to deserialize the reply. Returns a handle that's set when |
| 218 | // the reply has arrived. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 219 | void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 220 | // The event is created as manual reset because in between Signal and |
[email protected] | 4df10d61 | 2008-11-12 00:38:26 | [diff] [blame] | 221 | // OnObjectSignalled, another Send can happen which would stop the watcher |
| 222 | // from being called. The event would get watched later, when the nested |
| 223 | // Send completes, so the event will need to remain set. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 224 | PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | sync_msg->GetReplyDeserializer(), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 226 | new WaitableEvent(true, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | AutoLock auto_lock(deserializers_lock_); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 228 | deserializers_.push_back(pending); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 231 | bool SyncChannel::SyncContext::Pop() { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 232 | bool result; |
| 233 | { |
| 234 | AutoLock auto_lock(deserializers_lock_); |
| 235 | PendingSyncMsg msg = deserializers_.back(); |
| 236 | delete msg.deserializer; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 237 | delete msg.done_event; |
| 238 | msg.done_event = NULL; |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 239 | deserializers_.pop_back(); |
| 240 | result = msg.send_result; |
| 241 | } |
| 242 | |
| 243 | // We got a reply to a synchronous Send() call that's blocking the listener |
| 244 | // thread. However, further down the call stack there could be another |
| 245 | // blocking Send() call, whose reply we received after we made this last |
| 246 | // Send() call. So check if we have any queued replies available that |
| 247 | // can now unblock the listener thread. |
| 248 | ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
| 249 | received_sync_msgs_.get(), &ReceivedSyncMsgQueue::DispatchReplies)); |
| 250 | |
| 251 | return result; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 254 | WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 255 | AutoLock auto_lock(deserializers_lock_); |
| 256 | return deserializers_.back().done_event; |
| 257 | } |
| 258 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 259 | WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 260 | return received_sync_msgs_->dispatch_event(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void SyncChannel::SyncContext::DispatchMessages() { |
| 264 | received_sync_msgs_->DispatchMessages(); |
| 265 | } |
| 266 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 267 | bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) { |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 268 | AutoLock auto_lock(deserializers_lock_); |
| 269 | if (deserializers_.empty() || |
| 270 | !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) { |
| 271 | return false; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 272 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | |
[email protected] | 63a7bb8 | 2008-10-25 00:46:00 | [diff] [blame] | 274 | if (!msg->is_reply_error()) { |
| 275 | deserializers_.back().send_result = deserializers_.back().deserializer-> |
| 276 | SerializeOutputParameters(*msg); |
| 277 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 278 | deserializers_.back().done_event->Signal(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 280 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 283 | void SyncChannel::SyncContext::Clear() { |
| 284 | CancelPendingSends(); |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 285 | received_sync_msgs_->RemoveContext(this); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 286 | Context::Clear(); |
| 287 | } |
| 288 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | void SyncChannel::SyncContext::OnMessageReceived(const Message& msg) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 290 | // Give the filters a chance at processing this message. |
| 291 | if (TryFilters(msg)) |
| 292 | return; |
| 293 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 294 | if (TryToUnblockListener(&msg)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | return; |
| 296 | |
| 297 | if (msg.should_unblock()) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 298 | received_sync_msgs_->QueueMessage(msg, this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (msg.is_reply()) { |
| 303 | received_sync_msgs_->QueueReply(msg, this); |
| 304 | return; |
| 305 | } |
| 306 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 307 | return Context::OnMessageReceivedNoFilter(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | } |
| 309 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | void SyncChannel::SyncContext::OnChannelError() { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 311 | CancelPendingSends(); |
[email protected] | a4f82270 | 2009-02-06 00:44:53 | [diff] [blame] | 312 | shutdown_watcher_.StopWatching(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | Context::OnChannelError(); |
| 314 | } |
| 315 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 316 | void SyncChannel::SyncContext::OnChannelOpened() { |
| 317 | shutdown_watcher_.StartWatching(shutdown_event_, this); |
| 318 | Context::OnChannelOpened(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | } |
| 320 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 321 | void SyncChannel::SyncContext::OnChannelClosed() { |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame^] | 322 | CancelPendingSends(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 323 | shutdown_watcher_.StopWatching(); |
| 324 | Context::OnChannelClosed(); |
| 325 | } |
| 326 | |
| 327 | void SyncChannel::SyncContext::OnSendTimeout(int message_id) { |
| 328 | AutoLock auto_lock(deserializers_lock_); |
| 329 | PendingSyncMessageQueue::iterator iter; |
| 330 | for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) { |
[email protected] | d3ae7a07 | 2008-12-05 20:27:20 | [diff] [blame] | 331 | if (iter->id == message_id) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 332 | iter->done_event->Signal(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void SyncChannel::SyncContext::CancelPendingSends() { |
| 339 | AutoLock auto_lock(deserializers_lock_); |
| 340 | PendingSyncMessageQueue::iterator iter; |
| 341 | for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 342 | iter->done_event->Signal(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 345 | void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 346 | if (event == shutdown_event_) { |
| 347 | // Process shut down before we can get a reply to a synchronous message. |
| 348 | // Cancel pending Send calls, which will end up setting the send done event. |
| 349 | CancelPendingSends(); |
| 350 | } else { |
| 351 | // We got the reply, timed out or the process shutdown. |
| 352 | DCHECK(event == GetSendDoneEvent()); |
[email protected] | 781a7ed | 2010-02-23 07:12:22 | [diff] [blame] | 353 | MessageLoop::current()->QuitNow(); |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 354 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
| 358 | SyncChannel::SyncChannel( |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 359 | const std::string& channel_id, Channel::Mode mode, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 360 | Channel::Listener* listener, MessageFilter* filter, |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 361 | MessageLoop* ipc_message_loop, bool create_pipe_now, |
| 362 | WaitableEvent* shutdown_event) |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 363 | : ChannelProxy( |
| 364 | channel_id, mode, ipc_message_loop, |
| 365 | new SyncContext(listener, filter, ipc_message_loop, shutdown_event), |
| 366 | create_pipe_now), |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 367 | sync_messages_with_no_timeout_allowed_(true) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 368 | // Ideally we only want to watch this object when running a nested message |
| 369 | // loop. However, we don't know when it exits if there's another nested |
| 370 | // message loop running under it or not, so we wouldn't know whether to |
| 371 | // stop or keep watching. So we always watch it, and create the event as |
| 372 | // manual reset since the object watcher might otherwise reset the event |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 373 | // when we're doing a WaitMany. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 374 | dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | SyncChannel::~SyncChannel() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 380 | bool SyncChannel::Send(Message* message) { |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 381 | return SendWithTimeout(message, base::kNoTimeout); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 384 | bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) { |
| 385 | if (!message->is_sync()) { |
| 386 | ChannelProxy::Send(message); |
| 387 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 390 | // *this* might get deleted in WaitForReply. |
| 391 | scoped_refptr<SyncContext> context(sync_context()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 392 | if (context->shutdown_event()->IsSignaled()) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 393 | delete message; |
| 394 | return false; |
| 395 | } |
| 396 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 397 | DCHECK(sync_messages_with_no_timeout_allowed_ || |
| 398 | timeout_ms != base::kNoTimeout); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 399 | SyncMessage* sync_msg = static_cast<SyncMessage*>(message); |
| 400 | context->Push(sync_msg); |
| 401 | int message_id = SyncMessage::GetMessageId(*sync_msg); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 402 | WaitableEvent* pump_messages_event = sync_msg->pump_messages_event(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 403 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | ChannelProxy::Send(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | |
[email protected] | aa96ae77 | 2009-01-20 22:08:15 | [diff] [blame] | 406 | if (timeout_ms != base::kNoTimeout) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 407 | // We use the sync message id so that when a message times out, we don't |
| 408 | // confuse it with another send that is either above/below this Send in |
| 409 | // the call stack. |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 410 | context->ipc_message_loop()->PostDelayedTask(FROM_HERE, |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 411 | NewRunnableMethod(context.get(), |
| 412 | &SyncContext::OnSendTimeout, message_id), timeout_ms); |
| 413 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 414 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 415 | // Wait for reply, or for any other incoming synchronous messages. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 416 | // *this* might get deleted, so only call static functions at this point. |
| 417 | WaitForReply(context, pump_messages_event); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 418 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 419 | return context->Pop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 422 | void SyncChannel::WaitForReply( |
| 423 | SyncContext* context, WaitableEvent* pump_messages_event) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 424 | while (true) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 425 | WaitableEvent* objects[] = { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 426 | context->GetDispatchEvent(), |
| 427 | context->GetSendDoneEvent(), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 428 | pump_messages_event |
| 429 | }; |
| 430 | |
| 431 | unsigned count = pump_messages_event ? 3: 2; |
[email protected] | 7dc8d79 | 2009-11-20 17:30:44 | [diff] [blame] | 432 | size_t result = WaitableEvent::WaitMany(objects, count); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 433 | if (result == 0 /* dispatch event */) { |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 434 | // We're waiting for a reply, but we received a blocking synchronous |
| 435 | // call. We must process it or otherwise a deadlock might occur. |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 436 | context->GetDispatchEvent()->Reset(); |
| 437 | context->DispatchMessages(); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 441 | if (result == 2 /* pump_messages_event */) |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 442 | WaitForReplyWithNestedMessageLoop(context); // Run a nested message loop. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 443 | |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 448 | void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 449 | base::WaitableEventWatcher send_done_watcher; |
| 450 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 451 | ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 452 | DCHECK(sync_msg_queue != NULL); |
| 453 | |
| 454 | base::WaitableEventWatcher* old_send_done_event_watcher = |
| 455 | sync_msg_queue->top_send_done_watcher(); |
| 456 | |
| 457 | base::WaitableEventWatcher::Delegate* old_delegate = NULL; |
| 458 | base::WaitableEvent* old_event = NULL; |
| 459 | |
| 460 | // Maintain a local global stack of send done delegates to ensure that |
| 461 | // nested sync calls complete in the correct sequence, i.e. the |
| 462 | // outermost call completes first, etc. |
| 463 | if (old_send_done_event_watcher) { |
| 464 | old_delegate = old_send_done_event_watcher->delegate(); |
| 465 | old_event = old_send_done_event_watcher->GetWatchedEvent(); |
| 466 | old_send_done_event_watcher->StopWatching(); |
| 467 | } |
| 468 | |
| 469 | sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); |
| 470 | |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 471 | send_done_watcher.StartWatching(context->GetSendDoneEvent(), context); |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 472 | bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 473 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 474 | MessageLoop::current()->SetNestableTasksAllowed(true); |
| 475 | MessageLoop::current()->Run(); |
| 476 | MessageLoop::current()->SetNestableTasksAllowed(old_state); |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 477 | |
| 478 | sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); |
[email protected] | 42437980 | 2009-10-14 19:58:13 | [diff] [blame] | 479 | if (old_send_done_event_watcher && old_event) { |
[email protected] | ac0efda | 2009-10-14 16:22:02 | [diff] [blame] | 480 | old_send_done_event_watcher->StartWatching(old_event, old_delegate); |
| 481 | } |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 484 | void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { |
[email protected] | 9eec225 | 2009-12-01 02:34:18 | [diff] [blame] | 485 | DCHECK(event == sync_context()->GetDispatchEvent()); |
| 486 | // The call to DispatchMessages might delete this object, so reregister |
| 487 | // the object watcher first. |
| 488 | event->Reset(); |
| 489 | dispatch_watcher_.StartWatching(event, this); |
| 490 | sync_context()->DispatchMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | } // namespace IPC |