blob: 81f8dc1ce792068993b1230eda22619b7c6651d1 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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]f886b7bf2008-09-10 10:54:067#include "base/lazy_instance.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/logging.h"
[email protected]f886b7bf2008-09-10 10:54:069#include "base/thread_local.h"
[email protected]1c4947f2009-01-15 22:25:1110#include "base/message_loop.h"
11#include "base/waitable_event.h"
12#include "base/waitable_event_watcher.h"
[email protected]946d1b22009-07-22 23:57:2113#include "ipc/ipc_sync_message.h"
initial.commit09911bf2008-07-26 23:55:2914
[email protected]e1acf6f2008-10-27 20:43:3315using base::TimeDelta;
16using base::TimeTicks;
[email protected]1c4947f2009-01-15 22:25:1117using base::WaitableEvent;
initial.commit09911bf2008-07-26 23:55:2918
19namespace IPC {
20// When we're blocked in a Send(), we need to process incoming synchronous
21// messages right away because it could be blocking our reply (either
22// directly from the same object we're calling, or indirectly through one or
23// more other channels). That means that in SyncContext's OnMessageReceived,
24// we need to process sync message right away if we're blocked. However a
25// simple check isn't sufficient, because the listener thread can be in the
26// process of calling Send.
27// To work around this, when SyncChannel filters a sync message, it sets
28// an event that the listener thread waits on during its Send() call. This
29// allows us to dispatch incoming sync messages when blocked. The race
30// condition is handled because if Send is in the process of being called, it
31// will check the event. In case the listener thread isn't sending a message,
32// we queue a task on the listener thread to dispatch the received messages.
33// The messages are stored in this queue object that's shared among all
34// SyncChannel objects on the same thread (since one object can receive a
35// sync message while another one is blocked).
36
initial.commit09911bf2008-07-26 23:55:2937class SyncChannel::ReceivedSyncMsgQueue :
38 public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
39 public:
[email protected]3cdb7af812008-10-24 19:21:1340 // Returns the ReceivedSyncMsgQueue instance for this thread, creating one
[email protected]d3ae7a072008-12-05 20:27:2041 // if necessary. Call RemoveContext on the same thread when done.
42 static ReceivedSyncMsgQueue* AddContext() {
[email protected]3cdb7af812008-10-24 19:21:1343 // We want one ReceivedSyncMsgQueue per listener thread (i.e. since multiple
44 // SyncChannel objects can block the same thread).
45 ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get();
46 if (!rv) {
47 rv = new ReceivedSyncMsgQueue();
48 ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv);
49 }
50 rv->listener_count_++;
51 return rv;
initial.commit09911bf2008-07-26 23:55:2952 }
53
initial.commit09911bf2008-07-26 23:55:2954 // Called on IPC thread when a synchronous message or reply arrives.
[email protected]d3ae7a072008-12-05 20:27:2055 void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:2956 bool was_task_pending;
57 {
58 AutoLock auto_lock(message_lock_);
59
60 was_task_pending = task_pending_;
61 task_pending_ = true;
62
63 // We set the event in case the listener thread is blocked (or is about
64 // to). In case it's not, the PostTask dispatches the messages.
[email protected]d3ae7a072008-12-05 20:27:2065 message_queue_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2966 }
67
[email protected]1c4947f2009-01-15 22:25:1168 dispatch_event_.Signal();
initial.commit09911bf2008-07-26 23:55:2969 if (!was_task_pending) {
70 listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
71 this, &ReceivedSyncMsgQueue::DispatchMessagesTask));
72 }
73 }
74
75 void QueueReply(const Message &msg, SyncChannel::SyncContext* context) {
[email protected]d3ae7a072008-12-05 20:27:2076 received_replies_.push_back(QueuedMessage(new Message(msg), context));
initial.commit09911bf2008-07-26 23:55:2977 }
78
[email protected]d3ae7a072008-12-05 20:27:2079 // Called on the listener's thread to process any queues synchronous
initial.commit09911bf2008-07-26 23:55:2980 // messages.
81 void DispatchMessagesTask() {
82 {
83 AutoLock auto_lock(message_lock_);
84 task_pending_ = false;
85 }
86 DispatchMessages();
87 }
88
89 void DispatchMessages() {
90 while (true) {
[email protected]d3ae7a072008-12-05 20:27:2091 Message* message;
92 scoped_refptr<SyncChannel::SyncContext> context;
initial.commit09911bf2008-07-26 23:55:2993 {
94 AutoLock auto_lock(message_lock_);
95 if (message_queue_.empty())
96 break;
97
[email protected]d3ae7a072008-12-05 20:27:2098 message = message_queue_.front().message;
99 context = message_queue_.front().context;
100 message_queue_.pop_front();
initial.commit09911bf2008-07-26 23:55:29101 }
102
[email protected]827ab812009-03-12 07:17:17103 context->OnDispatchMessage(*message);
initial.commit09911bf2008-07-26 23:55:29104 delete message;
105 }
106 }
107
initial.commit09911bf2008-07-26 23:55:29108 // SyncChannel calls this in its destructor.
[email protected]d3ae7a072008-12-05 20:27:20109 void RemoveContext(SyncContext* context) {
initial.commit09911bf2008-07-26 23:55:29110 AutoLock auto_lock(message_lock_);
111
[email protected]d3ae7a072008-12-05 20:27:20112 SyncMessageQueue::iterator iter = message_queue_.begin();
113 while (iter != message_queue_.end()) {
114 if (iter->context == context) {
115 delete iter->message;
116 iter = message_queue_.erase(iter);
initial.commit09911bf2008-07-26 23:55:29117 } else {
[email protected]d3ae7a072008-12-05 20:27:20118 iter++;
initial.commit09911bf2008-07-26 23:55:29119 }
initial.commit09911bf2008-07-26 23:55:29120 }
[email protected]3cdb7af812008-10-24 19:21:13121
122 if (--listener_count_ == 0) {
123 DCHECK(lazy_tls_ptr_.Pointer()->Get());
124 lazy_tls_ptr_.Pointer()->Set(NULL);
125 }
initial.commit09911bf2008-07-26 23:55:29126 }
127
[email protected]1c4947f2009-01-15 22:25:11128 WaitableEvent* dispatch_event() { return &dispatch_event_; }
[email protected]8fd8de92008-08-12 23:50:30129 MessageLoop* listener_message_loop() { return listener_message_loop_; }
initial.commit09911bf2008-07-26 23:55:29130
[email protected]f886b7bf2008-09-10 10:54:06131 // Holds a pointer to the per-thread ReceivedSyncMsgQueue object.
132 static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
133 lazy_tls_ptr_;
134
initial.commit09911bf2008-07-26 23:55:29135 // Called on the ipc thread to check if we can unblock any current Send()
136 // calls based on a queued reply.
137 void DispatchReplies() {
initial.commit09911bf2008-07-26 23:55:29138 for (size_t i = 0; i < received_replies_.size(); ++i) {
139 Message* message = received_replies_[i].message;
[email protected]3cdb7af812008-10-24 19:21:13140 if (received_replies_[i].context->TryToUnblockListener(message)) {
initial.commit09911bf2008-07-26 23:55:29141 delete message;
142 received_replies_.erase(received_replies_.begin() + i);
143 return;
144 }
145 }
146 }
147
[email protected]ac0efda2009-10-14 16:22:02148 base::WaitableEventWatcher* top_send_done_watcher() {
149 return top_send_done_watcher_;
150 }
151
152 void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) {
153 top_send_done_watcher_ = watcher;
154 }
155
[email protected]63a7bb82008-10-25 00:46:00156 private:
[email protected]877d55d2009-11-05 21:53:08157 friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>;
158
[email protected]4df10d612008-11-12 00:38:26159 // See the comment in SyncChannel::SyncChannel for why this event is created
160 // as manual reset.
[email protected]63a7bb82008-10-25 00:46:00161 ReceivedSyncMsgQueue() :
[email protected]1c4947f2009-01-15 22:25:11162 dispatch_event_(true, false),
[email protected]63a7bb82008-10-25 00:46:00163 listener_message_loop_(MessageLoop::current()),
[email protected]1c4947f2009-01-15 22:25:11164 task_pending_(false),
[email protected]ac0efda2009-10-14 16:22:02165 listener_count_(0),
166 top_send_done_watcher_(NULL) {
[email protected]63a7bb82008-10-25 00:46:00167 }
168
[email protected]877d55d2009-11-05 21:53:08169 ~ReceivedSyncMsgQueue() {}
170
[email protected]d3ae7a072008-12-05 20:27:20171 // Holds information about a queued synchronous message or reply.
172 struct QueuedMessage {
173 QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
initial.commit09911bf2008-07-26 23:55:29174 Message* message;
175 scoped_refptr<SyncChannel::SyncContext> context;
176 };
177
[email protected]d3ae7a072008-12-05 20:27:20178 typedef std::deque<QueuedMessage> SyncMessageQueue;
[email protected]1c4947f2009-01-15 22:25:11179 SyncMessageQueue message_queue_;
[email protected]d3ae7a072008-12-05 20:27:20180
181 std::vector<QueuedMessage> received_replies_;
[email protected]3cdb7af812008-10-24 19:21:13182
183 // Set when we got a synchronous message that we must respond to as the
184 // sender needs its reply before it can reply to our original synchronous
185 // message.
[email protected]1c4947f2009-01-15 22:25:11186 WaitableEvent dispatch_event_;
[email protected]3cdb7af812008-10-24 19:21:13187 MessageLoop* listener_message_loop_;
188 Lock message_lock_;
189 bool task_pending_;
190 int listener_count_;
[email protected]ac0efda2009-10-14 16:22:02191
192 // The current send done event watcher for this thread. Used to maintain
193 // a local global stack of send done watchers to ensure that nested sync
194 // message loops complete correctly.
195 base::WaitableEventWatcher* top_send_done_watcher_;
initial.commit09911bf2008-07-26 23:55:29196};
197
[email protected]f886b7bf2008-09-10 10:54:06198base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
199 SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_(base::LINKER_INITIALIZED);
initial.commit09911bf2008-07-26 23:55:29200
201SyncChannel::SyncContext::SyncContext(
202 Channel::Listener* listener,
203 MessageFilter* filter,
[email protected]3cdb7af812008-10-24 19:21:13204 MessageLoop* ipc_thread,
[email protected]1c4947f2009-01-15 22:25:11205 WaitableEvent* shutdown_event)
initial.commit09911bf2008-07-26 23:55:29206 : ChannelProxy::Context(listener, filter, ipc_thread),
[email protected]1c4947f2009-01-15 22:25:11207 received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
208 shutdown_event_(shutdown_event) {
initial.commit09911bf2008-07-26 23:55:29209}
210
211SyncChannel::SyncContext::~SyncContext() {
212 while (!deserializers_.empty())
[email protected]3cdb7af812008-10-24 19:21:13213 Pop();
initial.commit09911bf2008-07-26 23:55:29214}
215
216// Adds information about an outgoing sync message to the context so that
217// we know how to deserialize the reply. Returns a handle that's set when
218// the reply has arrived.
[email protected]3cdb7af812008-10-24 19:21:13219void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
[email protected]1c4947f2009-01-15 22:25:11220 // The event is created as manual reset because in between Signal and
[email protected]4df10d612008-11-12 00:38:26221 // OnObjectSignalled, another Send can happen which would stop the watcher
222 // from being called. The event would get watched later, when the nested
223 // Send completes, so the event will need to remain set.
[email protected]3cdb7af812008-10-24 19:21:13224 PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
initial.commit09911bf2008-07-26 23:55:29225 sync_msg->GetReplyDeserializer(),
[email protected]1c4947f2009-01-15 22:25:11226 new WaitableEvent(true, false));
initial.commit09911bf2008-07-26 23:55:29227 AutoLock auto_lock(deserializers_lock_);
[email protected]3cdb7af812008-10-24 19:21:13228 deserializers_.push_back(pending);
initial.commit09911bf2008-07-26 23:55:29229}
230
[email protected]3cdb7af812008-10-24 19:21:13231bool SyncChannel::SyncContext::Pop() {
[email protected]63a7bb82008-10-25 00:46:00232 bool result;
233 {
234 AutoLock auto_lock(deserializers_lock_);
235 PendingSyncMsg msg = deserializers_.back();
236 delete msg.deserializer;
[email protected]1c4947f2009-01-15 22:25:11237 delete msg.done_event;
238 msg.done_event = NULL;
[email protected]63a7bb82008-10-25 00:46:00239 deserializers_.pop_back();
240 result = msg.send_result;
241 }
242
243 // We got a reply to a synchronous Send() call that's blocking the listener
244 // thread. However, further down the call stack there could be another
245 // blocking Send() call, whose reply we received after we made this last
246 // Send() call. So check if we have any queued replies available that
247 // can now unblock the listener thread.
248 ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
249 received_sync_msgs_.get(), &ReceivedSyncMsgQueue::DispatchReplies));
250
251 return result;
[email protected]3cdb7af812008-10-24 19:21:13252}
253
[email protected]1c4947f2009-01-15 22:25:11254WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
[email protected]3cdb7af812008-10-24 19:21:13255 AutoLock auto_lock(deserializers_lock_);
256 return deserializers_.back().done_event;
257}
258
[email protected]1c4947f2009-01-15 22:25:11259WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
[email protected]3cdb7af812008-10-24 19:21:13260 return received_sync_msgs_->dispatch_event();
initial.commit09911bf2008-07-26 23:55:29261}
262
263void SyncChannel::SyncContext::DispatchMessages() {
264 received_sync_msgs_->DispatchMessages();
265}
266
[email protected]3cdb7af812008-10-24 19:21:13267bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
[email protected]63a7bb82008-10-25 00:46:00268 AutoLock auto_lock(deserializers_lock_);
269 if (deserializers_.empty() ||
270 !SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
271 return false;
[email protected]3cdb7af812008-10-24 19:21:13272 }
initial.commit09911bf2008-07-26 23:55:29273
[email protected]63a7bb82008-10-25 00:46:00274 if (!msg->is_reply_error()) {
275 deserializers_.back().send_result = deserializers_.back().deserializer->
276 SerializeOutputParameters(*msg);
277 }
[email protected]1c4947f2009-01-15 22:25:11278 deserializers_.back().done_event->Signal();
initial.commit09911bf2008-07-26 23:55:29279
[email protected]3cdb7af812008-10-24 19:21:13280 return true;
initial.commit09911bf2008-07-26 23:55:29281}
282
[email protected]3cdb7af812008-10-24 19:21:13283void SyncChannel::SyncContext::Clear() {
284 CancelPendingSends();
[email protected]d3ae7a072008-12-05 20:27:20285 received_sync_msgs_->RemoveContext(this);
[email protected]3cdb7af812008-10-24 19:21:13286
287 Context::Clear();
288}
289
initial.commit09911bf2008-07-26 23:55:29290void SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
[email protected]d65cab7a2008-08-12 01:25:41291 // Give the filters a chance at processing this message.
292 if (TryFilters(msg))
293 return;
294
[email protected]3cdb7af812008-10-24 19:21:13295 if (TryToUnblockListener(&msg))
initial.commit09911bf2008-07-26 23:55:29296 return;
297
298 if (msg.should_unblock()) {
[email protected]d3ae7a072008-12-05 20:27:20299 received_sync_msgs_->QueueMessage(msg, this);
initial.commit09911bf2008-07-26 23:55:29300 return;
301 }
302
303 if (msg.is_reply()) {
304 received_sync_msgs_->QueueReply(msg, this);
305 return;
306 }
307
[email protected]3cdb7af812008-10-24 19:21:13308 return Context::OnMessageReceivedNoFilter(msg);
initial.commit09911bf2008-07-26 23:55:29309}
310
initial.commit09911bf2008-07-26 23:55:29311void SyncChannel::SyncContext::OnChannelError() {
[email protected]3cdb7af812008-10-24 19:21:13312 CancelPendingSends();
[email protected]a4f822702009-02-06 00:44:53313 shutdown_watcher_.StopWatching();
initial.commit09911bf2008-07-26 23:55:29314 Context::OnChannelError();
315}
316
[email protected]3cdb7af812008-10-24 19:21:13317void SyncChannel::SyncContext::OnChannelOpened() {
318 shutdown_watcher_.StartWatching(shutdown_event_, this);
319 Context::OnChannelOpened();
initial.commit09911bf2008-07-26 23:55:29320}
321
[email protected]3cdb7af812008-10-24 19:21:13322void SyncChannel::SyncContext::OnChannelClosed() {
323 shutdown_watcher_.StopWatching();
324 Context::OnChannelClosed();
325}
326
327void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
328 AutoLock auto_lock(deserializers_lock_);
329 PendingSyncMessageQueue::iterator iter;
330 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) {
[email protected]d3ae7a072008-12-05 20:27:20331 if (iter->id == message_id) {
[email protected]1c4947f2009-01-15 22:25:11332 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13333 break;
334 }
335 }
336}
337
338void SyncChannel::SyncContext::CancelPendingSends() {
339 AutoLock auto_lock(deserializers_lock_);
340 PendingSyncMessageQueue::iterator iter;
341 for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
[email protected]1c4947f2009-01-15 22:25:11342 iter->done_event->Signal();
[email protected]3cdb7af812008-10-24 19:21:13343}
344
[email protected]1c4947f2009-01-15 22:25:11345void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
346 DCHECK(event == shutdown_event_);
[email protected]3cdb7af812008-10-24 19:21:13347 // Process shut down before we can get a reply to a synchronous message.
348 // Cancel pending Send calls, which will end up setting the send done event.
349 CancelPendingSends();
350}
351
352
353SyncChannel::SyncChannel(
[email protected]9a3a293b2009-06-04 22:28:16354 const std::string& channel_id, Channel::Mode mode,
[email protected]3cdb7af812008-10-24 19:21:13355 Channel::Listener* listener, MessageFilter* filter,
[email protected]1c4947f2009-01-15 22:25:11356 MessageLoop* ipc_message_loop, bool create_pipe_now,
357 WaitableEvent* shutdown_event)
[email protected]3cdb7af812008-10-24 19:21:13358 : ChannelProxy(
359 channel_id, mode, ipc_message_loop,
360 new SyncContext(listener, filter, ipc_message_loop, shutdown_event),
361 create_pipe_now),
[email protected]d65cab7a2008-08-12 01:25:41362 sync_messages_with_no_timeout_allowed_(true) {
[email protected]3cdb7af812008-10-24 19:21:13363 // Ideally we only want to watch this object when running a nested message
364 // loop. However, we don't know when it exits if there's another nested
365 // message loop running under it or not, so we wouldn't know whether to
366 // stop or keep watching. So we always watch it, and create the event as
367 // manual reset since the object watcher might otherwise reset the event
[email protected]1c4947f2009-01-15 22:25:11368 // when we're doing a WaitMany.
[email protected]3cdb7af812008-10-24 19:21:13369 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this);
initial.commit09911bf2008-07-26 23:55:29370}
371
372SyncChannel::~SyncChannel() {
initial.commit09911bf2008-07-26 23:55:29373}
374
[email protected]3cdb7af812008-10-24 19:21:13375bool SyncChannel::Send(Message* message) {
[email protected]aa96ae772009-01-20 22:08:15376 return SendWithTimeout(message, base::kNoTimeout);
[email protected]d65cab7a2008-08-12 01:25:41377}
378
[email protected]3cdb7af812008-10-24 19:21:13379bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) {
380 if (!message->is_sync()) {
381 ChannelProxy::Send(message);
382 return true;
initial.commit09911bf2008-07-26 23:55:29383 }
384
[email protected]3cdb7af812008-10-24 19:21:13385 // *this* might get deleted in WaitForReply.
386 scoped_refptr<SyncContext> context(sync_context());
[email protected]1c4947f2009-01-15 22:25:11387 if (context->shutdown_event()->IsSignaled()) {
[email protected]3cdb7af812008-10-24 19:21:13388 delete message;
389 return false;
390 }
391
[email protected]d3216442009-03-05 21:07:27392 DCHECK(sync_messages_with_no_timeout_allowed_ ||
393 timeout_ms != base::kNoTimeout);
[email protected]3cdb7af812008-10-24 19:21:13394 SyncMessage* sync_msg = static_cast<SyncMessage*>(message);
395 context->Push(sync_msg);
396 int message_id = SyncMessage::GetMessageId(*sync_msg);
[email protected]1c4947f2009-01-15 22:25:11397 WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
[email protected]3cdb7af812008-10-24 19:21:13398
initial.commit09911bf2008-07-26 23:55:29399 ChannelProxy::Send(message);
initial.commit09911bf2008-07-26 23:55:29400
[email protected]aa96ae772009-01-20 22:08:15401 if (timeout_ms != base::kNoTimeout) {
[email protected]3cdb7af812008-10-24 19:21:13402 // We use the sync message id so that when a message times out, we don't
403 // confuse it with another send that is either above/below this Send in
404 // the call stack.
[email protected]f0a51fb52009-03-05 12:46:38405 context->ipc_message_loop()->PostDelayedTask(FROM_HERE,
[email protected]3cdb7af812008-10-24 19:21:13406 NewRunnableMethod(context.get(),
407 &SyncContext::OnSendTimeout, message_id), timeout_ms);
408 }
initial.commit09911bf2008-07-26 23:55:29409
[email protected]3cdb7af812008-10-24 19:21:13410 // Wait for reply, or for any other incoming synchronous messages.
411 WaitForReply(pump_messages_event);
initial.commit09911bf2008-07-26 23:55:29412
[email protected]3cdb7af812008-10-24 19:21:13413 return context->Pop();
initial.commit09911bf2008-07-26 23:55:29414}
415
[email protected]1c4947f2009-01-15 22:25:11416void SyncChannel::WaitForReply(WaitableEvent* pump_messages_event) {
[email protected]3cdb7af812008-10-24 19:21:13417 while (true) {
[email protected]1c4947f2009-01-15 22:25:11418 WaitableEvent* objects[] = {
419 sync_context()->GetDispatchEvent(),
420 sync_context()->GetSendDoneEvent(),
421 pump_messages_event
422 };
423
424 unsigned count = pump_messages_event ? 3: 2;
[email protected]7dc8d792009-11-20 17:30:44425 size_t result = WaitableEvent::WaitMany(objects, count);
[email protected]1c4947f2009-01-15 22:25:11426 if (result == 0 /* dispatch event */) {
[email protected]3cdb7af812008-10-24 19:21:13427 // We're waiting for a reply, but we received a blocking synchronous
428 // call. We must process it or otherwise a deadlock might occur.
[email protected]1c4947f2009-01-15 22:25:11429 sync_context()->GetDispatchEvent()->Reset();
[email protected]3cdb7af812008-10-24 19:21:13430 sync_context()->DispatchMessages();
431 continue;
432 }
433
[email protected]1c4947f2009-01-15 22:25:11434 if (result == 2 /* pump_messages_event */)
[email protected]3cdb7af812008-10-24 19:21:13435 WaitForReplyWithNestedMessageLoop(); // Start a nested message loop.
436
437 break;
438 }
439}
440
441void SyncChannel::WaitForReplyWithNestedMessageLoop() {
[email protected]ac0efda2009-10-14 16:22:02442 base::WaitableEventWatcher send_done_watcher;
443
444 ReceivedSyncMsgQueue* sync_msg_queue = sync_context()->received_sync_msgs();
445 DCHECK(sync_msg_queue != NULL);
446
447 base::WaitableEventWatcher* old_send_done_event_watcher =
448 sync_msg_queue->top_send_done_watcher();
449
450 base::WaitableEventWatcher::Delegate* old_delegate = NULL;
451 base::WaitableEvent* old_event = NULL;
452
453 // Maintain a local global stack of send done delegates to ensure that
454 // nested sync calls complete in the correct sequence, i.e. the
455 // outermost call completes first, etc.
456 if (old_send_done_event_watcher) {
457 old_delegate = old_send_done_event_watcher->delegate();
458 old_event = old_send_done_event_watcher->GetWatchedEvent();
459 old_send_done_event_watcher->StopWatching();
460 }
461
462 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
463
464 send_done_watcher.StartWatching(sync_context()->GetSendDoneEvent(), this);
[email protected]3cdb7af812008-10-24 19:21:13465 bool old_state = MessageLoop::current()->NestableTasksAllowed();
[email protected]ac0efda2009-10-14 16:22:02466
[email protected]3cdb7af812008-10-24 19:21:13467 MessageLoop::current()->SetNestableTasksAllowed(true);
468 MessageLoop::current()->Run();
469 MessageLoop::current()->SetNestableTasksAllowed(old_state);
[email protected]ac0efda2009-10-14 16:22:02470
471 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
[email protected]424379802009-10-14 19:58:13472 if (old_send_done_event_watcher && old_event) {
[email protected]ac0efda2009-10-14 16:22:02473 old_send_done_event_watcher->StartWatching(old_event, old_delegate);
474 }
[email protected]3cdb7af812008-10-24 19:21:13475}
476
[email protected]1c4947f2009-01-15 22:25:11477void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
478 WaitableEvent* dispatch_event = sync_context()->GetDispatchEvent();
479 if (event == dispatch_event) {
[email protected]3cdb7af812008-10-24 19:21:13480 // The call to DispatchMessages might delete this object, so reregister
481 // the object watcher first.
[email protected]1c4947f2009-01-15 22:25:11482 dispatch_event->Reset();
[email protected]3cdb7af812008-10-24 19:21:13483 dispatch_watcher_.StartWatching(dispatch_event, this);
484 sync_context()->DispatchMessages();
485 } else {
486 // We got the reply, timed out or the process shutdown.
[email protected]1c4947f2009-01-15 22:25:11487 DCHECK(event == sync_context()->GetSendDoneEvent());
[email protected]3cdb7af812008-10-24 19:21:13488 MessageLoop::current()->Quit();
489 }
initial.commit09911bf2008-07-26 23:55:29490}
491
492} // namespace IPC