2009-06-29 18:38:29 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
2009-07-13 21:55:04 +00:00
|
|
|
*/
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
#include "mozilla/ipc/RPCChannel.h"
|
2010-01-27 06:41:32 +00:00
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2009-11-12 22:46:29 +00:00
|
|
|
#include "nsTraceRefcnt.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
#define RPC_ASSERT(_cond, ...) \
|
|
|
|
do { \
|
|
|
|
if (!(_cond)) \
|
|
|
|
DebugAbort(__FILE__, __LINE__, #_cond,## __VA_ARGS__); \
|
|
|
|
} while (0)
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
using mozilla::MonitorAutoLock;
|
|
|
|
using mozilla::MonitorAutoUnlock;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::RPCChannel>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::RPCChannel* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::RPCChannel* obj) { }
|
|
|
|
};
|
|
|
|
|
2010-01-27 06:41:32 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// Async (from the sending side's perspective)
|
|
|
|
class BlockChildMessage : public IPC::Message
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum { ID = BLOCK_CHILD_MESSAGE_TYPE };
|
|
|
|
BlockChildMessage() :
|
|
|
|
Message(MSG_ROUTING_NONE, ID, IPC::Message::PRIORITY_NORMAL)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Async
|
|
|
|
class UnblockChildMessage : public IPC::Message
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum { ID = UNBLOCK_CHILD_MESSAGE_TYPE };
|
|
|
|
UnblockChildMessage() :
|
|
|
|
Message(MSG_ROUTING_NONE, ID, IPC::Message::PRIORITY_NORMAL)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace <anon>
|
|
|
|
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2010-03-11 07:35:30 +00:00
|
|
|
RPCChannel::RPCChannel(RPCListener* aListener)
|
2009-11-12 22:16:54 +00:00
|
|
|
: SyncChannel(aListener),
|
|
|
|
mPending(),
|
|
|
|
mStack(),
|
2010-01-22 02:04:10 +00:00
|
|
|
mOutOfTurnReplies(),
|
2009-11-12 22:16:54 +00:00
|
|
|
mDeferred(),
|
|
|
|
mRemoteStackDepthGuess(0),
|
2010-03-24 05:52:47 +00:00
|
|
|
mBlockedOnParent(false),
|
|
|
|
mSawRPCOutMsg(false)
|
2009-11-12 22:16:54 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(RPCChannel);
|
2010-02-15 07:47:00 +00:00
|
|
|
|
|
|
|
mDequeueOneTask = new RefCountedTask(NewRunnableMethod(
|
|
|
|
this,
|
|
|
|
&RPCChannel::OnMaybeDequeueOne));
|
2009-11-12 22:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RPCChannel::~RPCChannel()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(RPCChannel);
|
2010-03-18 22:52:32 +00:00
|
|
|
RPC_ASSERT(mCxxStackFrames.empty(), "mismatched CxxStackFrame ctor/dtors");
|
2009-11-12 22:16:54 +00:00
|
|
|
}
|
|
|
|
|
2010-02-15 07:47:00 +00:00
|
|
|
void
|
|
|
|
RPCChannel::Clear()
|
|
|
|
{
|
|
|
|
mDequeueOneTask->Cancel();
|
|
|
|
|
|
|
|
AsyncChannel::Clear();
|
|
|
|
}
|
|
|
|
|
2010-02-10 00:02:54 +00:00
|
|
|
bool
|
2010-04-27 05:42:59 +00:00
|
|
|
RPCChannel::EventOccurred() const
|
2010-02-10 00:02:54 +00:00
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-02-10 00:02:54 +00:00
|
|
|
RPC_ASSERT(StackDepth() > 0, "not in wait loop");
|
|
|
|
|
|
|
|
return (!Connected() ||
|
|
|
|
!mPending.empty() ||
|
|
|
|
(!mOutOfTurnReplies.empty() &&
|
|
|
|
mOutOfTurnReplies.find(mStack.top().seqno())
|
|
|
|
!= mOutOfTurnReplies.end()));
|
|
|
|
}
|
|
|
|
|
2010-02-16 18:44:21 +00:00
|
|
|
bool
|
|
|
|
RPCChannel::Send(Message* msg)
|
|
|
|
{
|
2010-03-18 22:52:32 +00:00
|
|
|
Message copy = *msg;
|
|
|
|
CxxStackFrame f(*this, OUT_MESSAGE, ©);
|
2010-02-16 18:44:21 +00:00
|
|
|
return AsyncChannel::Send(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RPCChannel::Send(Message* msg, Message* reply)
|
|
|
|
{
|
2010-03-18 22:52:32 +00:00
|
|
|
Message copy = *msg;
|
|
|
|
CxxStackFrame f(*this, OUT_MESSAGE, ©);
|
2010-02-16 18:44:21 +00:00
|
|
|
return SyncChannel::Send(msg, reply);
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
bool
|
2011-11-30 13:19:49 +00:00
|
|
|
RPCChannel::Call(Message* _msg, Message* reply)
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2011-11-30 13:19:49 +00:00
|
|
|
nsAutoPtr<Message> msg(_msg);
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2009-10-09 06:21:39 +00:00
|
|
|
RPC_ASSERT(!ProcessingSyncMessage(),
|
|
|
|
"violation of sync handler invariant");
|
|
|
|
RPC_ASSERT(msg->is_rpc(), "can only Call() RPC messages here");
|
2009-09-22 02:02:15 +00:00
|
|
|
|
2010-04-28 15:01:09 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
SyncStackFrame frame(this, true);
|
|
|
|
#endif
|
|
|
|
|
2010-03-18 22:52:32 +00:00
|
|
|
Message copy = *msg;
|
|
|
|
CxxStackFrame f(*this, OUT_MESSAGE, ©);
|
2010-02-16 18:44:21 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2009-10-08 21:44:43 +00:00
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("RPCChannel");
|
2009-09-22 02:02:15 +00:00
|
|
|
return false;
|
2009-10-27 21:32:55 +00:00
|
|
|
}
|
2009-07-02 05:45:19 +00:00
|
|
|
|
2010-01-22 02:04:09 +00:00
|
|
|
msg->set_seqno(NextSeqno());
|
2009-10-08 21:44:43 +00:00
|
|
|
msg->set_rpc_remote_stack_depth_guess(mRemoteStackDepthGuess);
|
2010-01-22 02:04:09 +00:00
|
|
|
msg->set_rpc_local_stack_depth(1 + StackDepth());
|
|
|
|
mStack.push(*msg);
|
2009-08-19 05:22:01 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
mLink->SendMessage(msg.forget());
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
while (1) {
|
2010-03-07 20:16:02 +00:00
|
|
|
// if a handler invoked by *Dispatch*() spun a nested event
|
|
|
|
// loop, and the connection was broken during that loop, we
|
|
|
|
// might have already processed the OnError event. if so,
|
|
|
|
// trying another loop iteration will be futile because
|
|
|
|
// channel state will have been cleared
|
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("RPCChannel");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-12-09 23:15:01 +00:00
|
|
|
// now might be the time to process a message deferred because
|
|
|
|
// of race resolution
|
2011-01-12 07:07:17 +00:00
|
|
|
MaybeUndeferIncall();
|
2009-12-09 23:15:01 +00:00
|
|
|
|
2009-09-11 07:28:09 +00:00
|
|
|
// here we're waiting for something to happen. see long
|
|
|
|
// comment about the queue in RPCChannel.h
|
2010-02-10 00:02:54 +00:00
|
|
|
while (!EventOccurred()) {
|
|
|
|
bool maybeTimedOut = !RPCChannel::WaitForNotify();
|
|
|
|
|
2010-03-24 21:49:54 +00:00
|
|
|
if (EventOccurred() ||
|
|
|
|
// we might have received a "subtly deferred" message
|
|
|
|
// in a nested loop that it's now time to process
|
|
|
|
(!maybeTimedOut &&
|
|
|
|
(!mDeferred.empty() || !mOutOfTurnReplies.empty())))
|
2010-02-10 00:02:54 +00:00
|
|
|
break;
|
|
|
|
|
2010-02-10 21:41:44 +00:00
|
|
|
if (maybeTimedOut && !ShouldContinueFromTimeout())
|
2010-02-10 00:02:54 +00:00
|
|
|
return false;
|
2009-09-11 07:28:09 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("RPCChannel");
|
2009-09-22 02:02:15 +00:00
|
|
|
return false;
|
2009-10-27 21:32:55 +00:00
|
|
|
}
|
2009-09-22 02:02:15 +00:00
|
|
|
|
2010-01-22 02:04:10 +00:00
|
|
|
Message recvd;
|
2010-02-09 22:34:38 +00:00
|
|
|
MessageMap::iterator it;
|
2010-01-22 02:04:10 +00:00
|
|
|
if (!mOutOfTurnReplies.empty() &&
|
2010-02-09 22:34:38 +00:00
|
|
|
((it = mOutOfTurnReplies.find(mStack.top().seqno())) !=
|
|
|
|
mOutOfTurnReplies.end())) {
|
|
|
|
recvd = it->second;
|
|
|
|
mOutOfTurnReplies.erase(it);
|
2010-01-22 02:04:10 +00:00
|
|
|
}
|
2010-03-24 21:49:54 +00:00
|
|
|
else if (!mPending.empty()) {
|
2010-01-22 02:04:10 +00:00
|
|
|
recvd = mPending.front();
|
2012-08-25 08:25:08 +00:00
|
|
|
mPending.pop_front();
|
2010-01-22 02:04:10 +00:00
|
|
|
}
|
2010-03-24 21:49:54 +00:00
|
|
|
else {
|
|
|
|
// because of subtleties with nested event loops, it's
|
|
|
|
// possible that we got here and nothing happened. or, we
|
|
|
|
// might have a deferred in-call that needs to be
|
|
|
|
// processed. either way, we won't break the inner while
|
|
|
|
// loop again until something new happens.
|
|
|
|
continue;
|
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2009-09-11 07:28:09 +00:00
|
|
|
if (!recvd.is_sync() && !recvd.is_rpc()) {
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoUnlock unlock(*mMonitor);
|
2010-03-18 22:52:32 +00:00
|
|
|
|
|
|
|
CxxStackFrame f(*this, IN_MESSAGE, &recvd);
|
2009-09-11 07:28:09 +00:00
|
|
|
AsyncChannel::OnDispatchMessage(recvd);
|
2010-03-18 22:52:32 +00:00
|
|
|
|
2009-09-11 07:28:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-10 23:54:37 +00:00
|
|
|
if (recvd.is_sync()) {
|
2009-10-09 06:21:39 +00:00
|
|
|
RPC_ASSERT(mPending.empty(),
|
|
|
|
"other side should have been blocked");
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoUnlock unlock(*mMonitor);
|
2010-03-18 22:52:32 +00:00
|
|
|
|
|
|
|
CxxStackFrame f(*this, IN_MESSAGE, &recvd);
|
2009-09-10 23:54:37 +00:00
|
|
|
SyncChannel::OnDispatchMessage(recvd);
|
2010-03-18 22:52:32 +00:00
|
|
|
|
2009-09-10 23:54:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-01-22 02:04:09 +00:00
|
|
|
RPC_ASSERT(recvd.is_rpc(), "wtf???");
|
2009-09-10 23:54:37 +00:00
|
|
|
|
2009-08-19 05:22:01 +00:00
|
|
|
if (recvd.is_reply()) {
|
2009-10-08 22:41:18 +00:00
|
|
|
RPC_ASSERT(0 < mStack.size(), "invalid RPC stack");
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2009-09-11 07:28:09 +00:00
|
|
|
const Message& outcall = mStack.top();
|
2009-08-07 23:13:20 +00:00
|
|
|
|
2010-02-24 21:59:23 +00:00
|
|
|
// in the parent, seqno's increase from 0, and in the
|
|
|
|
// child, they decrease from 0
|
|
|
|
if ((!mChild && recvd.seqno() < outcall.seqno()) ||
|
|
|
|
(mChild && recvd.seqno() > outcall.seqno())) {
|
2010-02-09 22:34:38 +00:00
|
|
|
mOutOfTurnReplies[recvd.seqno()] = recvd;
|
2010-01-22 02:04:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-08 22:41:18 +00:00
|
|
|
// FIXME/cjones: handle error
|
2009-10-09 06:21:39 +00:00
|
|
|
RPC_ASSERT(
|
2010-01-22 02:04:09 +00:00
|
|
|
recvd.is_reply_error() ||
|
|
|
|
(recvd.type() == (outcall.type()+1) &&
|
|
|
|
recvd.seqno() == outcall.seqno()),
|
2009-10-09 06:21:39 +00:00
|
|
|
"somebody's misbehavin'", "rpc", true);
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2009-08-19 05:22:01 +00:00
|
|
|
// we received a reply to our most recent outstanding
|
|
|
|
// call. pop this frame and return the reply
|
2009-09-11 07:28:09 +00:00
|
|
|
mStack.pop();
|
2009-08-07 23:13:20 +00:00
|
|
|
|
|
|
|
bool isError = recvd.is_reply_error();
|
|
|
|
if (!isError) {
|
|
|
|
*reply = recvd;
|
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2010-01-22 02:04:10 +00:00
|
|
|
if (0 == StackDepth()) {
|
|
|
|
RPC_ASSERT(
|
|
|
|
mOutOfTurnReplies.empty(),
|
|
|
|
"still have pending replies with no pending out-calls",
|
|
|
|
"rpc", true);
|
|
|
|
}
|
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
// finished with this RPC stack frame
|
2009-08-07 23:13:20 +00:00
|
|
|
return !isError;
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
// in-call. process in a new stack frame.
|
2009-09-11 07:28:09 +00:00
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
// "snapshot" the current stack depth while we own the Monitor
|
2009-09-11 07:28:09 +00:00
|
|
|
size_t stackDepth = StackDepth();
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoUnlock unlock(*mMonitor);
|
2009-06-29 18:38:29 +00:00
|
|
|
// someone called in to us from the other side. handle the call
|
2010-03-18 22:52:32 +00:00
|
|
|
CxxStackFrame f(*this, IN_MESSAGE, &recvd);
|
2009-10-09 06:21:39 +00:00
|
|
|
Incall(recvd, stackDepth);
|
2009-07-13 21:55:04 +00:00
|
|
|
// FIXME/cjones: error handling
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-12 07:07:17 +00:00
|
|
|
void
|
|
|
|
RPCChannel::MaybeUndeferIncall()
|
2009-09-11 07:28:09 +00:00
|
|
|
{
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2009-10-09 06:21:39 +00:00
|
|
|
|
|
|
|
if (mDeferred.empty())
|
2011-01-12 07:07:17 +00:00
|
|
|
return;
|
2009-10-09 06:21:39 +00:00
|
|
|
|
|
|
|
size_t stackDepth = StackDepth();
|
|
|
|
|
|
|
|
// the other side can only *under*-estimate our actual stack depth
|
|
|
|
RPC_ASSERT(mDeferred.top().rpc_remote_stack_depth_guess() <= stackDepth,
|
|
|
|
"fatal logic error");
|
|
|
|
|
2011-05-04 20:55:54 +00:00
|
|
|
if (mDeferred.top().rpc_remote_stack_depth_guess() < RemoteViewOfStackDepth(stackDepth))
|
2011-01-12 07:07:17 +00:00
|
|
|
return;
|
2009-10-09 06:21:39 +00:00
|
|
|
|
2011-01-12 07:07:17 +00:00
|
|
|
// maybe time to process this message
|
2009-10-09 06:21:39 +00:00
|
|
|
Message call = mDeferred.top();
|
|
|
|
mDeferred.pop();
|
|
|
|
|
|
|
|
// fix up fudge factor we added to account for race
|
|
|
|
RPC_ASSERT(0 < mRemoteStackDepthGuess, "fatal logic error");
|
|
|
|
--mRemoteStackDepthGuess;
|
|
|
|
|
2012-08-25 08:25:08 +00:00
|
|
|
mPending.push_back(call);
|
2009-10-09 06:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RPCChannel::EnqueuePendingMessages()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2009-12-09 23:15:01 +00:00
|
|
|
|
2011-01-12 07:07:17 +00:00
|
|
|
MaybeUndeferIncall();
|
|
|
|
|
2010-02-18 20:12:54 +00:00
|
|
|
for (size_t i = 0; i < mDeferred.size(); ++i)
|
2009-12-09 23:15:01 +00:00
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
2010-02-15 07:47:00 +00:00
|
|
|
new DequeueTask(mDequeueOneTask));
|
2009-12-09 23:15:01 +00:00
|
|
|
|
|
|
|
// XXX performance tuning knob: could process all or k pending
|
|
|
|
// messages here, rather than enqueuing for later processing
|
2009-10-09 06:21:39 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < mPending.size(); ++i)
|
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
2010-02-15 07:47:00 +00:00
|
|
|
new DequeueTask(mDequeueOneTask));
|
2009-09-11 07:28:09 +00:00
|
|
|
}
|
|
|
|
|
2009-09-22 15:23:29 +00:00
|
|
|
void
|
2010-05-21 15:48:34 +00:00
|
|
|
RPCChannel::FlushPendingRPCQueue()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2010-05-21 15:48:34 +00:00
|
|
|
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-05-21 15:48:34 +00:00
|
|
|
|
|
|
|
if (mDeferred.empty()) {
|
|
|
|
if (mPending.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Message& last = mPending.back();
|
|
|
|
if (!last.is_rpc() || last.is_reply())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (OnMaybeDequeueOne());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-09-22 15:23:29 +00:00
|
|
|
RPCChannel::OnMaybeDequeueOne()
|
|
|
|
{
|
2009-10-09 06:21:39 +00:00
|
|
|
// XXX performance tuning knob: could process all or k pending
|
|
|
|
// messages here
|
|
|
|
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2009-10-09 06:21:39 +00:00
|
|
|
|
|
|
|
Message recvd;
|
2009-09-22 15:23:29 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2009-09-22 15:23:29 +00:00
|
|
|
|
2010-03-11 07:35:26 +00:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("RPCChannel");
|
2010-05-21 15:48:34 +00:00
|
|
|
return false;
|
2010-03-11 07:35:26 +00:00
|
|
|
}
|
|
|
|
|
2009-12-09 23:15:01 +00:00
|
|
|
if (!mDeferred.empty())
|
2011-01-12 07:07:17 +00:00
|
|
|
MaybeUndeferIncall();
|
2009-12-09 23:15:01 +00:00
|
|
|
|
2009-09-22 15:23:29 +00:00
|
|
|
if (mPending.empty())
|
2010-05-21 15:48:34 +00:00
|
|
|
return false;
|
2009-09-22 15:23:29 +00:00
|
|
|
|
|
|
|
recvd = mPending.front();
|
2012-08-25 08:25:08 +00:00
|
|
|
mPending.pop_front();
|
2009-09-22 15:23:29 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 06:57:00 +00:00
|
|
|
if (IsOnCxxStack() && recvd.is_rpc() && recvd.is_reply()) {
|
|
|
|
// We probably just received a reply in a nested loop for an
|
|
|
|
// RPC call sent before entering that loop.
|
|
|
|
mOutOfTurnReplies[recvd.seqno()] = recvd;
|
2010-05-21 15:48:34 +00:00
|
|
|
return false;
|
2010-03-19 06:57:00 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 22:52:32 +00:00
|
|
|
CxxStackFrame f(*this, IN_MESSAGE, &recvd);
|
2010-02-16 18:44:21 +00:00
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
if (recvd.is_rpc())
|
2010-05-21 15:48:34 +00:00
|
|
|
Incall(recvd, 0);
|
2009-10-09 06:21:39 +00:00
|
|
|
else if (recvd.is_sync())
|
2010-05-21 15:48:34 +00:00
|
|
|
SyncChannel::OnDispatchMessage(recvd);
|
2009-10-09 06:21:39 +00:00
|
|
|
else
|
2010-05-21 15:48:34 +00:00
|
|
|
AsyncChannel::OnDispatchMessage(recvd);
|
|
|
|
|
|
|
|
return true;
|
2009-08-19 05:22:01 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 20:55:54 +00:00
|
|
|
size_t
|
|
|
|
RPCChannel::RemoteViewOfStackDepth(size_t stackDepth) const
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
return stackDepth - mOutOfTurnReplies.size();
|
|
|
|
}
|
|
|
|
|
2009-10-08 22:41:18 +00:00
|
|
|
void
|
2009-10-09 06:21:39 +00:00
|
|
|
RPCChannel::Incall(const Message& call, size_t stackDepth)
|
2009-08-19 05:22:01 +00:00
|
|
|
{
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2009-10-09 06:21:39 +00:00
|
|
|
RPC_ASSERT(call.is_rpc() && !call.is_reply(), "wrong message type");
|
2009-08-19 05:22:01 +00:00
|
|
|
|
2009-10-08 21:44:43 +00:00
|
|
|
// Race detection: see the long comment near
|
|
|
|
// mRemoteStackDepthGuess in RPCChannel.h. "Remote" stack depth
|
|
|
|
// means our side, and "local" means other side.
|
2011-05-04 20:55:54 +00:00
|
|
|
if (call.rpc_remote_stack_depth_guess() != RemoteViewOfStackDepth(stackDepth)) {
|
2010-03-12 01:17:35 +00:00
|
|
|
// RPC in-calls have raced.
|
2009-10-08 21:44:43 +00:00
|
|
|
// the "winner", if there is one, gets to defer processing of
|
|
|
|
// the other side's in-call
|
|
|
|
bool defer;
|
|
|
|
const char* winner;
|
2010-03-11 07:35:30 +00:00
|
|
|
switch (Listener()->MediateRPCRace(mChild ? call : mStack.top(),
|
|
|
|
mChild ? mStack.top() : call)) {
|
2009-10-08 21:44:43 +00:00
|
|
|
case RRPChildWins:
|
|
|
|
winner = "child";
|
|
|
|
defer = mChild;
|
|
|
|
break;
|
|
|
|
case RRPParentWins:
|
|
|
|
winner = "parent";
|
|
|
|
defer = !mChild;
|
|
|
|
break;
|
|
|
|
case RRPError:
|
|
|
|
NS_RUNTIMEABORT("NYI: 'Error' RPC race policy");
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
NS_RUNTIMEABORT("not reached");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-06 19:17:08 +00:00
|
|
|
if (LoggingEnabled()) {
|
2012-09-05 17:11:05 +00:00
|
|
|
printf_stderr(" (%s: %s won, so we're%sdeferring)\n",
|
|
|
|
mChild ? "child" : "parent", winner,
|
|
|
|
defer ? " " : " not ");
|
2010-05-06 19:17:08 +00:00
|
|
|
}
|
2009-10-08 21:44:43 +00:00
|
|
|
|
|
|
|
if (defer) {
|
2009-10-09 06:21:39 +00:00
|
|
|
// we now know the other side's stack has one more frame
|
|
|
|
// than we thought
|
|
|
|
++mRemoteStackDepthGuess; // decremented in MaybeProcessDeferred()
|
|
|
|
mDeferred.push(call);
|
2009-10-08 21:44:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
// we "lost" and need to process the other side's in-call.
|
|
|
|
// don't need to fix up the mRemoteStackDepthGuess here,
|
|
|
|
// because we're just about to increment it in DispatchCall(),
|
|
|
|
// which will make it correct again
|
2009-10-08 21:44:43 +00:00
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2011-05-18 11:57:08 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
SyncStackFrame frame(this, true);
|
|
|
|
#endif
|
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
DispatchIncall(call);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RPCChannel::DispatchIncall(const Message& call)
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2009-10-09 06:21:39 +00:00
|
|
|
RPC_ASSERT(call.is_rpc() && !call.is_reply(),
|
|
|
|
"wrong message type");
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
Message* reply = nullptr;
|
2009-08-19 05:22:01 +00:00
|
|
|
|
2009-10-08 21:44:43 +00:00
|
|
|
++mRemoteStackDepthGuess;
|
2010-03-11 07:35:30 +00:00
|
|
|
Result rv = Listener()->OnCallReceived(call, reply);
|
2009-10-08 21:44:43 +00:00
|
|
|
--mRemoteStackDepthGuess;
|
2009-08-19 05:22:01 +00:00
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
if (!MaybeHandleError(rv, "RPCChannel")) {
|
2009-08-07 23:13:20 +00:00
|
|
|
delete reply;
|
|
|
|
reply = new Message();
|
|
|
|
reply->set_rpc();
|
|
|
|
reply->set_reply();
|
|
|
|
reply->set_reply_error();
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
2009-09-14 20:00:31 +00:00
|
|
|
|
2010-01-22 02:04:09 +00:00
|
|
|
reply->set_seqno(call.seqno());
|
|
|
|
|
2010-02-11 23:33:53 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-02-11 23:33:53 +00:00
|
|
|
if (ChannelConnected == mChannelState)
|
2011-11-30 16:24:46 +00:00
|
|
|
mLink->SendMessage(reply);
|
2010-02-11 23:33:53 +00:00
|
|
|
}
|
2009-10-09 06:21:39 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 06:41:32 +00:00
|
|
|
bool
|
|
|
|
RPCChannel::BlockChild()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
if (mChild)
|
|
|
|
NS_RUNTIMEABORT("child tried to block parent");
|
2011-11-30 16:24:46 +00:00
|
|
|
|
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-01-27 06:41:32 +00:00
|
|
|
SendSpecialMessage(new BlockChildMessage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RPCChannel::UnblockChild()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
if (mChild)
|
|
|
|
NS_RUNTIMEABORT("child tried to unblock parent");
|
2011-11-30 16:24:46 +00:00
|
|
|
|
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-01-27 06:41:32 +00:00
|
|
|
SendSpecialMessage(new UnblockChildMessage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-09-17 08:37:20 +00:00
|
|
|
RPCChannel::OnSpecialMessage(uint16_t id, const Message& msg)
|
2010-01-27 06:41:32 +00:00
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case BLOCK_CHILD_MESSAGE_TYPE:
|
|
|
|
BlockOnParent();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case UNBLOCK_CHILD_MESSAGE_TYPE:
|
|
|
|
UnblockFromParent();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return SyncChannel::OnSpecialMessage(id, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RPCChannel::BlockOnParent()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
if (!mChild)
|
|
|
|
NS_RUNTIMEABORT("child tried to block parent");
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-01-27 06:41:32 +00:00
|
|
|
|
|
|
|
if (mBlockedOnParent || AwaitingSyncReply() || 0 < StackDepth())
|
|
|
|
NS_RUNTIMEABORT("attempt to block child when it's already blocked");
|
|
|
|
|
|
|
|
mBlockedOnParent = true;
|
2010-01-27 08:17:17 +00:00
|
|
|
do {
|
2010-01-27 06:41:32 +00:00
|
|
|
// XXX this dispatch loop shares some similarities with the
|
|
|
|
// one in Call(), but the logic is simpler and different
|
|
|
|
// enough IMHO to warrant its own dispatch loop
|
|
|
|
while (Connected() && mPending.empty() && mBlockedOnParent) {
|
|
|
|
WaitForNotify();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Connected()) {
|
|
|
|
mBlockedOnParent = false;
|
|
|
|
ReportConnectionError("RPCChannel");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPending.empty()) {
|
|
|
|
Message recvd = mPending.front();
|
2012-08-25 08:25:08 +00:00
|
|
|
mPending.pop_front();
|
2010-01-27 06:41:32 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoUnlock unlock(*mMonitor);
|
2010-03-18 22:52:32 +00:00
|
|
|
|
|
|
|
CxxStackFrame f(*this, IN_MESSAGE, &recvd);
|
2010-01-27 06:41:32 +00:00
|
|
|
if (recvd.is_rpc()) {
|
|
|
|
// stack depth must be 0 here
|
|
|
|
Incall(recvd, 0);
|
|
|
|
}
|
|
|
|
else if (recvd.is_sync()) {
|
|
|
|
SyncChannel::OnDispatchMessage(recvd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
AsyncChannel::OnDispatchMessage(recvd);
|
|
|
|
}
|
|
|
|
}
|
2010-01-27 08:17:17 +00:00
|
|
|
} while (mBlockedOnParent);
|
2010-01-27 06:41:32 +00:00
|
|
|
|
|
|
|
EnqueuePendingMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RPCChannel::UnblockFromParent()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
if (!mChild)
|
|
|
|
NS_RUNTIMEABORT("child tried to block parent");
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-01-27 06:41:32 +00:00
|
|
|
mBlockedOnParent = false;
|
|
|
|
}
|
2009-09-14 20:00:31 +00:00
|
|
|
|
2010-03-18 22:52:33 +00:00
|
|
|
void
|
|
|
|
RPCChannel::ExitedCxxStack()
|
|
|
|
{
|
|
|
|
Listener()->OnExitedCxxStack();
|
2010-03-24 05:52:47 +00:00
|
|
|
if (mSawRPCOutMsg) {
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-03-18 22:52:33 +00:00
|
|
|
// see long comment in OnMaybeDequeueOne()
|
|
|
|
EnqueuePendingMessages();
|
2010-03-24 05:52:47 +00:00
|
|
|
mSawRPCOutMsg = false;
|
2010-03-18 22:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
void
|
|
|
|
RPCChannel::DebugAbort(const char* file, int line, const char* cond,
|
|
|
|
const char* why,
|
2010-04-27 05:42:59 +00:00
|
|
|
const char* type, bool reply) const
|
2009-10-09 06:21:39 +00:00
|
|
|
{
|
2012-09-05 17:11:05 +00:00
|
|
|
printf_stderr("###!!! [RPCChannel][%s][%s:%d] "
|
|
|
|
"Assertion (%s) failed. %s (triggered by %s%s)\n",
|
|
|
|
mChild ? "Child" : "Parent",
|
|
|
|
file, line, cond,
|
|
|
|
why,
|
|
|
|
type, reply ? "reply" : "");
|
2009-10-09 06:21:39 +00:00
|
|
|
// technically we need the mutex for this, but we're dying anyway
|
2012-09-05 17:11:05 +00:00
|
|
|
DumpRPCStack(" ");
|
|
|
|
printf_stderr(" remote RPC stack guess: %lu\n",
|
|
|
|
mRemoteStackDepthGuess);
|
|
|
|
printf_stderr(" deferred stack size: %lu\n",
|
|
|
|
mDeferred.size());
|
|
|
|
printf_stderr(" out-of-turn RPC replies stack size: %lu\n",
|
|
|
|
mOutOfTurnReplies.size());
|
|
|
|
printf_stderr(" Pending queue size: %lu, front to back:\n",
|
|
|
|
mPending.size());
|
2010-04-27 05:42:59 +00:00
|
|
|
|
|
|
|
MessageQueue pending = mPending;
|
|
|
|
while (!pending.empty()) {
|
2012-09-05 17:11:05 +00:00
|
|
|
printf_stderr(" [ %s%s ]\n",
|
|
|
|
pending.front().is_rpc() ? "rpc" :
|
|
|
|
(pending.front().is_sync() ? "sync" : "async"),
|
|
|
|
pending.front().is_reply() ? "reply" : "");
|
2012-08-25 08:25:08 +00:00
|
|
|
pending.pop_front();
|
2009-10-09 06:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_RUNTIMEABORT(why);
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 22:52:32 +00:00
|
|
|
void
|
2012-09-05 17:11:05 +00:00
|
|
|
RPCChannel::DumpRPCStack(const char* const pfx) const
|
2010-03-18 22:52:32 +00:00
|
|
|
{
|
|
|
|
NS_WARN_IF_FALSE(MessageLoop::current() != mWorkerLoop,
|
|
|
|
"The worker thread had better be paused in a debugger!");
|
|
|
|
|
2012-09-05 17:11:05 +00:00
|
|
|
printf_stderr("%sRPCChannel 'backtrace':\n", pfx);
|
2010-03-18 22:52:32 +00:00
|
|
|
|
|
|
|
// print a python-style backtrace, first frame to last
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mCxxStackFrames.size(); ++i) {
|
2012-09-17 08:37:20 +00:00
|
|
|
int32_t id;
|
2010-03-18 22:52:32 +00:00
|
|
|
const char* dir, *sems, *name;
|
|
|
|
mCxxStackFrames[i].Describe(&id, &dir, &sems, &name);
|
|
|
|
|
2012-09-05 17:11:05 +00:00
|
|
|
printf_stderr("%s[(%u) %s %s %s(actor=%d) ]\n", pfx,
|
|
|
|
i, dir, sems, name, id);
|
2010-03-18 22:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
//
|
2011-11-30 16:24:46 +00:00
|
|
|
// The methods below run in the context of the link thread, and can proxy
|
2009-06-29 18:38:29 +00:00
|
|
|
// back to the methods above
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2011-11-30 16:24:46 +00:00
|
|
|
RPCChannel::OnMessageReceivedFromLink(const Message& msg)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2010-03-11 07:35:26 +00:00
|
|
|
if (MaybeInterceptSpecialIOMessage(msg))
|
|
|
|
return;
|
|
|
|
|
2009-09-22 15:23:29 +00:00
|
|
|
// regardless of the RPC stack, if we're awaiting a sync reply, we
|
|
|
|
// know that it needs to be immediately handled to unblock us.
|
2009-10-09 06:21:39 +00:00
|
|
|
if (AwaitingSyncReply() && msg.is_sync()) {
|
|
|
|
// wake up worker thread waiting at SyncChannel::Send
|
2009-09-22 15:23:29 +00:00
|
|
|
mRecvd = msg;
|
2009-10-09 06:21:39 +00:00
|
|
|
NotifyWorkerThread();
|
2009-09-22 15:23:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-25 08:25:08 +00:00
|
|
|
bool compressMessage = (msg.compress() && !mPending.empty() &&
|
|
|
|
mPending.back().type() == msg.type() &&
|
|
|
|
mPending.back().routing_id() == msg.routing_id());
|
|
|
|
if (compressMessage) {
|
|
|
|
// This message type has compression enabled, and the back of
|
|
|
|
// the queue was the same message type and routed to the same
|
|
|
|
// destination. Replace it with the newer message.
|
|
|
|
MOZ_ASSERT(mPending.back().compress());
|
|
|
|
mPending.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
mPending.push_back(msg);
|
2009-09-22 15:23:29 +00:00
|
|
|
|
2010-02-15 07:47:00 +00:00
|
|
|
if (0 == StackDepth() && !mBlockedOnParent) {
|
2009-10-09 06:21:39 +00:00
|
|
|
// the worker thread might be idle, make sure it wakes up
|
2012-08-25 08:25:08 +00:00
|
|
|
if (!compressMessage) {
|
|
|
|
// If we compressed away the previous message, we'll reuse
|
|
|
|
// its pending task.
|
|
|
|
mWorkerLoop->PostTask(FROM_HERE, new DequeueTask(mDequeueOneTask));
|
|
|
|
}
|
2010-02-15 07:47:00 +00:00
|
|
|
}
|
2010-02-10 00:02:54 +00:00
|
|
|
else if (!AwaitingSyncReply())
|
2009-10-09 06:21:39 +00:00
|
|
|
NotifyWorkerThread();
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-22 02:02:15 +00:00
|
|
|
void
|
2011-11-30 16:24:46 +00:00
|
|
|
RPCChannel::OnChannelErrorFromLink()
|
2009-09-22 02:02:15 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2009-09-22 02:02:15 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
if (0 < StackDepth())
|
2010-03-18 22:52:28 +00:00
|
|
|
NotifyWorkerThread();
|
2010-02-19 20:39:38 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
SyncChannel::OnChannelErrorFromLink();
|
2009-09-22 02:02:15 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
2009-10-09 06:21:39 +00:00
|
|
|
|