2009-07-13 14:55:04 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
2012-05-21 04:12:37 -07: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-07-13 14:55:04 -07:00
|
|
|
|
2012-12-14 15:58:45 -08:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
#include "mozilla/ipc/SyncChannel.h"
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2009-11-12 14:46:29 -08:00
|
|
|
#include "nsTraceRefcnt.h"
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
using mozilla::MonitorAutoLock;
|
2009-07-13 14:55:04 -07:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::SyncChannel>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::SyncChannel* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::SyncChannel* obj) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
const int32_t SyncChannel::kNoTimeout = INT32_MIN;
|
2010-02-09 16:02:54 -08:00
|
|
|
|
2009-11-12 14:16:54 -08:00
|
|
|
SyncChannel::SyncChannel(SyncListener* aListener)
|
2010-04-28 08:01:09 -07:00
|
|
|
: AsyncChannel(aListener)
|
2012-10-04 01:32:45 -07:00
|
|
|
#ifdef OS_WIN
|
|
|
|
, mTopFrame(NULL)
|
|
|
|
#endif
|
2010-04-28 08:01:09 -07:00
|
|
|
, mPendingReply(0)
|
|
|
|
, mProcessingSyncMessage(false)
|
|
|
|
, mNextSeqno(0)
|
2012-01-06 10:17:03 -08:00
|
|
|
, mInTimeoutSecondHalf(false)
|
2012-01-30 13:06:33 -08:00
|
|
|
, mTimeoutMs(kNoTimeout)
|
2009-11-12 14:16:54 -08:00
|
|
|
{
|
2010-03-25 14:53:10 -07:00
|
|
|
MOZ_COUNT_CTOR(SyncChannel);
|
|
|
|
#ifdef OS_WIN
|
|
|
|
mEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
NS_ASSERTION(mEvent, "CreateEvent failed! Nothing is going to work!");
|
|
|
|
#endif
|
2009-11-12 14:16:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
SyncChannel::~SyncChannel()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SyncChannel);
|
2010-03-25 14:53:10 -07:00
|
|
|
#ifdef OS_WIN
|
|
|
|
CloseHandle(mEvent);
|
|
|
|
#endif
|
2009-11-12 14:16:54 -08:00
|
|
|
}
|
|
|
|
|
2009-11-18 15:18:08 -08:00
|
|
|
// static
|
|
|
|
bool SyncChannel::sIsPumpingMessages = false;
|
|
|
|
|
2010-02-09 16:02:54 -08:00
|
|
|
bool
|
|
|
|
SyncChannel::EventOccurred()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 08:24:46 -08:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-02-09 16:02:54 -08:00
|
|
|
NS_ABORT_IF_FALSE(AwaitingSyncReply(), "not in wait loop");
|
|
|
|
|
2011-07-13 22:43:43 -07:00
|
|
|
return (!Connected() || 0 != mRecvd.type() || mRecvd.is_reply_error());
|
2010-02-09 16:02:54 -08:00
|
|
|
}
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
bool
|
2011-11-30 05:19:49 -08:00
|
|
|
SyncChannel::Send(Message* _msg, Message* reply)
|
2009-07-13 14:55:04 -07:00
|
|
|
{
|
2011-11-30 05:19:49 -08:00
|
|
|
nsAutoPtr<Message> msg(_msg);
|
|
|
|
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertWorkerThread();
|
2011-11-30 08:24:46 -08:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2009-08-19 08:44:56 -07:00
|
|
|
NS_ABORT_IF_FALSE(!ProcessingSyncMessage(),
|
|
|
|
"violation of sync handler invariant");
|
2009-09-21 19:02:15 -07:00
|
|
|
NS_ABORT_IF_FALSE(msg->is_sync(), "can only Send() sync messages here");
|
|
|
|
|
2010-04-28 08:01:09 -07:00
|
|
|
#ifdef OS_WIN
|
|
|
|
SyncStackFrame frame(this, false);
|
|
|
|
#endif
|
|
|
|
|
2010-01-21 18:04:09 -08:00
|
|
|
msg->set_seqno(NextSeqno());
|
|
|
|
|
2011-11-30 08:24:46 -08:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2009-10-08 14:44:43 -07:00
|
|
|
|
2009-10-27 14:32:55 -07:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("SyncChannel");
|
2009-09-21 19:02:15 -07:00
|
|
|
return false;
|
2009-10-27 14:32:55 -07:00
|
|
|
}
|
2009-07-13 14:55:04 -07:00
|
|
|
|
|
|
|
mPendingReply = msg->type() + 1;
|
2012-10-02 17:18:48 -07:00
|
|
|
DebugOnly<int32_t> msgSeqno = msg->seqno();
|
2011-11-30 08:24:46 -08:00
|
|
|
mLink->SendMessage(msg.forget());
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2010-02-09 16:02:54 -08:00
|
|
|
while (1) {
|
|
|
|
bool maybeTimedOut = !SyncChannel::WaitForNotify();
|
|
|
|
|
|
|
|
if (EventOccurred())
|
|
|
|
break;
|
|
|
|
|
2010-02-10 13:41:44 -08:00
|
|
|
if (maybeTimedOut && !ShouldContinueFromTimeout())
|
2010-02-09 16:02:54 -08:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-19 08:44:56 -07:00
|
|
|
|
2009-10-27 14:32:55 -07:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("SyncChannel");
|
2009-09-21 19:02:15 -07:00
|
|
|
return false;
|
2009-10-27 14:32:55 -07:00
|
|
|
}
|
2009-09-21 19:02:15 -07:00
|
|
|
|
2009-08-19 08:44:56 -07:00
|
|
|
// we just received a synchronous message from the other side.
|
|
|
|
// If it's not the reply we were awaiting, there's a serious
|
|
|
|
// error: either a mistimed/malformed message or a sync in-message
|
|
|
|
// that raced with our sync out-message.
|
|
|
|
// (NB: IPDL prevents the latter from occuring in actor code)
|
|
|
|
|
|
|
|
// FIXME/cjones: real error handling
|
2011-07-13 22:43:43 -07:00
|
|
|
bool replyIsError = mRecvd.is_reply_error();
|
2009-09-14 13:00:31 -07:00
|
|
|
NS_ABORT_IF_FALSE(mRecvd.is_sync() && mRecvd.is_reply() &&
|
2011-07-13 22:43:43 -07:00
|
|
|
(replyIsError ||
|
2010-01-21 18:04:09 -08:00
|
|
|
(mPendingReply == mRecvd.type() &&
|
|
|
|
msgSeqno == mRecvd.seqno())),
|
2009-08-19 08:44:56 -07:00
|
|
|
"unexpected sync message");
|
|
|
|
|
|
|
|
mPendingReply = 0;
|
2011-07-13 22:43:43 -07:00
|
|
|
if (!replyIsError) {
|
|
|
|
*reply = mRecvd;
|
|
|
|
}
|
2010-01-20 19:50:36 -08:00
|
|
|
mRecvd = Message();
|
2009-08-19 08:44:56 -07:00
|
|
|
|
2011-07-13 22:43:43 -07:00
|
|
|
return !replyIsError;
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SyncChannel::OnDispatchMessage(const Message& msg)
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertWorkerThread();
|
2009-08-19 08:44:56 -07:00
|
|
|
NS_ABORT_IF_FALSE(msg.is_sync(), "only sync messages here");
|
2009-09-10 16:54:37 -07:00
|
|
|
NS_ABORT_IF_FALSE(!msg.is_reply(), "wasn't awaiting reply");
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2009-09-16 19:49:41 -07:00
|
|
|
Message* reply = 0;
|
2009-08-19 08:44:56 -07:00
|
|
|
|
|
|
|
mProcessingSyncMessage = true;
|
|
|
|
Result rv =
|
|
|
|
static_cast<SyncListener*>(mListener)->OnMessageReceived(msg, reply);
|
|
|
|
mProcessingSyncMessage = false;
|
|
|
|
|
2009-10-27 14:32:55 -07:00
|
|
|
if (!MaybeHandleError(rv, "SyncChannel")) {
|
2009-07-13 14:55:04 -07:00
|
|
|
// FIXME/cjones: error handling; OnError()?
|
2009-09-14 13:00:31 -07:00
|
|
|
delete reply;
|
|
|
|
reply = new Message();
|
|
|
|
reply->set_sync();
|
|
|
|
reply->set_reply();
|
|
|
|
reply->set_reply_error();
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
2009-09-14 13:00:31 -07:00
|
|
|
|
2010-01-21 18:04:09 -08:00
|
|
|
reply->set_seqno(msg.seqno());
|
|
|
|
|
2010-02-11 15:33:53 -08:00
|
|
|
{
|
2011-11-30 08:24:46 -08:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-02-11 15:33:53 -08:00
|
|
|
if (ChannelConnected == mChannelState)
|
2011-11-30 08:24:46 -08:00
|
|
|
mLink->SendMessage(reply);
|
2010-02-11 15:33:53 -08:00
|
|
|
}
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2011-11-30 08:24:46 -08:00
|
|
|
// The methods below run in the context of the link thread, and can proxy
|
2009-07-13 14:55:04 -07:00
|
|
|
// back to the methods above
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2011-11-30 08:24:46 -08:00
|
|
|
SyncChannel::OnMessageReceivedFromLink(const Message& msg)
|
2009-07-13 14:55:04 -07:00
|
|
|
{
|
2011-11-30 08:24:46 -08:00
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
2009-08-19 08:44:56 -07:00
|
|
|
if (!msg.is_sync()) {
|
2011-11-30 08:24:46 -08:00
|
|
|
AsyncChannel::OnMessageReceivedFromLink(msg);
|
|
|
|
return;
|
2009-08-19 08:44:56 -07:00
|
|
|
}
|
|
|
|
|
2010-03-10 23:35:26 -08:00
|
|
|
if (MaybeInterceptSpecialIOMessage(msg))
|
|
|
|
return;
|
|
|
|
|
2009-08-19 08:44:56 -07:00
|
|
|
if (!AwaitingSyncReply()) {
|
2009-07-13 14:55:04 -07:00
|
|
|
// wake up the worker, there's work to do
|
2009-08-19 08:44:56 -07:00
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &SyncChannel::OnDispatchMessage, msg));
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
2009-08-19 08:44:56 -07:00
|
|
|
else {
|
|
|
|
// let the worker know a new sync message has arrived
|
2009-07-13 14:55:04 -07:00
|
|
|
mRecvd = msg;
|
2009-10-08 23:21:39 -07:00
|
|
|
NotifyWorkerThread();
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 19:02:15 -07:00
|
|
|
void
|
2011-11-30 08:24:46 -08:00
|
|
|
SyncChannel::OnChannelErrorFromLink()
|
2009-09-21 19:02:15 -07:00
|
|
|
{
|
2011-11-30 08:24:46 -08:00
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-02-19 12:39:38 -08:00
|
|
|
|
2010-03-18 15:52:28 -07:00
|
|
|
if (AwaitingSyncReply())
|
|
|
|
NotifyWorkerThread();
|
2010-02-19 12:39:38 -08:00
|
|
|
|
2011-11-30 08:24:46 -08:00
|
|
|
AsyncChannel::OnChannelErrorFromLink();
|
2009-09-21 19:02:15 -07:00
|
|
|
}
|
|
|
|
|
2009-10-08 23:21:39 -07:00
|
|
|
//
|
|
|
|
// Synchronization between worker and IO threads
|
|
|
|
//
|
|
|
|
|
2010-02-09 16:02:54 -08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsTimeoutExpired(PRIntervalTime aStart, PRIntervalTime aTimeout)
|
|
|
|
{
|
|
|
|
return (aTimeout != PR_INTERVAL_NO_TIMEOUT) &&
|
|
|
|
(aTimeout <= (PR_IntervalNow() - aStart));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace <anon>
|
|
|
|
|
|
|
|
bool
|
|
|
|
SyncChannel::ShouldContinueFromTimeout()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 08:24:46 -08:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-02-09 16:02:54 -08:00
|
|
|
|
2010-02-09 16:02:55 -08:00
|
|
|
bool cont;
|
|
|
|
{
|
2011-11-30 08:24:46 -08:00
|
|
|
MonitorAutoUnlock unlock(*mMonitor);
|
2010-02-09 16:02:55 -08:00
|
|
|
cont = static_cast<SyncListener*>(mListener)->OnReplyTimeout();
|
|
|
|
}
|
2010-02-09 16:02:54 -08:00
|
|
|
|
|
|
|
if (!cont) {
|
|
|
|
// NB: there's a sublety here. If parents were allowed to
|
|
|
|
// send sync messages to children, then it would be possible
|
|
|
|
// for this synchronous close-on-timeout to race with async
|
|
|
|
// |OnMessageReceived| tasks arriving from the child, posted
|
|
|
|
// to the worker thread's event loop. This would complicate
|
|
|
|
// cleanup of the *Channel. But since IPDL forbids this (and
|
|
|
|
// since it doesn't support children timing out on parents),
|
|
|
|
// the parent can only block on RPC messages to the child, and
|
|
|
|
// in that case arriving async messages are enqueued to the
|
|
|
|
// RPC channel's special queue. They're then ignored because
|
|
|
|
// the channel state changes to ChannelTimeout
|
|
|
|
// (i.e. !Connected).
|
|
|
|
SynchronouslyClose();
|
|
|
|
mChannelState = ChannelTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
2012-01-06 10:17:03 -08:00
|
|
|
bool
|
|
|
|
SyncChannel::WaitResponse(bool aWaitTimedOut)
|
|
|
|
{
|
|
|
|
if (aWaitTimedOut) {
|
|
|
|
if (mInTimeoutSecondHalf) {
|
|
|
|
// We've really timed out this time
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Try a second time
|
|
|
|
mInTimeoutSecondHalf = true;
|
|
|
|
} else {
|
|
|
|
mInTimeoutSecondHalf = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-18 15:18:08 -08:00
|
|
|
// Windows versions of the following two functions live in
|
|
|
|
// WindowsMessageLoop.cpp.
|
2009-11-06 14:33:12 -08:00
|
|
|
|
2009-11-18 15:18:08 -08:00
|
|
|
#ifndef OS_WIN
|
2009-11-06 14:33:12 -08:00
|
|
|
|
2010-02-09 16:02:54 -08:00
|
|
|
bool
|
2009-10-08 23:21:39 -07:00
|
|
|
SyncChannel::WaitForNotify()
|
|
|
|
{
|
2010-02-09 16:02:54 -08:00
|
|
|
PRIntervalTime timeout = (kNoTimeout == mTimeoutMs) ?
|
|
|
|
PR_INTERVAL_NO_TIMEOUT :
|
|
|
|
PR_MillisecondsToInterval(mTimeoutMs);
|
|
|
|
// XXX could optimize away this syscall for "no timeout" case if desired
|
|
|
|
PRIntervalTime waitStart = PR_IntervalNow();
|
|
|
|
|
2011-11-30 08:24:46 -08:00
|
|
|
mMonitor->Wait(timeout);
|
2010-02-09 16:02:54 -08:00
|
|
|
|
|
|
|
// if the timeout didn't expire, we know we received an event.
|
|
|
|
// The converse is not true.
|
2012-01-06 10:17:03 -08:00
|
|
|
return WaitResponse(IsTimeoutExpired(waitStart, timeout));
|
2009-10-08 23:21:39 -07:00
|
|
|
}
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
void
|
2009-10-08 23:21:39 -07:00
|
|
|
SyncChannel::NotifyWorkerThread()
|
2009-07-13 14:55:04 -07:00
|
|
|
{
|
2011-11-30 08:24:46 -08:00
|
|
|
mMonitor->Notify();
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
2009-11-18 15:18:08 -08:00
|
|
|
#endif // ifndef OS_WIN
|
2009-11-06 14:33:12 -08:00
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|