2011-06-12 05:37:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: set sw=2 ts=8 et tw=80 :
|
|
|
|
*/
|
2012-05-21 15:12:37 +04: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/. */
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2017-03-18 06:36:08 +03:00
|
|
|
#include "ChannelEventQueue.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2016-09-02 10:12:24 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-03-18 06:36:08 +03:00
|
|
|
#include "nsISupports.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2011-06-12 05:37:09 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2016-03-14 19:10:26 +03:00
|
|
|
ChannelEvent*
|
|
|
|
ChannelEventQueue::TakeEvent()
|
|
|
|
{
|
2017-05-08 14:16:06 +03:00
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2016-03-14 19:10:26 +03:00
|
|
|
MOZ_ASSERT(mFlushing);
|
|
|
|
|
|
|
|
if (mSuspended || mEventQueue.IsEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<ChannelEvent> event(Move(mEventQueue[0]));
|
|
|
|
mEventQueue.RemoveElementAt(0);
|
|
|
|
|
|
|
|
return event.release();
|
|
|
|
}
|
|
|
|
|
2011-06-12 05:37:09 +04:00
|
|
|
void
|
|
|
|
ChannelEventQueue::FlushQueue()
|
|
|
|
{
|
|
|
|
// Events flushed could include destruction of channel (and our own
|
|
|
|
// destructor) unless we make sure its refcount doesn't drop to 0 while this
|
|
|
|
// method is running.
|
2011-05-04 17:36:23 +04:00
|
|
|
nsCOMPtr<nsISupports> kungFuDeathGrip(mOwner);
|
2016-07-18 19:44:45 +03:00
|
|
|
mozilla::Unused << kungFuDeathGrip; // Not used in this function
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2018-01-04 13:53:02 +03:00
|
|
|
#ifdef DEBUG
|
2016-03-14 19:10:26 +03:00
|
|
|
{
|
2017-05-08 14:16:06 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2018-01-04 13:53:02 +03:00
|
|
|
MOZ_ASSERT(mFlushing);
|
2017-05-08 14:16:06 +03:00
|
|
|
}
|
2018-01-04 13:53:02 +03:00
|
|
|
#endif // DEBUG
|
2017-05-08 14:16:06 +03:00
|
|
|
|
|
|
|
bool needResumeOnOtherThread = false;
|
2017-03-18 06:36:08 +03:00
|
|
|
|
2017-05-08 14:16:06 +03:00
|
|
|
while (true) {
|
|
|
|
UniquePtr<ChannelEvent> event;
|
2017-03-18 06:36:08 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-05-08 14:16:06 +03:00
|
|
|
event.reset(TakeEvent());
|
2017-03-18 06:36:08 +03:00
|
|
|
if (!event) {
|
2017-05-08 14:16:06 +03:00
|
|
|
MOZ_ASSERT(mFlushing);
|
|
|
|
mFlushing = false;
|
|
|
|
MOZ_ASSERT(mEventQueue.IsEmpty() || (mSuspended || !!mForcedCount));
|
2017-03-18 06:36:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-05-08 14:16:06 +03:00
|
|
|
}
|
2017-03-18 06:36:08 +03:00
|
|
|
|
2017-05-08 14:16:06 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target = event->GetEventTarget();
|
|
|
|
MOZ_ASSERT(target);
|
2017-03-18 06:36:08 +03:00
|
|
|
|
2017-05-08 14:16:06 +03:00
|
|
|
bool isCurrentThread = false;
|
|
|
|
nsresult rv = target->IsOnCurrentThread(&isCurrentThread);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// Simply run this event on current thread if we are not sure about it
|
|
|
|
// in release channel, or assert in Aurora/Nightly channel.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(false);
|
|
|
|
isCurrentThread = true;
|
|
|
|
}
|
2017-03-18 06:36:08 +03:00
|
|
|
|
2017-05-08 14:16:06 +03:00
|
|
|
if (!isCurrentThread) {
|
|
|
|
// Next event needs to run on another thread. Put it back to
|
|
|
|
// the front of the queue can try resume on that thread.
|
|
|
|
Suspend();
|
|
|
|
PrependEvent(event);
|
|
|
|
|
|
|
|
needResumeOnOtherThread = true;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(mFlushing);
|
|
|
|
mFlushing = false;
|
|
|
|
MOZ_ASSERT(!mEventQueue.IsEmpty());
|
2017-03-18 06:36:08 +03:00
|
|
|
}
|
2017-05-08 14:16:06 +03:00
|
|
|
break;
|
2017-03-18 06:36:08 +03:00
|
|
|
}
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2017-05-08 14:16:06 +03:00
|
|
|
event->Run();
|
|
|
|
} // end of while(true)
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2017-03-18 06:36:08 +03:00
|
|
|
// The flush procedure is aborted because next event cannot be run on current
|
|
|
|
// thread. We need to resume the event processing right after flush procedure
|
|
|
|
// is finished.
|
|
|
|
// Note: we cannot call Resume() while "mFlushing == true" because
|
|
|
|
// CompleteResume will not trigger FlushQueue while there is an ongoing flush.
|
|
|
|
if (needResumeOnOtherThread) {
|
|
|
|
Resume();
|
2016-03-14 19:10:26 +03:00
|
|
|
}
|
2017-03-18 06:36:08 +03:00
|
|
|
}
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2017-03-18 06:36:08 +03:00
|
|
|
void
|
|
|
|
ChannelEventQueue::Suspend()
|
|
|
|
{
|
2016-03-14 19:10:26 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-03-18 06:36:08 +03:00
|
|
|
SuspendInternal();
|
2011-06-12 05:37:09 +04:00
|
|
|
}
|
|
|
|
|
2013-09-19 17:54:39 +04:00
|
|
|
void
|
2017-03-18 06:36:08 +03:00
|
|
|
ChannelEventQueue::SuspendInternal()
|
|
|
|
{
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
mSuspended = true;
|
|
|
|
mSuspendCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelEventQueue::Resume()
|
2013-09-19 17:54:39 +04:00
|
|
|
{
|
2016-03-14 19:10:26 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-03-18 06:36:08 +03:00
|
|
|
ResumeInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelEventQueue::ResumeInternal()
|
|
|
|
{
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
2016-03-14 19:10:26 +03:00
|
|
|
|
2013-09-19 17:54:39 +04:00
|
|
|
// Resuming w/o suspend: error in debug mode, ignore in build
|
|
|
|
MOZ_ASSERT(mSuspendCount > 0);
|
|
|
|
if (mSuspendCount <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!--mSuspendCount) {
|
2018-01-04 13:53:02 +03:00
|
|
|
if (mEventQueue.IsEmpty() || !!mForcedCount) {
|
|
|
|
// Nothing in queue to flush or waiting for AutoEventEnqueuer to
|
|
|
|
// finish the force enqueue period, simply clear the flag.
|
2017-03-18 06:36:08 +03:00
|
|
|
mSuspended = false;
|
|
|
|
return;
|
2014-03-28 00:58:19 +04:00
|
|
|
}
|
|
|
|
|
2017-06-09 14:59:41 +03:00
|
|
|
// Hold a strong reference of mOwner to avoid the channel release
|
|
|
|
// before CompleteResume was executed.
|
|
|
|
class CompleteResumeRunnable : public CancelableRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit CompleteResumeRunnable(ChannelEventQueue* aQueue, nsISupports* aOwner)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("CompleteResumeRunnable")
|
|
|
|
, mQueue(aQueue)
|
2017-06-09 14:59:41 +03:00
|
|
|
, mOwner(aOwner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override
|
|
|
|
{
|
|
|
|
mQueue->CompleteResume();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~CompleteResumeRunnable() {}
|
|
|
|
|
|
|
|
RefPtr<ChannelEventQueue> mQueue;
|
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
};
|
|
|
|
|
2017-03-18 06:36:08 +03:00
|
|
|
// Worker thread requires a CancelableRunnable.
|
2017-06-09 14:59:41 +03:00
|
|
|
RefPtr<Runnable> event = new CompleteResumeRunnable(this, mOwner);
|
2016-11-16 03:13:17 +03:00
|
|
|
|
2017-03-18 06:36:08 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target;
|
|
|
|
target = mEventQueue[0]->GetEventTarget();
|
|
|
|
MOZ_ASSERT(target);
|
2016-11-16 03:13:17 +03:00
|
|
|
|
2017-03-18 06:36:08 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(target->Dispatch(event.forget(),
|
|
|
|
NS_DISPATCH_NORMAL)));
|
|
|
|
}
|
2016-11-16 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|