blob: f3c538421386a3fc03c076501e4b7d7eb10be632 [file] [log] [blame]
[email protected]54af05f2011-04-08 03:38:211// Copyright (c) 2011 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]1357c322010-12-30 22:18:5611#include "base/threading/thread_local.h"
[email protected]44f9c952011-01-02 06:05:3912#include "base/synchronization/waitable_event.h"
13#include "base/synchronization/waitable_event_watcher.h"
[email protected]946d1b22009-07-22 23:57:2114#include "ipc/ipc_sync_message.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]e1acf6f2008-10-27 20:43:3316using base::TimeDelta;
17using base::TimeTicks;
[email protected]1c4947f2009-01-15 22:25:1118using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2919
20namespace 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.commit09911bf2008-07-26 23:55:2938class SyncChannel::ReceivedSyncMsgQueue :
39 public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
40 public:
[email protected]3cdb7af812008-10-24 19:21:1341 // Returns the ReceivedSyncMsgQueue instance for this thread, creating one
[email protected]d3ae7a072008-12-05 20:27:2042 // if necessary. Call RemoveContext on the same thread when done.
43 static ReceivedSyncMsgQueue* AddContext() {
[email protected]3cdb7af812008-10-24 19:21:1344 // 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.commit09911bf2008-07-26 23:55:2953 }
54
initial.commit09911bf2008-07-26 23:55:2955 // Called on IPC thread when a synchronous message or reply arrives.
[email protected]d3ae7a072008-12-05 20:27:2056 void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2957 bool was_task_pending;
58 {
[email protected]20305ec2011-01-21 04:55:5259 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2960
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]d3ae7a072008-12-05 20:27:2066 message_queue_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2967 }
68
[email protected]1c4947f2009-01-15 22:25:1169 dispatch_event_.Signal();
initial.commit09911bf2008-07-26 23:55:2970 if (!was_task_pending) {
[email protected]72b6f8e22011-11-12 21:16:4171 listener_message_loop_->PostTask(
72 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask,
73 this, scoped_refptr<SyncContext>(context)));
initial.commit09911bf2008-07-26 23:55:2974 }
75 }
76
77 void QueueReply(const Message &msg, SyncChannel::SyncContext* context) {
[email protected]d3ae7a072008-12-05 20:27:2078 received_replies_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2979 }
80
[email protected]d3ae7a072008-12-05 20:27:2081 // Called on the listener's thread to process any queues synchronous
initial.commit09911bf2008-07-26 23:55:2982 // messages.
[email protected]54af05f2011-04-08 03:38:2183 void DispatchMessagesTask(SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2984 {
[email protected]20305ec2011-01-21 04:55:5285 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:2986 task_pending_ = false;
87 }
[email protected]54af05f2011-04-08 03:38:2188 context->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:2989 }
90
[email protected]54af05f2011-04-08 03:38:2191 void DispatchMessages(SyncContext* dispatching_context) {
92 SyncMessageQueue delayed_queue;
initial.commit09911bf2008-07-26 23:55:2993 while (true) {
[email protected]d3ae7a072008-12-05 20:27:2094 Message* message;
95 scoped_refptr<SyncChannel::SyncContext> context;
initial.commit09911bf2008-07-26 23:55:2996 {
[email protected]20305ec2011-01-21 04:55:5297 base::AutoLock auto_lock(message_lock_);
[email protected]54af05f2011-04-08 03:38:2198 if (message_queue_.empty()) {
99 message_queue_ = delayed_queue;
initial.commit09911bf2008-07-26 23:55:29100 break;
[email protected]54af05f2011-04-08 03:38:21101 }
initial.commit09911bf2008-07-26 23:55:29102
[email protected]d3ae7a072008-12-05 20:27:20103 message = message_queue_.front().message;
104 context = message_queue_.front().context;
105 message_queue_.pop_front();
initial.commit09911bf2008-07-26 23:55:29106 }
[email protected]54af05f2011-04-08 03:38:21107 if (context->restrict_dispatch() && context != dispatching_context) {
108 delayed_queue.push_back(QueuedMessage(message, context));
109 } else {
110 context->OnDispatchMessage(*message);
111 delete message;
112 }
initial.commit09911bf2008-07-26 23:55:29113 }
114 }
115
initial.commit09911bf2008-07-26 23:55:29116 // SyncChannel calls this in its destructor.
[email protected]d3ae7a072008-12-05 20:27:20117 void RemoveContext(SyncContext* context) {
[email protected]20305ec2011-01-21 04:55:52118 base::AutoLock auto_lock(message_lock_);
initial.commit09911bf2008-07-26 23:55:29119
[email protected]d3ae7a072008-12-05 20:27:20120 SyncMessageQueue::iterator iter = message_queue_.begin();
121 while (iter != message_queue_.end()) {
122 if (iter->context == context) {
123 delete iter->message;
124 iter = message_queue_.erase(iter);
initial.commit09911bf2008-07-26 23:55:29125 } else {
[email protected]d3ae7a072008-12-05 20:27:20126 iter++;
initial.commit09911bf2008-07-26 23:55:29127 }
initial.commit09911bf2008-07-26 23:55:29128 }
[email protected]3cdb7af812008-10-24 19:21:13129
130 if (--listener_count_ == 0) {
131 DCHECK(lazy_tls_ptr_.Pointer()->Get());
132 lazy_tls_ptr_.Pointer()->Set(NULL);
133 }
initial.commit09911bf2008-07-26 23:55:29134 }
135
[email protected]1c4947f2009-01-15 22:25:11136 WaitableEvent* dispatch_event() { return &dispatch_event_; }
[email protected]92bf9062011-05-02 18:00:49137 base::MessageLoopProxy* listener_message_loop() {
138 return listener_message_loop_;
139 }
initial.commit09911bf2008-07-26 23:55:29140
[email protected]f886b7bf2008-09-10 10:54:06141 // Holds a pointer to the per-thread ReceivedSyncMsgQueue object.
142 static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
143 lazy_tls_ptr_;
144
initial.commit09911bf2008-07-26 23:55:29145 // Called on the ipc thread to check if we can unblock any current Send()
146 // calls based on a queued reply.
147 void DispatchReplies() {
initial.commit09911bf2008-07-26 23:55:29148 for (size_t i = 0; i < received_replies_.size(); ++i) {
149 Message* message = received_replies_[i].message;
[email protected]3cdb7af812008-10-24 19:21:13150 if (received_replies_[i].context->TryToUnblockListener(message)) {
initial.commit09911bf2008-07-26 23:55:29151 delete message;
152 received_replies_.erase(received_replies_.begin() + i);
153 return;
154 }
155 }
156 }
157
[email protected]ac0efda2009-10-14 16:22:02158 base::WaitableEventWatcher* top_send_done_watcher() {
159 return top_send_done_watcher_;
160 }
161
162 void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) {
163 top_send_done_watcher_ = watcher;
164 }
165
[email protected]63a7bb82008-10-25 00:46:00166 private:
[email protected]877d55d2009-11-05 21:53:08167 friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>;
168
[email protected]4df10d612008-11-12 00:38:26169 // See the comment in SyncChannel::SyncChannel for why this event is created
170 // as manual reset.
[email protected]63a7bb82008-10-25 00:46:00171 ReceivedSyncMsgQueue() :
[email protected]1c4947f2009-01-15 22:25:11172 dispatch_event_(true, false),
[email protected]edd685f2011-08-15 20:33:46173 listener_message_loop_(base::MessageLoopProxy::current()),
[email protected]1c4947f2009-01-15 22:25:11174 task_pending_(false),
[email protected]ac0efda2009-10-14 16:22:02175 listener_count_(0),
176 top_send_done_watcher_(NULL) {
[email protected]63a7bb82008-10-25 00:46:00177 }
178
[email protected]877d55d2009-11-05 21:53:08179 ~ReceivedSyncMsgQueue() {}
180
[email protected]d3ae7a072008-12-05 20:27:20181 // Holds information about a queued synchronous message or reply.
182 struct QueuedMessage {
183 QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
initial.commit09911bf2008-07-26 23:55:29184 Message* message;
185 scoped_refptr<SyncChannel::SyncContext> context;
186 };
187
[email protected]d3ae7a072008-12-05 20:27:20188 typedef std::deque<QueuedMessage> SyncMessageQueue;
[email protected]1c4947f2009-01-15 22:25:11189 SyncMessageQueue message_queue_;
[email protected]d3ae7a072008-12-05 20:27:20190
191 std::vector<QueuedMessage> received_replies_;
[email protected]3cdb7af812008-10-24 19:21:13192
193 // Set when we got a synchronous message that we must respond to as the
194 // sender needs its reply before it can reply to our original synchronous
195 // message.
[email protected]1c4947f2009-01-15 22:25:11196 WaitableEvent dispatch_event_;
[email protected]92bf9062011-05-02 18:00:49197 scoped_refptr<base::MessageLoopProxy> listener_message_loop_;
[email protected]20305ec2011-01-21 04:55:52198 base::Lock message_lock_;
[email protected]3cdb7af812008-10-24 19:21:13199 bool task_pending_;
200 int listener_count_;
[email protected]ac0efda2009-10-14 16:22:02201
202 // The current send done event watcher for this thread. Used to maintain
203 // a local global stack of send done watchers to ensure that nested sync
204 // message loops complete correctly.
205 base::WaitableEventWatcher* top_send_done_watcher_;
initial.commit09911bf2008-07-26 23:55:29206};
207
[email protected]f886b7bf2008-09-10 10:54:06208base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
[email protected]6de0fd1d2011-11-15 13:31:49209 SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ =
210 LAZY_INSTANCE_INITIALIZER;
initial.commit09911bf2008-07-26 23:55:29211
212SyncChannel::SyncContext::SyncContext(
213 Channel::Listener* listener,
[email protected]92bf9062011-05-02 18:00:49214 base::MessageLoopProxy* ipc_thread,
[email protected]1c4947f2009-01-15 22:25:11215 WaitableEvent* shutdown_event)
[email protected]4b580bf2010-12-02 19:16:07216 : ChannelProxy::Context(listener, ipc_thread),
[email protected]1c4947f2009-01-15 22:25:11217 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
[email protected]54af05f2011-04-08 03:38:21218 shutdown_event_(shutdown_event),
219 restrict_dispatch_(false) {
initial.commit09911bf2008-07-26 23:55:29220}
221
222SyncChannel::SyncContext::~SyncContext() {
223 while (!deserializers_.empty())
[email protected]3cdb7af812008-10-24 19:21:13224 Pop();
initial.commit09911bf2008-07-26 23:55:29225}
226
227// Adds information about an outgoing sync message to the context so that
228// we know how to deserialize the reply. Returns a handle that's set when
229// the reply has arrived.
[email protected]3cdb7af812008-10-24 19:21:13230void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
[email protected]4a180a52011-04-15 19:07:43231 // Create the tracking information for this message. This object is stored
232 // by value since all members are pointers that are cheap to copy. These
233 // pointers are cleaned up in the Pop() function.
234 //
[email protected]1c4947f2009-01-15 22:25:11235 // The event is created as manual reset because in between Signal and
[email protected]4df10d612008-11-12 00:38:26236 // OnObjectSignalled, another Send can happen which would stop the watcher
237 // from being called. The event would get watched later, when the nested
238 // Send completes, so the event will need to remain set.
[email protected]3cdb7af812008-10-24 19:21:13239 PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
initial.commit09911bf2008-07-26 23:55:29240 sync_msg->GetReplyDeserializer(),
[email protected]1c4947f2009-01-15 22:25:11241 new WaitableEvent(true, false));
[email protected]20305ec2011-01-21 04:55:52242 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13243 deserializers_.push_back(pending);
initial.commit09911bf2008-07-26 23:55:29244}
245
[email protected]3cdb7af812008-10-24 19:21:13246bool SyncChannel::SyncContext::Pop() {
[email protected]63a7bb82008-10-25 00:46:00247 bool result;
248 {
[email protected]20305ec2011-01-21 04:55:52249 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00250 PendingSyncMsg msg = deserializers_.back();
251 delete msg.deserializer;
[email protected]1c4947f2009-01-15 22:25:11252 delete msg.done_event;
253 msg.done_event = NULL;
[email protected]63a7bb82008-10-25 00:46:00254 deserializers_.pop_back();
255 result = msg.send_result;
256 }
257
258 // We got a reply to a synchronous Send() call that's blocking the listener
259 // thread. However, further down the call stack there could be another
260 // blocking Send() call, whose reply we received after we made this last
261 // Send() call. So check if we have any queued replies available that
262 // can now unblock the listener thread.
[email protected]72b6f8e22011-11-12 21:16:41263 ipc_message_loop()->PostTask(
264 FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies,
265 received_sync_msgs_.get()));
[email protected]63a7bb82008-10-25 00:46:00266
267 return result;
[email protected]3cdb7af812008-10-24 19:21:13268}
269
[email protected]1c4947f2009-01-15 22:25:11270WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
[email protected]20305ec2011-01-21 04:55:52271 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13272 return deserializers_.back().done_event;
273}
274
[email protected]1c4947f2009-01-15 22:25:11275WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
[email protected]3cdb7af812008-10-24 19:21:13276 return received_sync_msgs_->dispatch_event();
initial.commit09911bf2008-07-26 23:55:29277}
278
279void SyncChannel::SyncContext::DispatchMessages() {
[email protected]54af05f2011-04-08 03:38:21280 received_sync_msgs_->DispatchMessages(this);
initial.commit09911bf2008-07-26 23:55:29281}
282
[email protected]3cdb7af812008-10-24 19:21:13283bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
[email protected]20305ec2011-01-21 04:55:52284 base::AutoLock auto_lock(deserializers_lock_);
[email protected]63a7bb82008-10-25 00:46:00285 if (deserializers_.empty() ||
286 !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
287 return false;
[email protected]3cdb7af812008-10-24 19:21:13288 }
initial.commit09911bf2008-07-26 23:55:29289
[email protected]63a7bb82008-10-25 00:46:00290 if (!msg->is_reply_error()) {
291 deserializers_.back().send_result = deserializers_.back().deserializer->
292 SerializeOutputParameters(*msg);
293 }
[email protected]1c4947f2009-01-15 22:25:11294 deserializers_.back().done_event->Signal();
initial.commit09911bf2008-07-26 23:55:29295
[email protected]3cdb7af812008-10-24 19:21:13296 return true;
initial.commit09911bf2008-07-26 23:55:29297}
298
[email protected]3cdb7af812008-10-24 19:21:13299void SyncChannel::SyncContext::Clear() {
300 CancelPendingSends();
[email protected]d3ae7a072008-12-05 20:27:20301 received_sync_msgs_->RemoveContext(this);
[email protected]3cdb7af812008-10-24 19:21:13302 Context::Clear();
303}
304
[email protected]a95986a82010-12-24 06:19:28305bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
[email protected]d65cab7a2008-08-12 01:25:41306 // Give the filters a chance at processing this message.
307 if (TryFilters(msg))
[email protected]a95986a82010-12-24 06:19:28308 return true;
[email protected]d65cab7a2008-08-12 01:25:41309
[email protected]3cdb7af812008-10-24 19:21:13310 if (TryToUnblockListener(&msg))
[email protected]a95986a82010-12-24 06:19:28311 return true;
initial.commit09911bf2008-07-26 23:55:29312
313 if (msg.should_unblock()) {
[email protected]d3ae7a072008-12-05 20:27:20314 received_sync_msgs_->QueueMessage(msg, this);
[email protected]a95986a82010-12-24 06:19:28315 return true;
initial.commit09911bf2008-07-26 23:55:29316 }
317
318 if (msg.is_reply()) {
319 received_sync_msgs_->QueueReply(msg, this);
[email protected]a95986a82010-12-24 06:19:28320 return true;
initial.commit09911bf2008-07-26 23:55:29321 }
322
[email protected]3cdb7af812008-10-24 19:21:13323 return Context::OnMessageReceivedNoFilter(msg);
initial.commit09911bf2008-07-26 23:55:29324}
325
initial.commit09911bf2008-07-26 23:55:29326void SyncChannel::SyncContext::OnChannelError() {
[email protected]3cdb7af812008-10-24 19:21:13327 CancelPendingSends();
[email protected]a4f822702009-02-06 00:44:53328 shutdown_watcher_.StopWatching();
initial.commit09911bf2008-07-26 23:55:29329 Context::OnChannelError();
330}
331
[email protected]3cdb7af812008-10-24 19:21:13332void SyncChannel::SyncContext::OnChannelOpened() {
333 shutdown_watcher_.StartWatching(shutdown_event_, this);
334 Context::OnChannelOpened();
initial.commit09911bf2008-07-26 23:55:29335}
336
[email protected]3cdb7af812008-10-24 19:21:13337void SyncChannel::SyncContext::OnChannelClosed() {
[email protected]87339f02010-09-02 21:45:50338 CancelPendingSends();
[email protected]3cdb7af812008-10-24 19:21:13339 shutdown_watcher_.StopWatching();
340 Context::OnChannelClosed();
341}
342
343void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
[email protected]20305ec2011-01-21 04:55:52344 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13345 PendingSyncMessageQueue::iterator iter;
346 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) {
[email protected]d3ae7a072008-12-05 20:27:20347 if (iter->id == message_id) {
[email protected]1c4947f2009-01-15 22:25:11348 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13349 break;
350 }
351 }
352}
353
354void SyncChannel::SyncContext::CancelPendingSends() {
[email protected]20305ec2011-01-21 04:55:52355 base::AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13356 PendingSyncMessageQueue::iterator iter;
357 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
[email protected]1c4947f2009-01-15 22:25:11358 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13359}
360
[email protected]1c4947f2009-01-15 22:25:11361void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18362 if (event == shutdown_event_) {
363 // Process shut down before we can get a reply to a synchronous message.
364 // Cancel pending Send calls, which will end up setting the send done event.
365 CancelPendingSends();
366 } else {
367 // We got the reply, timed out or the process shutdown.
[email protected]5e0be642011-04-28 18:20:09368 DCHECK_EQ(GetSendDoneEvent(), event);
[email protected]781a7ed2010-02-23 07:12:22369 MessageLoop::current()->QuitNow();
[email protected]9eec2252009-12-01 02:34:18370 }
[email protected]3cdb7af812008-10-24 19:21:13371}
372
373
374SyncChannel::SyncChannel(
[email protected]42ce94e2010-12-08 19:28:09375 const IPC::ChannelHandle& channel_handle,
[email protected]4b580bf2010-12-02 19:16:07376 Channel::Mode mode,
377 Channel::Listener* listener,
[email protected]92bf9062011-05-02 18:00:49378 base::MessageLoopProxy* ipc_message_loop,
[email protected]4b580bf2010-12-02 19:16:07379 bool create_pipe_now,
[email protected]1c4947f2009-01-15 22:25:11380 WaitableEvent* shutdown_event)
[email protected]952394af2011-11-16 01:06:46381 : ChannelProxy(new SyncContext(listener, ipc_message_loop, shutdown_event)),
[email protected]d65cab7a2008-08-12 01:25:41382 sync_messages_with_no_timeout_allowed_(true) {
[email protected]952394af2011-11-16 01:06:46383 ChannelProxy::Init(channel_handle, mode, create_pipe_now);
384 StartWatching();
385}
386
387SyncChannel::SyncChannel(
388 Channel::Listener* listener,
389 base::MessageLoopProxy* ipc_message_loop,
390 WaitableEvent* shutdown_event)
391 : ChannelProxy(new SyncContext(listener, ipc_message_loop, shutdown_event)),
392 sync_messages_with_no_timeout_allowed_(true) {
393 StartWatching();
initial.commit09911bf2008-07-26 23:55:29394}
395
396SyncChannel::~SyncChannel() {
initial.commit09911bf2008-07-26 23:55:29397}
398
[email protected]54af05f2011-04-08 03:38:21399void SyncChannel::SetRestrictDispatchToSameChannel(bool value) {
400 sync_context()->set_restrict_dispatch(value);
401}
402
[email protected]3cdb7af812008-10-24 19:21:13403bool SyncChannel::Send(Message* message) {
[email protected]aa96ae772009-01-20 22:08:15404 return SendWithTimeout(message, base::kNoTimeout);
[email protected]d65cab7a2008-08-12 01:25:41405}
406
[email protected]3cdb7af812008-10-24 19:21:13407bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) {
408 if (!message->is_sync()) {
409 ChannelProxy::Send(message);
410 return true;
initial.commit09911bf2008-07-26 23:55:29411 }
412
[email protected]3cdb7af812008-10-24 19:21:13413 // *this* might get deleted in WaitForReply.
414 scoped_refptr<SyncContext> context(sync_context());
[email protected]1c4947f2009-01-15 22:25:11415 if (context->shutdown_event()->IsSignaled()) {
[email protected]3cdb7af812008-10-24 19:21:13416 delete message;
417 return false;
418 }
419
[email protected]d3216442009-03-05 21:07:27420 DCHECK(sync_messages_with_no_timeout_allowed_ ||
421 timeout_ms != base::kNoTimeout);
[email protected]3cdb7af812008-10-24 19:21:13422 SyncMessage* sync_msg = static_cast<SyncMessage*>(message);
423 context->Push(sync_msg);
424 int message_id = SyncMessage::GetMessageId(*sync_msg);
[email protected]1c4947f2009-01-15 22:25:11425 WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
[email protected]3cdb7af812008-10-24 19:21:13426
initial.commit09911bf2008-07-26 23:55:29427 ChannelProxy::Send(message);
initial.commit09911bf2008-07-26 23:55:29428
[email protected]aa96ae772009-01-20 22:08:15429 if (timeout_ms != base::kNoTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13430 // We use the sync message id so that when a message times out, we don't
431 // confuse it with another send that is either above/below this Send in
432 // the call stack.
[email protected]72b6f8e22011-11-12 21:16:41433 context->ipc_message_loop()->PostDelayedTask(
434 FROM_HERE,
435 base::Bind(&SyncContext::OnSendTimeout, context.get(), message_id),
436 timeout_ms);
[email protected]3cdb7af812008-10-24 19:21:13437 }
initial.commit09911bf2008-07-26 23:55:29438
[email protected]3cdb7af812008-10-24 19:21:13439 // Wait for reply, or for any other incoming synchronous messages.
[email protected]9eec2252009-12-01 02:34:18440 // *this* might get deleted, so only call static functions at this point.
441 WaitForReply(context, pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29442
[email protected]3cdb7af812008-10-24 19:21:13443 return context->Pop();
initial.commit09911bf2008-07-26 23:55:29444}
445
[email protected]9eec2252009-12-01 02:34:18446void SyncChannel::WaitForReply(
447 SyncContext* context, WaitableEvent* pump_messages_event) {
[email protected]54af05f2011-04-08 03:38:21448 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13449 while (true) {
[email protected]1c4947f2009-01-15 22:25:11450 WaitableEvent* objects[] = {
[email protected]9eec2252009-12-01 02:34:18451 context->GetDispatchEvent(),
452 context->GetSendDoneEvent(),
[email protected]1c4947f2009-01-15 22:25:11453 pump_messages_event
454 };
455
456 unsigned count = pump_messages_event ? 3: 2;
[email protected]7dc8d792009-11-20 17:30:44457 size_t result = WaitableEvent::WaitMany(objects, count);
[email protected]1c4947f2009-01-15 22:25:11458 if (result == 0 /* dispatch event */) {
[email protected]3cdb7af812008-10-24 19:21:13459 // We're waiting for a reply, but we received a blocking synchronous
460 // call. We must process it or otherwise a deadlock might occur.
[email protected]9eec2252009-12-01 02:34:18461 context->GetDispatchEvent()->Reset();
462 context->DispatchMessages();
[email protected]3cdb7af812008-10-24 19:21:13463 continue;
464 }
465
[email protected]1c4947f2009-01-15 22:25:11466 if (result == 2 /* pump_messages_event */)
[email protected]9eec2252009-12-01 02:34:18467 WaitForReplyWithNestedMessageLoop(context); // Run a nested message loop.
[email protected]3cdb7af812008-10-24 19:21:13468
469 break;
470 }
471}
472
[email protected]9eec2252009-12-01 02:34:18473void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
[email protected]ac0efda2009-10-14 16:22:02474 base::WaitableEventWatcher send_done_watcher;
475
[email protected]9eec2252009-12-01 02:34:18476 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
[email protected]ac0efda2009-10-14 16:22:02477 DCHECK(sync_msg_queue != NULL);
478
479 base::WaitableEventWatcher* old_send_done_event_watcher =
480 sync_msg_queue->top_send_done_watcher();
481
482 base::WaitableEventWatcher::Delegate* old_delegate = NULL;
483 base::WaitableEvent* old_event = NULL;
484
485 // Maintain a local global stack of send done delegates to ensure that
486 // nested sync calls complete in the correct sequence, i.e. the
487 // outermost call completes first, etc.
488 if (old_send_done_event_watcher) {
489 old_delegate = old_send_done_event_watcher->delegate();
490 old_event = old_send_done_event_watcher->GetWatchedEvent();
491 old_send_done_event_watcher->StopWatching();
492 }
493
494 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
495
[email protected]9eec2252009-12-01 02:34:18496 send_done_watcher.StartWatching(context->GetSendDoneEvent(), context);
[email protected]3cdb7af812008-10-24 19:21:13497 bool old_state = MessageLoop::current()->NestableTasksAllowed();
[email protected]ac0efda2009-10-14 16:22:02498
[email protected]3cdb7af812008-10-24 19:21:13499 MessageLoop::current()->SetNestableTasksAllowed(true);
500 MessageLoop::current()->Run();
501 MessageLoop::current()->SetNestableTasksAllowed(old_state);
[email protected]ac0efda2009-10-14 16:22:02502
503 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
[email protected]424379802009-10-14 19:58:13504 if (old_send_done_event_watcher && old_event) {
[email protected]ac0efda2009-10-14 16:22:02505 old_send_done_event_watcher->StartWatching(old_event, old_delegate);
506 }
[email protected]3cdb7af812008-10-24 19:21:13507}
508
[email protected]1c4947f2009-01-15 22:25:11509void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
[email protected]9eec2252009-12-01 02:34:18510 DCHECK(event == sync_context()->GetDispatchEvent());
511 // The call to DispatchMessages might delete this object, so reregister
512 // the object watcher first.
513 event->Reset();
514 dispatch_watcher_.StartWatching(event, this);
515 sync_context()->DispatchMessages();
initial.commit09911bf2008-07-26 23:55:29516}
517
[email protected]952394af2011-11-16 01:06:46518void SyncChannel::StartWatching() {
519 // Ideally we only want to watch this object when running a nested message
520 // loop. However, we don't know when it exits if there's another nested
521 // message loop running under it or not, so we wouldn't know whether to
522 // stop or keep watching. So we always watch it, and create the event as
523 // manual reset since the object watcher might otherwise reset the event
524 // when we're doing a WaitMany.
525 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this);
526}
527
initial.commit09911bf2008-07-26 23:55:29528} // namespace IPC