blob: 682286f7a57cb239b43397de701a5095ca477fa6 [file] [log] [blame]
[email protected]522cc10d2012-01-11 22:39:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#include "ipc/ipc_sync_channel.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]72b6f8e22011-11-12 21:16:417#include "base/bind.h"
[email protected]f886b7bf2008-09-10 10:54:068#include "base/lazy_instance.h"
[email protected]c62dd9d2011-09-21 18:05:419#include "base/location.h"
initial.commit09911bf2008-07-26 23:55:2910#include "base/logging.h"
[email protected]44f9c952011-01-02 06:05:3911#include "base/synchronization/waitable_event.h"
12#include "base/synchronization/waitable_event_watcher.h"
[email protected]b2432302012-07-02 21:15:5213#include "base/thread_task_runner_handle.h"
14#include "base/threading/thread_local.h"
[email protected]946d1b22009-07-22 23:57:2115#include "ipc/ipc_sync_message.h"
initial.commit09911bf2008-07-26 23:55:2916
[email protected]e1acf6f2008-10-27 20:43:3317using base::TimeDelta;
18using base::TimeTicks;
[email protected]1c4947f2009-01-15 22:25:1119using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2920
21namespace IPC {
22// When we're blocked in a Send(), we need to process incoming synchronous
23// messages right away because it could be blocking our reply (either
24// directly from the same object we're calling, or indirectly through one or
25// more other channels). That means that in SyncContext's OnMessageReceived,
26// we need to process sync message right away if we're blocked. However a
27// simple check isn't sufficient, because the listener thread can be in the
28// process of calling Send.
29// To work around this, when SyncChannel filters a sync message, it sets
30// an event that the listener thread waits on during its Send() call. This
31// allows us to dispatch incoming sync messages when blocked. The race
32// condition is handled because if Send is in the process of being called, it
33// will check the event. In case the listener thread isn't sending a message,
34// we queue a task on the listener thread to dispatch the received messages.
35// The messages are stored in this queue object that's shared among all
36// SyncChannel objects on the same thread (since one object can receive a
37// sync message while another one is blocked).
38
initial.commit09911bf2008-07-26 23:55:2939class SyncChannel::ReceivedSyncMsgQueue :
40 public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
41 public:
[email protected]3cdb7af812008-10-24 19:21:1342 // Returns the ReceivedSyncMsgQueue instance for this thread, creating one
[email protected]d3ae7a072008-12-05 20:27:2043 // if necessary. Call RemoveContext on the same thread when done.
44 static ReceivedSyncMsgQueue* AddContext() {
[email protected]3cdb7af812008-10-24 19:21:1345 // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple
46 // SyncChannel objects can block the same thread).
47 ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get();
48 if (!rv) {
49 rv = new ReceivedSyncMsgQueue();
50 ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv);
51 }
52 rv->listener_count_++;
53 return rv;
initial.commit09911bf2008-07-26 23:55:2954 }
55
initial.commit09911bf2008-07-26 23:55:2956 // Called on IPC thread when a synchronous message or reply arrives.
[email protected]d3ae7a072008-12-05 20:27:2057 void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2958 bool was_task_pending;
59 {
[email protected]20305ec2011-01-21 04:55:5260 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2961
62 was_task_pending = task_pending_;
63 task_pending_ = true;
64
65 // We set the event in case the listener thread is blocked (or is about
66 // to). In case it's not, the PostTask dispatches the messages.
[email protected]d3ae7a072008-12-05 20:27:2067 message_queue_.push_back(QueuedMessage(new Message(msg), context));
[email protected]522cc10d2012-01-11 22:39:5468 message_queue_version_++;
initial.commit09911bf2008-07-26 23:55:2969 }
70
[email protected]1c4947f2009-01-15 22:25:1171 dispatch_event_.Signal();
initial.commit09911bf2008-07-26 23:55:2972 if (!was_task_pending) {
[email protected]b2432302012-07-02 21:15:5273 listener_task_runner_->PostTask(
[email protected]72b6f8e22011-11-12 21:16:4174 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask,
75 this, scoped_refptr<SyncContext>(context)));
initial.commit09911bf2008-07-26 23:55:2976 }
77 }
78
79 void QueueReply(const Message &msg, SyncChannel::SyncContext* context) {
[email protected]d3ae7a072008-12-05 20:27:2080 received_replies_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2981 }
82
[email protected]d3ae7a072008-12-05 20:27:2083 // Called on the listener's thread to process any queues synchronous
initial.commit09911bf2008-07-26 23:55:2984 // messages.
[email protected]54af05f2011-04-08 03:38:2185 void DispatchMessagesTask(SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2986 {
[email protected]20305ec2011-01-21 04:55:5287 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2988 task_pending_ = false;
89 }
[email protected]54af05f2011-04-08 03:38:2190 context->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:2991 }
92
[email protected]54af05f2011-04-08 03:38:2193 void DispatchMessages(SyncContext* dispatching_context) {
[email protected]522cc10d2012-01-11 22:39:5494 bool first_time = true;
95 uint32 expected_version = 0;
96 SyncMessageQueue::iterator it;
initial.commit09911bf2008-07-26 23:55:2997 while (true) {
[email protected]522cc10d2012-01-11 22:39:5498 Message* message = NULL;
[email protected]d3ae7a072008-12-05 20:27:2099 scoped_refptr<SyncChannel::SyncContext> context;
initial.commit09911bf2008-07-26 23:55:29100 {
[email protected]20305ec2011-01-21 04:55:52101 base::AutoLock auto_lock(message_lock_);
[email protected]522cc10d2012-01-11 22:39:54102 if (first_time || message_queue_version_ != expected_version) {
103 it = message_queue_.begin();
104 first_time = false;
[email protected]54af05f2011-04-08 03:38:21105 }
[email protected]522cc10d2012-01-11 22:39:54106 for (; it != message_queue_.end(); it++) {
[email protected]298ee7d2012-03-30 21:29:30107 int message_group = it->context->restrict_dispatch_group();
108 if (message_group == kRestrictDispatchGroup_None ||
109 message_group == dispatching_context->restrict_dispatch_group()) {
[email protected]522cc10d2012-01-11 22:39:54110 message = it->message;
111 context = it->context;
112 it = message_queue_.erase(it);
113 message_queue_version_++;
114 expected_version = message_queue_version_;
115 break;
116 }
117 }
118 }
initial.commit09911bf2008-07-26 23:55:29119
[email protected]522cc10d2012-01-11 22:39:54120 if (message == NULL)
121 break;
122 context->OnDispatchMessage(*message);
123 delete message;
initial.commit09911bf2008-07-26 23:55:29124 }
125 }
126
initial.commit09911bf2008-07-26 23:55:29127 // SyncChannel calls this in its destructor.
[email protected]d3ae7a072008-12-05 20:27:20128 void RemoveContext(SyncContext* context) {
[email protected]20305ec2011-01-21 04:55:52129 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:29130
[email protected]d3ae7a072008-12-05 20:27:20131 SyncMessageQueue::iterator iter = message_queue_.begin();
132 while (iter != message_queue_.end()) {
133 if (iter->context == context) {
134 delete iter->message;
135 iter = message_queue_.erase(iter);
[email protected]522cc10d2012-01-11 22:39:54136 message_queue_version_++;
initial.commit09911bf2008-07-26 23:55:29137 } else {
[email protected]d3ae7a072008-12-05 20:27:20138 iter++;
initial.commit09911bf2008-07-26 23:55:29139 }
initial.commit09911bf2008-07-26 23:55:29140 }
[email protected]3cdb7af812008-10-24 19:21:13141
142 if (--listener_count_ == 0) {
143 DCHECK(lazy_tls_ptr_.Pointer()->Get());
144 lazy_tls_ptr_.Pointer()->Set(NULL);
145 }
initial.commit09911bf2008-07-26 23:55:29146 }
147
[email protected]1c4947f2009-01-15 22:25:11148 WaitableEvent* dispatch_event() { return &dispatch_event_; }
[email protected]b2432302012-07-02 21:15:52149 base::SingleThreadTaskRunner* listener_task_runner() {
150 return listener_task_runner_;
[email protected]92bf9062011-05-02 18:00:49151 }
initial.commit09911bf2008-07-26 23:55:29152
[email protected]f886b7bf2008-09-10 10:54:06153 // Holds a pointer to the per-thread ReceivedSyncMsgQueue object.
154 static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
155 lazy_tls_ptr_;
156
initial.commit09911bf2008-07-26 23:55:29157 // Called on the ipc thread to check if we can unblock any current Send()
158 // calls based on a queued reply.
159 void DispatchReplies() {
initial.commit09911bf2008-07-26 23:55:29160 for (size_t i = 0; i < received_replies_.size(); ++i) {
161 Message* message = received_replies_[i].message;
[email protected]3cdb7af812008-10-24 19:21:13162 if (received_replies_[i].context->TryToUnblockListener(message)) {
initial.commit09911bf2008-07-26 23:55:29163 delete message;
164 received_replies_.erase(received_replies_.begin() + i);
165 return;
166 }
167 }
168 }
169
[email protected]ac0efda2009-10-14 16:22:02170 base::WaitableEventWatcher* top_send_done_watcher() {
171 return top_send_done_watcher_;
172 }
173
174 void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) {
175 top_send_done_watcher_ = watcher;
176 }
177
[email protected]63a7bb82008-10-25 00:46:00178 private:
[email protected]877d55d2009-11-05 21:53:08179 friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>;
180
[email protected]4df10d612008-11-12 00:38:26181 // See the comment in SyncChannel::SyncChannel for why this event is created
182 // as manual reset.
[email protected]63a7bb82008-10-25 00:46:00183 ReceivedSyncMsgQueue() :
[email protected]522cc10d2012-01-11 22:39:54184 message_queue_version_(0),
[email protected]1c4947f2009-01-15 22:25:11185 dispatch_event_(true, false),
[email protected]b2432302012-07-02 21:15:52186 listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
[email protected]1c4947f2009-01-15 22:25:11187 task_pending_(false),
[email protected]ac0efda2009-10-14 16:22:02188 listener_count_(0),
189 top_send_done_watcher_(NULL) {
[email protected]63a7bb82008-10-25 00:46:00190 }
191
[email protected]877d55d2009-11-05 21:53:08192 ~ReceivedSyncMsgQueue() {}
193
[email protected]d3ae7a072008-12-05 20:27:20194 // Holds information about a queued synchronous message or reply.
195 struct QueuedMessage {
196 QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
initial.commit09911bf2008-07-26 23:55:29197 Message* message;
198 scoped_refptr<SyncChannel::SyncContext> context;
199 };
200
[email protected]522cc10d2012-01-11 22:39:54201 typedef std::list<QueuedMessage> SyncMessageQueue;
[email protected]1c4947f2009-01-15 22:25:11202 SyncMessageQueue message_queue_;
[email protected]522cc10d2012-01-11 22:39:54203 uint32 message_queue_version_; // Used to signal DispatchMessages to rescan
[email protected]d3ae7a072008-12-05 20:27:20204
205 std::vector<QueuedMessage> received_replies_;
[email protected]3cdb7af812008-10-24 19:21:13206
207 // Set when we got a synchronous message that we must respond to as the
208 // sender needs its reply before it can reply to our original synchronous
209 // message.
[email protected]1c4947f2009-01-15 22:25:11210 WaitableEvent dispatch_event_;
[email protected]b2432302012-07-02 21:15:52211 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
[email protected]20305ec2011-01-21 04:55:52212 base::Lock message_lock_;
[email protected]3cdb7af812008-10-24 19:21:13213 bool task_pending_;
214 int listener_count_;
[email protected]ac0efda2009-10-14 16:22:02215
216 // The current send done event watcher for this thread. Used to maintain
217 // a local global stack of send done watchers to ensure that nested sync
218 // message loops complete correctly.
219 base::WaitableEventWatcher* top_send_done_watcher_;
initial.commit09911bf2008-07-26 23:55:29220};
221
[email protected]f886b7bf2008-09-10 10:54:06222base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
[email protected]6de0fd1d2011-11-15 13:31:49223 SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ =
224 LAZY_INSTANCE_INITIALIZER;
initial.commit09911bf2008-07-26 23:55:29225
226SyncChannel::SyncContext::SyncContext(
[email protected]b7f59e822012-06-29 22:05:26227 Listener* listener,
[email protected]b2432302012-07-02 21:15:52228 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]1c4947f2009-01-15 22:25:11229 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52230 : ChannelProxy::Context(listener, ipc_task_runner),
[email protected]1c4947f2009-01-15 22:25:11231 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
[email protected]54af05f2011-04-08 03:38:21232 shutdown_event_(shutdown_event),
[email protected]298ee7d2012-03-30 21:29:30233 restrict_dispatch_group_(kRestrictDispatchGroup_None) {
initial.commit09911bf2008-07-26 23:55:29234}
235
236SyncChannel::SyncContext::~SyncContext() {
237 while (!deserializers_.empty())
[email protected]3cdb7af812008-10-24 19:21:13238 Pop();
initial.commit09911bf2008-07-26 23:55:29239}
240
241// Adds information about an outgoing sync message to the context so that
242// we know how to deserialize the reply. Returns a handle that's set when
243// the reply has arrived.
[email protected]3cdb7af812008-10-24 19:21:13244void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
[email protected]4a180a52011-04-15 19:07:43245 // Create the tracking information for this message. This object is stored
246 // by value since all members are pointers that are cheap to copy. These
247 // pointers are cleaned up in the Pop() function.
248 //
[email protected]1c4947f2009-01-15 22:25:11249 // The event is created as manual reset because in between Signal and
[email protected]4df10d612008-11-12 00:38:26250 // OnObjectSignalled, another Send can happen which would stop the watcher
251 // from being called. The event would get watched later, when the nested
252 // Send completes, so the event will need to remain set.
[email protected]3cdb7af812008-10-24 19:21:13253 PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
initial.commit09911bf2008-07-26 23:55:29254 sync_msg->GetReplyDeserializer(),
[email protected]1c4947f2009-01-15 22:25:11255 new WaitableEvent(true, false));
[email protected]20305ec2011-01-21 04:55:52256 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13257 deserializers_.push_back(pending);
initial.commit09911bf2008-07-26 23:55:29258}
259
[email protected]3cdb7af812008-10-24 19:21:13260bool SyncChannel::SyncContext::Pop() {
[email protected]63a7bb82008-10-25 00:46:00261 bool result;
262 {
[email protected]20305ec2011-01-21 04:55:52263 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00264 PendingSyncMsg msg = deserializers_.back();
265 delete msg.deserializer;
[email protected]1c4947f2009-01-15 22:25:11266 delete msg.done_event;
267 msg.done_event = NULL;
[email protected]63a7bb82008-10-25 00:46:00268 deserializers_.pop_back();
269 result = msg.send_result;
270 }
271
272 // We got a reply to a synchronous Send() call that's blocking the listener
273 // thread. However, further down the call stack there could be another
274 // blocking Send() call, whose reply we received after we made this last
275 // Send() call. So check if we have any queued replies available that
276 // can now unblock the listener thread.
[email protected]b2432302012-07-02 21:15:52277 ipc_task_runner()->PostTask(
[email protected]72b6f8e22011-11-12 21:16:41278 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies,
279 received_sync_msgs_.get()));
[email protected]63a7bb82008-10-25 00:46:00280
281 return result;
[email protected]3cdb7af812008-10-24 19:21:13282}
283
[email protected]1c4947f2009-01-15 22:25:11284WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
[email protected]20305ec2011-01-21 04:55:52285 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13286 return deserializers_.back().done_event;
287}
288
[email protected]1c4947f2009-01-15 22:25:11289WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
[email protected]3cdb7af812008-10-24 19:21:13290 return received_sync_msgs_->dispatch_event();
initial.commit09911bf2008-07-26 23:55:29291}
292
293void SyncChannel::SyncContext::DispatchMessages() {
[email protected]54af05f2011-04-08 03:38:21294 received_sync_msgs_->DispatchMessages(this);
initial.commit09911bf2008-07-26 23:55:29295}
296
[email protected]3cdb7af812008-10-24 19:21:13297bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
[email protected]20305ec2011-01-21 04:55:52298 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00299 if (deserializers_.empty() ||
300 !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
301 return false;
[email protected]3cdb7af812008-10-24 19:21:13302 }
initial.commit09911bf2008-07-26 23:55:29303
[email protected]211142cd2012-08-13 09:41:19304 // TODO(bauerb): Remove logging once investigation of http://crbug.com/141055
305 // has finished.
[email protected]63a7bb82008-10-25 00:46:00306 if (!msg->is_reply_error()) {
[email protected]211142cd2012-08-13 09:41:19307 bool send_result = deserializers_.back().deserializer->
[email protected]63a7bb82008-10-25 00:46:00308 SerializeOutputParameters(*msg);
[email protected]211142cd2012-08-13 09:41:19309 deserializers_.back().send_result = send_result;
310 LOG_IF(ERROR, !send_result) << "Couldn't deserialize reply message";
311 } else {
312 LOG(ERROR) << "Received error reply";
[email protected]63a7bb82008-10-25 00:46:00313 }
[email protected]1c4947f2009-01-15 22:25:11314 deserializers_.back().done_event->Signal();
initial.commit09911bf2008-07-26 23:55:29315
[email protected]3cdb7af812008-10-24 19:21:13316 return true;
initial.commit09911bf2008-07-26 23:55:29317}
318
[email protected]3cdb7af812008-10-24 19:21:13319void SyncChannel::SyncContext::Clear() {
320 CancelPendingSends();
[email protected]d3ae7a072008-12-05 20:27:20321 received_sync_msgs_->RemoveContext(this);
[email protected]3cdb7af812008-10-24 19:21:13322 Context::Clear();
323}
324
[email protected]a95986a82010-12-24 06:19:28325bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
[email protected]d65cab7a2008-08-12 01:25:41326 // Give the filters a chance at processing this message.
327 if (TryFilters(msg))
[email protected]a95986a82010-12-24 06:19:28328 return true;
[email protected]d65cab7a2008-08-12 01:25:41329
[email protected]3cdb7af812008-10-24 19:21:13330 if (TryToUnblockListener(&msg))
[email protected]a95986a82010-12-24 06:19:28331 return true;
initial.commit09911bf2008-07-26 23:55:29332
[email protected]9134cce6d2012-04-10 20:07:53333 if (msg.is_reply()) {
334 received_sync_msgs_->QueueReply(msg, this);
[email protected]a95986a82010-12-24 06:19:28335 return true;
initial.commit09911bf2008-07-26 23:55:29336 }
337
[email protected]9134cce6d2012-04-10 20:07:53338 if (msg.should_unblock()) {
339 received_sync_msgs_->QueueMessage(msg, this);
[email protected]a95986a82010-12-24 06:19:28340 return true;
initial.commit09911bf2008-07-26 23:55:29341 }
342
[email protected]3cdb7af812008-10-24 19:21:13343 return Context::OnMessageReceivedNoFilter(msg);
initial.commit09911bf2008-07-26 23:55:29344}
345
initial.commit09911bf2008-07-26 23:55:29346void SyncChannel::SyncContext::OnChannelError() {
[email protected]3cdb7af812008-10-24 19:21:13347 CancelPendingSends();
[email protected]a4f822702009-02-06 00:44:53348 shutdown_watcher_.StopWatching();
initial.commit09911bf2008-07-26 23:55:29349 Context::OnChannelError();
350}
351
[email protected]3cdb7af812008-10-24 19:21:13352void SyncChannel::SyncContext::OnChannelOpened() {
353 shutdown_watcher_.StartWatching(shutdown_event_, this);
354 Context::OnChannelOpened();
initial.commit09911bf2008-07-26 23:55:29355}
356
[email protected]3cdb7af812008-10-24 19:21:13357void SyncChannel::SyncContext::OnChannelClosed() {
[email protected]87339f02010-09-02 21:45:50358 CancelPendingSends();
[email protected]3cdb7af812008-10-24 19:21:13359 shutdown_watcher_.StopWatching();
360 Context::OnChannelClosed();
361}
362
363void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
[email protected]20305ec2011-01-21 04:55:52364 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13365 PendingSyncMessageQueue::iterator iter;
[email protected]211142cd2012-08-13 09:41:19366 LOG(ERROR) << "Send timeout";
[email protected]3cdb7af812008-10-24 19:21:13367 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) {
[email protected]d3ae7a072008-12-05 20:27:20368 if (iter->id == message_id) {
[email protected]1c4947f2009-01-15 22:25:11369 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13370 break;
371 }
372 }
373}
374
375void SyncChannel::SyncContext::CancelPendingSends() {
[email protected]20305ec2011-01-21 04:55:52376 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13377 PendingSyncMessageQueue::iterator iter;
[email protected]211142cd2012-08-13 09:41:19378 LOG(ERROR) << "Canceling pending sends";
[email protected]3cdb7af812008-10-24 19:21:13379 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
[email protected]1c4947f2009-01-15 22:25:11380 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13381}
382
[email protected]1c4947f2009-01-15 22:25:11383void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18384 if (event == shutdown_event_) {
385 // Process shut down before we can get a reply to a synchronous message.
386 // Cancel pending Send calls, which will end up setting the send done event.
387 CancelPendingSends();
388 } else {
389 // We got the reply, timed out or the process shutdown.
[email protected]5e0be642011-04-28 18:20:09390 DCHECK_EQ(GetSendDoneEvent(), event);
[email protected]781a7ed2010-02-23 07:12:22391 MessageLoop::current()->QuitNow();
[email protected]9eec2252009-12-01 02:34:18392 }
[email protected]3cdb7af812008-10-24 19:21:13393}
394
395
396SyncChannel::SyncChannel(
[email protected]42ce94e2010-12-08 19:28:09397 const IPC::ChannelHandle& channel_handle,
[email protected]4b580bf2010-12-02 19:16:07398 Channel::Mode mode,
[email protected]b7f59e822012-06-29 22:05:26399 Listener* listener,
[email protected]b2432302012-07-02 21:15:52400 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]4b580bf2010-12-02 19:16:07401 bool create_pipe_now,
[email protected]1c4947f2009-01-15 22:25:11402 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52403 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
[email protected]d65cab7a2008-08-12 01:25:41404 sync_messages_with_no_timeout_allowed_(true) {
[email protected]952394af2011-11-16 01:06:46405 ChannelProxy::Init(channel_handle, mode, create_pipe_now);
406 StartWatching();
407}
408
409SyncChannel::SyncChannel(
[email protected]b7f59e822012-06-29 22:05:26410 Listener* listener,
[email protected]b2432302012-07-02 21:15:52411 base::SingleThreadTaskRunner* ipc_task_runner,
[email protected]952394af2011-11-16 01:06:46412 WaitableEvent* shutdown_event)
[email protected]b2432302012-07-02 21:15:52413 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
[email protected]952394af2011-11-16 01:06:46414 sync_messages_with_no_timeout_allowed_(true) {
415 StartWatching();
initial.commit09911bf2008-07-26 23:55:29416}
417
418SyncChannel::~SyncChannel() {
initial.commit09911bf2008-07-26 23:55:29419}
420
[email protected]298ee7d2012-03-30 21:29:30421void SyncChannel::SetRestrictDispatchChannelGroup(int group) {
422 sync_context()->set_restrict_dispatch_group(group);
[email protected]54af05f2011-04-08 03:38:21423}
424
[email protected]3cdb7af812008-10-24 19:21:13425bool SyncChannel::Send(Message* message) {
[email protected]aa96ae772009-01-20 22:08:15426 return SendWithTimeout(message, base::kNoTimeout);
[email protected]d65cab7a2008-08-12 01:25:41427}
428
[email protected]3cdb7af812008-10-24 19:21:13429bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) {
430 if (!message->is_sync()) {
431 ChannelProxy::Send(message);
432 return true;
initial.commit09911bf2008-07-26 23:55:29433 }
434
[email protected]3cdb7af812008-10-24 19:21:13435 // *this* might get deleted in WaitForReply.
436 scoped_refptr<SyncContext> context(sync_context());
[email protected]1c4947f2009-01-15 22:25:11437 if (context->shutdown_event()->IsSignaled()) {
[email protected]211142cd2012-08-13 09:41:19438 LOG(ERROR) << "shutdown event is signaled";
[email protected]3cdb7af812008-10-24 19:21:13439 delete message;
440 return false;
441 }
442
[email protected]d3216442009-03-05 21:07:27443 DCHECK(sync_messages_with_no_timeout_allowed_ ||
444 timeout_ms != base::kNoTimeout);
[email protected]3cdb7af812008-10-24 19:21:13445 SyncMessage* sync_msg = static_cast<SyncMessage*>(message);
446 context->Push(sync_msg);
447 int message_id = SyncMessage::GetMessageId(*sync_msg);
[email protected]1c4947f2009-01-15 22:25:11448 WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
[email protected]3cdb7af812008-10-24 19:21:13449
initial.commit09911bf2008-07-26 23:55:29450 ChannelProxy::Send(message);
initial.commit09911bf2008-07-26 23:55:29451
[email protected]aa96ae772009-01-20 22:08:15452 if (timeout_ms != base::kNoTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13453 // We use the sync message id so that when a message times out, we don't
454 // confuse it with another send that is either above/below this Send in
455 // the call stack.
[email protected]b2432302012-07-02 21:15:52456 context->ipc_task_runner()->PostDelayedTask(
[email protected]72b6f8e22011-11-12 21:16:41457 FROM_HERE,
458 base::Bind(&SyncContext::OnSendTimeout, context.get(), message_id),
[email protected]5896fa042012-03-07 04:41:40459 base::TimeDelta::FromMilliseconds(timeout_ms));
[email protected]3cdb7af812008-10-24 19:21:13460 }
initial.commit09911bf2008-07-26 23:55:29461
[email protected]3cdb7af812008-10-24 19:21:13462 // Wait for reply, or for any other incoming synchronous messages.
[email protected]9eec2252009-12-01 02:34:18463 // *this* might get deleted, so only call static functions at this point.
464 WaitForReply(context, pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29465
[email protected]3cdb7af812008-10-24 19:21:13466 return context->Pop();
initial.commit09911bf2008-07-26 23:55:29467}
468
[email protected]9eec2252009-12-01 02:34:18469void SyncChannel::WaitForReply(
470 SyncContext* context, WaitableEvent* pump_messages_event) {
[email protected]54af05f2011-04-08 03:38:21471 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13472 while (true) {
[email protected]1c4947f2009-01-15 22:25:11473 WaitableEvent* objects[] = {
[email protected]9eec2252009-12-01 02:34:18474 context->GetDispatchEvent(),
475 context->GetSendDoneEvent(),
[email protected]1c4947f2009-01-15 22:25:11476 pump_messages_event
477 };
478
479 unsigned count = pump_messages_event ? 3: 2;
[email protected]7dc8d792009-11-20 17:30:44480 size_t result = WaitableEvent::WaitMany(objects, count);
[email protected]1c4947f2009-01-15 22:25:11481 if (result == 0 /* dispatch event */) {
[email protected]3cdb7af812008-10-24 19:21:13482 // We're waiting for a reply, but we received a blocking synchronous
483 // call. We must process it or otherwise a deadlock might occur.
[email protected]9eec2252009-12-01 02:34:18484 context->GetDispatchEvent()->Reset();
485 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13486 continue;
487 }
488
[email protected]1c4947f2009-01-15 22:25:11489 if (result == 2 /* pump_messages_event */)
[email protected]9eec2252009-12-01 02:34:18490 WaitForReplyWithNestedMessageLoop(context); // Run a nested message loop.
[email protected]3cdb7af812008-10-24 19:21:13491
492 break;
493 }
494}
495
[email protected]9eec2252009-12-01 02:34:18496void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
[email protected]ac0efda2009-10-14 16:22:02497 base::WaitableEventWatcher send_done_watcher;
498
[email protected]9eec2252009-12-01 02:34:18499 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
[email protected]ac0efda2009-10-14 16:22:02500 DCHECK(sync_msg_queue != NULL);
501
502 base::WaitableEventWatcher* old_send_done_event_watcher =
503 sync_msg_queue->top_send_done_watcher();
504
505 base::WaitableEventWatcher::Delegate* old_delegate = NULL;
506 base::WaitableEvent* old_event = NULL;
507
508 // Maintain a local global stack of send done delegates to ensure that
509 // nested sync calls complete in the correct sequence, i.e. the
510 // outermost call completes first, etc.
511 if (old_send_done_event_watcher) {
512 old_delegate = old_send_done_event_watcher->delegate();
513 old_event = old_send_done_event_watcher->GetWatchedEvent();
514 old_send_done_event_watcher->StopWatching();
515 }
516
517 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
518
[email protected]9eec2252009-12-01 02:34:18519 send_done_watcher.StartWatching(context->GetSendDoneEvent(), context);
[email protected]ac0efda2009-10-14 16:22:02520
[email protected]b5717a42012-02-14 19:33:52521 {
522 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
523 MessageLoop::current()->Run();
524 }
[email protected]ac0efda2009-10-14 16:22:02525
526 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
[email protected]424379802009-10-14 19:58:13527 if (old_send_done_event_watcher && old_event) {
[email protected]ac0efda2009-10-14 16:22:02528 old_send_done_event_watcher->StartWatching(old_event, old_delegate);
529 }
[email protected]3cdb7af812008-10-24 19:21:13530}
531
[email protected]1c4947f2009-01-15 22:25:11532void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18533 DCHECK(event == sync_context()->GetDispatchEvent());
534 // The call to DispatchMessages might delete this object, so reregister
535 // the object watcher first.
536 event->Reset();
537 dispatch_watcher_.StartWatching(event, this);
538 sync_context()->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29539}
540
[email protected]952394af2011-11-16 01:06:46541void SyncChannel::StartWatching() {
542 // Ideally we only want to watch this object when running a nested message
543 // loop. However, we don't know when it exits if there's another nested
544 // message loop running under it or not, so we wouldn't know whether to
545 // stop or keep watching. So we always watch it, and create the event as
546 // manual reset since the object watcher might otherwise reset the event
547 // when we're doing a WaitMany.
548 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this);
549}
550
initial.commit09911bf2008-07-26 23:55:29551} // namespace IPC