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
|
|
|
|
2011-05-04 17:36:23 +04:00
|
|
|
#include "nsISupports.h"
|
2011-06-12 05:37:09 +04:00
|
|
|
#include "mozilla/net/ChannelEventQueue.h"
|
2016-09-02 10:12:24 +03:00
|
|
|
#include "mozilla/Unused.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()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
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);
|
2011-06-12 05:37:09 +04:00
|
|
|
|
|
|
|
// Prevent flushed events from flushing the queue recursively
|
2016-03-14 19:10:26 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mFlushing = true;
|
2011-06-12 05:37:09 +04:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:10:26 +03:00
|
|
|
while (true) {
|
|
|
|
UniquePtr<ChannelEvent> event(TakeEvent());
|
|
|
|
if (!event) {
|
|
|
|
break;
|
|
|
|
}
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2016-03-14 19:10:26 +03:00
|
|
|
event->Run();
|
|
|
|
}
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2016-03-14 19:10:26 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2011-06-12 05:37:09 +04:00
|
|
|
mFlushing = false;
|
|
|
|
}
|
|
|
|
|
2013-09-19 17:54:39 +04:00
|
|
|
void
|
|
|
|
ChannelEventQueue::Resume()
|
|
|
|
{
|
2016-03-14 19:10:26 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
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) {
|
2016-05-05 11:45:00 +03:00
|
|
|
RefPtr<Runnable> event =
|
|
|
|
NewRunnableMethod(this, &ChannelEventQueue::CompleteResume);
|
2014-03-28 00:58:19 +04:00
|
|
|
if (mTargetThread) {
|
2016-05-05 11:45:00 +03:00
|
|
|
mTargetThread->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
|
2014-03-28 00:58:19 +04:00
|
|
|
} else {
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(NS_DispatchToCurrentThread(event.forget())));
|
2014-03-28 00:58:19 +04:00
|
|
|
}
|
2013-09-19 17:54:39 +04:00
|
|
|
}
|
|
|
|
}
|
2011-06-12 05:37:09 +04:00
|
|
|
|
2014-03-28 00:58:19 +04:00
|
|
|
nsresult
|
|
|
|
ChannelEventQueue::RetargetDeliveryTo(nsIEventTarget* aTargetThread)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_RELEASE_ASSERT(!mTargetThread);
|
|
|
|
MOZ_RELEASE_ASSERT(aTargetThread);
|
|
|
|
|
|
|
|
mTargetThread = do_QueryInterface(aTargetThread);
|
|
|
|
MOZ_RELEASE_ASSERT(mTargetThread);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|