2009-07-14 01:55:04 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Plugin App.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Chris Jones <jones.chris.g@gmail.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "mozilla/ipc/SyncChannel.h"
|
|
|
|
#include "mozilla/ipc/GeckoThread.h"
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2009-11-13 01:46:29 +03:00
|
|
|
#include "nsTraceRefcnt.h"
|
2009-07-14 01:55:04 +04:00
|
|
|
|
|
|
|
using mozilla::MutexAutoLock;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::SyncChannel>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::SyncChannel* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::SyncChannel* obj) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2009-11-13 01:16:54 +03:00
|
|
|
SyncChannel::SyncChannel(SyncListener* aListener)
|
|
|
|
: AsyncChannel(aListener),
|
|
|
|
mPendingReply(0),
|
|
|
|
mProcessingSyncMessage(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SyncChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncChannel::~SyncChannel()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SyncChannel);
|
|
|
|
// FIXME/cjones: impl
|
|
|
|
}
|
|
|
|
|
2009-11-19 02:18:08 +03:00
|
|
|
// static
|
|
|
|
bool SyncChannel::sIsPumpingMessages = false;
|
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
bool
|
|
|
|
SyncChannel::Send(Message* msg, Message* reply)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertWorkerThread();
|
2009-12-03 11:16:28 +03:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2009-08-19 19:44:56 +04:00
|
|
|
NS_ABORT_IF_FALSE(!ProcessingSyncMessage(),
|
|
|
|
"violation of sync handler invariant");
|
2009-09-22 06:02:15 +04:00
|
|
|
NS_ABORT_IF_FALSE(msg->is_sync(), "can only Send() sync messages here");
|
|
|
|
|
2009-10-09 01:44:43 +04:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("SyncChannel");
|
2009-09-22 06:02:15 +04:00
|
|
|
return false;
|
2009-10-28 00:32:55 +03:00
|
|
|
}
|
2009-07-14 01:55:04 +04:00
|
|
|
|
|
|
|
mPendingReply = msg->type() + 1;
|
2009-10-09 10:21:39 +04:00
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &SyncChannel::OnSend, msg));
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2010-01-21 06:50:36 +03:00
|
|
|
// NB: this is a do-while loop instead of a single wait because if
|
|
|
|
// there's a pending RPC out- or in-call below us, and the sync
|
|
|
|
// message handler on the other side sends us an async message,
|
|
|
|
// the IO thread will Notify() this thread of the async message.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=538239.
|
|
|
|
do {
|
|
|
|
// wait for the next sync message to arrive
|
|
|
|
WaitForNotify();
|
|
|
|
} while(Connected() &&
|
|
|
|
mPendingReply != mRecvd.type() && !mRecvd.is_reply_error());
|
2009-08-19 19:44:56 +04:00
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("SyncChannel");
|
2009-09-22 06:02:15 +04:00
|
|
|
return false;
|
2009-10-28 00:32:55 +03:00
|
|
|
}
|
2009-09-22 06:02:15 +04:00
|
|
|
|
2009-08-19 19:44:56 +04: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
|
2009-09-15 00:00:31 +04:00
|
|
|
NS_ABORT_IF_FALSE(mRecvd.is_sync() && mRecvd.is_reply() &&
|
2010-01-21 06:50:36 +03:00
|
|
|
(mPendingReply == mRecvd.type() ||
|
|
|
|
mRecvd.is_reply_error()),
|
2009-08-19 19:44:56 +04:00
|
|
|
"unexpected sync message");
|
|
|
|
|
|
|
|
mPendingReply = 0;
|
|
|
|
*reply = mRecvd;
|
2010-01-21 06:50:36 +03:00
|
|
|
mRecvd = Message();
|
2009-08-19 19:44:56 +04:00
|
|
|
|
|
|
|
return true;
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SyncChannel::OnDispatchMessage(const Message& msg)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertWorkerThread();
|
2009-08-19 19:44:56 +04:00
|
|
|
NS_ABORT_IF_FALSE(msg.is_sync(), "only sync messages here");
|
2009-09-11 03:54:37 +04:00
|
|
|
NS_ABORT_IF_FALSE(!msg.is_reply(), "wasn't awaiting reply");
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2009-09-17 06:49:41 +04:00
|
|
|
Message* reply = 0;
|
2009-08-19 19:44:56 +04:00
|
|
|
|
|
|
|
mProcessingSyncMessage = true;
|
|
|
|
Result rv =
|
|
|
|
static_cast<SyncListener*>(mListener)->OnMessageReceived(msg, reply);
|
|
|
|
mProcessingSyncMessage = false;
|
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
if (!MaybeHandleError(rv, "SyncChannel")) {
|
2009-07-14 01:55:04 +04:00
|
|
|
// FIXME/cjones: error handling; OnError()?
|
2009-09-15 00:00:31 +04:00
|
|
|
delete reply;
|
|
|
|
reply = new Message();
|
|
|
|
reply->set_sync();
|
|
|
|
reply->set_reply();
|
|
|
|
reply->set_reply_error();
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
2009-09-15 00:00:31 +04:00
|
|
|
|
2009-10-09 10:21:39 +04:00
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &SyncChannel::OnSend, reply));
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// The methods below run in the context of the IO thread, and can proxy
|
|
|
|
// back to the methods above
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
SyncChannel::OnMessageReceived(const Message& msg)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertIOThread();
|
2009-08-19 19:44:56 +04:00
|
|
|
if (!msg.is_sync()) {
|
|
|
|
return AsyncChannel::OnMessageReceived(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2009-08-19 19:44:56 +04:00
|
|
|
if (!AwaitingSyncReply()) {
|
2009-07-14 01:55:04 +04:00
|
|
|
// wake up the worker, there's work to do
|
2009-08-19 19:44:56 +04:00
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &SyncChannel::OnDispatchMessage, msg));
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
2009-08-19 19:44:56 +04:00
|
|
|
else {
|
|
|
|
// let the worker know a new sync message has arrived
|
2009-07-14 01:55:04 +04:00
|
|
|
mRecvd = msg;
|
2009-10-09 10:21:39 +04:00
|
|
|
NotifyWorkerThread();
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-22 06:02:15 +04:00
|
|
|
void
|
|
|
|
SyncChannel::OnChannelError()
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertIOThread();
|
2009-09-22 06:02:15 +04:00
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
AsyncChannel::OnChannelError();
|
2009-09-22 06:02:15 +04:00
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (AwaitingSyncReply())
|
|
|
|
NotifyWorkerThread();
|
2009-09-22 06:02:15 +04:00
|
|
|
}
|
|
|
|
|
2009-10-09 10:21:39 +04:00
|
|
|
//
|
|
|
|
// Synchronization between worker and IO threads
|
|
|
|
//
|
|
|
|
|
2009-11-19 02:18:08 +03:00
|
|
|
// Windows versions of the following two functions live in
|
|
|
|
// WindowsMessageLoop.cpp.
|
2009-11-07 01:33:12 +03:00
|
|
|
|
2009-11-19 02:18:08 +03:00
|
|
|
#ifndef OS_WIN
|
2009-11-07 01:33:12 +03:00
|
|
|
|
2009-11-07 02:51:04 +03:00
|
|
|
void
|
2009-10-09 10:21:39 +04:00
|
|
|
SyncChannel::WaitForNotify()
|
|
|
|
{
|
|
|
|
mCvar.Wait();
|
|
|
|
}
|
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
void
|
2009-10-09 10:21:39 +04:00
|
|
|
SyncChannel::NotifyWorkerThread()
|
2009-07-14 01:55:04 +04:00
|
|
|
{
|
2009-10-09 10:21:39 +04:00
|
|
|
mCvar.Notify();
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
2009-11-19 02:18:08 +03:00
|
|
|
#endif // ifndef OS_WIN
|
2009-11-07 01:33:12 +03:00
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|