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