Bug 1300118 P1 Make TaskQueue deliver runnables to nsIEventTarget. r=bholley

This commit is contained in:
Ben Kelly 2016-09-12 11:21:01 -07:00
Родитель 396475783e
Коммит 157b0fe1ed
5 изменённых файлов: 19 добавлений и 18 удалений

Просмотреть файл

@ -13,6 +13,7 @@
#include "mozilla/CDMProxy.h"
#endif
#include "mozilla/Logging.h"
#include "mozilla/SharedThreadPool.h"
#include "nsMimeTypes.h"
#include "nsContentTypeParser.h"
#include "VideoUtils.h"

Просмотреть файл

@ -8,6 +8,7 @@
#define MOZILLA_AUTOTASKQUEUE_H_
#include "mozilla/RefPtr.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/TaskQueue.h"
namespace mozilla {

Просмотреть файл

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/StateWatching.h"
#include "mozilla/TaskQueue.h"
#include "nsISupportsImpl.h"

Просмотреть файл

@ -7,14 +7,13 @@
#include "mozilla/TaskQueue.h"
#include "nsThreadUtils.h"
#include "mozilla/SharedThreadPool.h"
namespace mozilla {
TaskQueue::TaskQueue(already_AddRefed<SharedThreadPool> aPool,
bool aRequireTailDispatch)
TaskQueue::TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
bool aRequireTailDispatch)
: AbstractThread(aRequireTailDispatch)
, mPool(aPool)
, mTarget(aTarget)
, mQueueMonitor("TaskQueue::Queue")
, mTailDispatcher(nullptr)
, mIsRunning(false)
@ -64,7 +63,7 @@ TaskQueue::DispatchLocked(nsCOMPtr<nsIRunnable>& aRunnable,
return NS_OK;
}
RefPtr<nsIRunnable> runner(new Runner(this));
nsresult rv = mPool->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
nsresult rv = mTarget->Dispatch(runner.forget(), NS_DISPATCH_NORMAL);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to dispatch runnable to run TaskQueue");
return rv;
@ -191,11 +190,11 @@ TaskQueue::Runner::Run()
}
// There's at least one more event that we can run. Dispatch this Runner
// to the thread pool again to ensure it runs again. Note that we don't just
// run in a loop here so that we don't hog the thread pool. This means we may
// to the target again to ensure it runs again. Note that we don't just
// run in a loop here so that we don't hog the target. This means we may
// run on another thread next time, but we rely on the memory fences from
// mQueueMonitor for thread safety of non-threadsafe tasks.
nsresult rv = mQueue->mPool->DispatchFromEndOfTaskInThisPool(this);
nsresult rv = mQueue->mTarget->Dispatch(this, NS_DISPATCH_AT_END);
if (NS_FAILED(rv)) {
// Failed to dispatch, shutdown!
MonitorAutoLock mon(mQueue->mQueueMonitor);

Просмотреть файл

@ -15,25 +15,24 @@
#include <queue>
#include "mozilla/SharedThreadPool.h"
#include "nsThreadUtils.h"
class nsIEventTarget;
class nsIRunnable;
namespace mozilla {
class SharedThreadPool;
typedef MozPromise<bool, bool, false> ShutdownPromise;
// Abstracts executing runnables in order in a thread pool. The runnables
// dispatched to the TaskQueue will be executed in the order in which
// Abstracts executing runnables in order on an arbitrary event target. The
// runnables dispatched to the TaskQueue will be executed in the order in which
// they're received, and are guaranteed to not be executed concurrently.
// They may be executed on different threads, and a memory barrier is used
// to make this threadsafe for objects that aren't already threadsafe.
class TaskQueue : public AbstractThread {
public:
explicit TaskQueue(already_AddRefed<SharedThreadPool> aPool, bool aSupportsTailDispatch = false);
explicit TaskQueue(already_AddRefed<nsIEventTarget> aTarget,
bool aSupportsTailDispatch = false);
TaskDispatcher& TailDispatcher() override;
@ -59,7 +58,7 @@ public:
// Puts the queue in a shutdown state and returns immediately. The queue will
// remain alive at least until all the events are drained, because the Runners
// hold a strong reference to the task queue, and one of them is always held
// by the threadpool event queue when the task queue is non-empty.
// by the target event queue when the task queue is non-empty.
//
// The returned promise is resolved when the queue goes empty.
RefPtr<ShutdownPromise> BeginShutdown();
@ -98,11 +97,11 @@ protected:
mQueueMonitor.AssertCurrentThreadOwns();
if (mIsShutdown && !mIsRunning) {
mShutdownPromise.ResolveIfExists(true, __func__);
mPool = nullptr;
mTarget = nullptr;
}
}
RefPtr<SharedThreadPool> mPool;
nsCOMPtr<nsIEventTarget> mTarget;
// Monitor that protects the queue and mIsRunning;
Monitor mQueueMonitor;
@ -156,7 +155,7 @@ protected:
TaskDispatcher* mTailDispatcher;
// True if we've dispatched an event to the pool to execute events from
// True if we've dispatched an event to the target to execute events from
// the queue.
bool mIsRunning;