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