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