зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1054638 - Notify the worker event loop when an XPCOM event is received, r=khuey.
This commit is contained in:
Родитель
c8b921018d
Коммит
2d1d91d847
|
@ -1542,8 +1542,10 @@ RuntimeService::ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|||
}
|
||||
}
|
||||
|
||||
const WorkerThreadFriendKey friendKey;
|
||||
|
||||
if (!thread) {
|
||||
thread = WorkerThread::Create();
|
||||
thread = WorkerThread::Create(friendKey);
|
||||
if (!thread) {
|
||||
UnregisterWorker(aCx, aWorkerPrivate);
|
||||
JS_ReportError(aCx, "Could not create new thread!");
|
||||
|
@ -1551,8 +1553,6 @@ RuntimeService::ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|||
}
|
||||
}
|
||||
|
||||
MOZ_ASSERT(thread->IsAcceptingNonWorkerRunnables());
|
||||
|
||||
int32_t priority = aWorkerPrivate->IsChromeWorker() ?
|
||||
nsISupportsPriority::PRIORITY_NORMAL :
|
||||
nsISupportsPriority::PRIORITY_LOW;
|
||||
|
@ -1563,7 +1563,7 @@ RuntimeService::ScheduleWorker(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|||
|
||||
nsCOMPtr<nsIRunnable> runnable =
|
||||
new WorkerThreadPrimaryRunnable(aWorkerPrivate, thread, JS_GetParentRuntime(aCx));
|
||||
if (NS_FAILED(thread->Dispatch(runnable, NS_DISPATCH_NORMAL))) {
|
||||
if (NS_FAILED(thread->DispatchPrimaryRunnable(friendKey, runnable))) {
|
||||
UnregisterWorker(aCx, aWorkerPrivate);
|
||||
JS_ReportError(aCx, "Could not dispatch to thread!");
|
||||
return false;
|
||||
|
@ -2315,10 +2315,6 @@ RuntimeService::NoteIdleThread(WorkerThread* aThread)
|
|||
AssertIsOnMainThread();
|
||||
MOZ_ASSERT(aThread);
|
||||
|
||||
#ifdef DEBUG
|
||||
aThread->SetAcceptingNonWorkerRunnables(true);
|
||||
#endif
|
||||
|
||||
static TimeDuration timeout =
|
||||
TimeDuration::FromSeconds(IDLE_THREAD_TIMEOUT_SEC);
|
||||
|
||||
|
@ -2566,15 +2562,17 @@ WorkerThreadPrimaryRunnable::Run()
|
|||
|
||||
char stackBaseGuess;
|
||||
|
||||
PR_SetCurrentThreadName("DOM Worker");
|
||||
|
||||
nsAutoCString threadName;
|
||||
threadName.AssignLiteral("WebWorker '");
|
||||
threadName.AssignLiteral("DOM Worker '");
|
||||
threadName.Append(NS_LossyConvertUTF16toASCII(mWorkerPrivate->ScriptURL()));
|
||||
threadName.Append('\'');
|
||||
|
||||
profiler_register_thread(threadName.get(), &stackBaseGuess);
|
||||
|
||||
// Note: SynchronouslyCreatePBackground() must be called prior to
|
||||
// mThread->SetWorker() in order to avoid accidentally consuming
|
||||
// mWorkerPrivate->SetThread() in order to avoid accidentally consuming
|
||||
// worker messages here.
|
||||
nsresult rv = SynchronouslyCreatePBackground();
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
|
@ -2582,7 +2580,7 @@ WorkerThreadPrimaryRunnable::Run()
|
|||
return rv;
|
||||
}
|
||||
|
||||
mThread->SetWorker(mWorkerPrivate);
|
||||
mWorkerPrivate->SetThread(mThread);
|
||||
|
||||
#ifdef ENABLE_TESTS
|
||||
TestPBackground();
|
||||
|
@ -2643,7 +2641,7 @@ WorkerThreadPrimaryRunnable::Run()
|
|||
// participating.
|
||||
}
|
||||
|
||||
mThread->SetWorker(nullptr);
|
||||
mWorkerPrivate->SetThread(nullptr);
|
||||
|
||||
mWorkerPrivate->ScheduleDeletion(WorkerPrivate::WorkerRan);
|
||||
|
||||
|
@ -2688,10 +2686,6 @@ WorkerThreadPrimaryRunnable::SynchronouslyCreatePBackground()
|
|||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
mThread->SetAcceptingNonWorkerRunnables(false);
|
||||
#endif
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -65,6 +65,7 @@
|
|||
#include "nsPrintfCString.h"
|
||||
#include "nsProxyRelease.h"
|
||||
#include "nsSandboxFlags.h"
|
||||
#include "prthread.h"
|
||||
#include "xpcpublic.h"
|
||||
|
||||
#ifdef ANDROID
|
||||
|
@ -86,6 +87,7 @@
|
|||
#include "WorkerFeature.h"
|
||||
#include "WorkerRunnable.h"
|
||||
#include "WorkerScope.h"
|
||||
#include "WorkerThread.h"
|
||||
|
||||
// JS_MaybeGC will run once every second during normal execution.
|
||||
#define PERIODIC_GC_TIMER_DELAY_SEC 1
|
||||
|
@ -2130,15 +2132,13 @@ WorkerPrivateParent<Derived>::DispatchPrivate(WorkerRunnable* aRunnable,
|
|||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIEventTarget> target;
|
||||
nsresult rv;
|
||||
if (aSyncLoopTarget) {
|
||||
target = aSyncLoopTarget;
|
||||
}
|
||||
else {
|
||||
target = self->mThread;
|
||||
rv = aSyncLoopTarget->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
|
||||
} else {
|
||||
rv = self->mThread->Dispatch(WorkerThreadFriendKey(), aRunnable);
|
||||
}
|
||||
|
||||
nsresult rv = target->Dispatch(aRunnable, NS_DISPATCH_NORMAL);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
@ -3713,19 +3713,25 @@ WorkerPrivate::WorkerPrivate(JSContext* aCx,
|
|||
bool aIsChromeWorker, WorkerType aWorkerType,
|
||||
const nsACString& aSharedWorkerName,
|
||||
LoadInfo& aLoadInfo)
|
||||
: WorkerPrivateParent<WorkerPrivate>(aCx, aParent, aScriptURL,
|
||||
aIsChromeWorker, aWorkerType,
|
||||
aSharedWorkerName, aLoadInfo),
|
||||
mJSContext(nullptr), mErrorHandlerRecursionCount(0), mNextTimeoutId(1),
|
||||
mStatus(Pending), mSuspended(false), mTimerRunning(false),
|
||||
mRunningExpiredTimeouts(false), mCloseHandlerStarted(false),
|
||||
mCloseHandlerFinished(false), mMemoryReporterRunning(false),
|
||||
mBlockedForMemoryReporter(false), mCancelAllPendingRunnables(false),
|
||||
mPeriodicGCTimerRunning(false), mIdleGCTimerRunning(false),
|
||||
mWorkerScriptExecutedSuccessfully(false)
|
||||
#ifdef DEBUG
|
||||
: WorkerPrivateParent<WorkerPrivate>(aCx, aParent, aScriptURL,
|
||||
aIsChromeWorker, aWorkerType,
|
||||
aSharedWorkerName, aLoadInfo)
|
||||
, mJSContext(nullptr)
|
||||
, mPRThread(nullptr)
|
||||
#endif
|
||||
, mErrorHandlerRecursionCount(0)
|
||||
, mNextTimeoutId(1)
|
||||
, mStatus(Pending)
|
||||
, mSuspended(false)
|
||||
, mTimerRunning(false)
|
||||
, mRunningExpiredTimeouts(false)
|
||||
, mCloseHandlerStarted(false)
|
||||
, mCloseHandlerFinished(false)
|
||||
, mMemoryReporterRunning(false)
|
||||
, mBlockedForMemoryReporter(false)
|
||||
, mCancelAllPendingRunnables(false)
|
||||
, mPeriodicGCTimerRunning(false)
|
||||
, mIdleGCTimerRunning(false)
|
||||
, mWorkerScriptExecutedSuccessfully(false)
|
||||
{
|
||||
MOZ_ASSERT_IF(!IsDedicatedWorker(), !aSharedWorkerName.IsVoid());
|
||||
MOZ_ASSERT_IF(IsDedicatedWorker(), aSharedWorkerName.IsEmpty());
|
||||
|
@ -4444,24 +4450,9 @@ WorkerPrivate::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
|||
// May be called on any thread!
|
||||
|
||||
MOZ_ASSERT(aIsOnCurrentThread);
|
||||
MOZ_ASSERT(mPRThread);
|
||||
|
||||
nsCOMPtr<nsIThread> thread;
|
||||
{
|
||||
MutexAutoLock lock(mMutex);
|
||||
thread = mThread;
|
||||
}
|
||||
|
||||
if (!thread) {
|
||||
NS_WARNING("Trying to test thread correctness after the worker has "
|
||||
"released its thread!");
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
nsresult rv = thread->IsOnCurrentThread(aIsOnCurrentThread);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
*aIsOnCurrentThread = PR_GetCurrentThread() == mPRThread;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -4981,14 +4972,11 @@ WorkerPrivate::RunCurrentSyncLoop()
|
|||
loopInfo->mHasRun = true;
|
||||
#endif
|
||||
|
||||
nsCOMPtr<nsIThreadInternal> thread = do_QueryInterface(mThread);
|
||||
MOZ_ASSERT(thread);
|
||||
|
||||
while (!loopInfo->mCompleted) {
|
||||
bool normalRunnablesPending = false;
|
||||
|
||||
// Don't block with the periodic GC timer running.
|
||||
if (!NS_HasPendingEvents(thread)) {
|
||||
if (!NS_HasPendingEvents(mThread)) {
|
||||
SetGCTimerMode(IdleTimer);
|
||||
}
|
||||
|
||||
|
@ -4999,7 +4987,7 @@ WorkerPrivate::RunCurrentSyncLoop()
|
|||
for (;;) {
|
||||
while (mControlQueue.IsEmpty() &&
|
||||
!normalRunnablesPending &&
|
||||
!(normalRunnablesPending = NS_HasPendingEvents(thread))) {
|
||||
!(normalRunnablesPending = NS_HasPendingEvents(mThread))) {
|
||||
WaitForWorkerEvents();
|
||||
}
|
||||
|
||||
|
@ -5017,7 +5005,7 @@ WorkerPrivate::RunCurrentSyncLoop()
|
|||
// Make sure the periodic timer is running before we continue.
|
||||
SetGCTimerMode(PeriodicTimer);
|
||||
|
||||
MOZ_ALWAYS_TRUE(NS_ProcessNextEvent(thread, false));
|
||||
MOZ_ALWAYS_TRUE(NS_ProcessNextEvent(mThread, false));
|
||||
|
||||
// Now *might* be a good time to GC. Let the JS engine make the decision.
|
||||
JS_MaybeGC(cx);
|
||||
|
@ -5037,10 +5025,7 @@ WorkerPrivate::DestroySyncLoop(uint32_t aLoopIndex, nsIThreadInternal* aThread)
|
|||
MOZ_ASSERT(mSyncLoopStack.Length() - 1 == aLoopIndex);
|
||||
|
||||
if (!aThread) {
|
||||
nsCOMPtr<nsIThreadInternal> thread = do_QueryInterface(mThread);
|
||||
MOZ_ASSERT(thread);
|
||||
|
||||
aThread = thread.get();
|
||||
aThread = mThread;
|
||||
}
|
||||
|
||||
// We're about to delete the loop, stash its event target and result.
|
||||
|
@ -5851,13 +5836,16 @@ WorkerPrivate::CycleCollectInternal(JSContext* aCx, bool aCollectChildren)
|
|||
}
|
||||
|
||||
void
|
||||
WorkerPrivate::SetThread(nsIThread* aThread)
|
||||
WorkerPrivate::SetThread(WorkerThread* aThread)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
if (aThread) {
|
||||
bool isOnCurrentThread;
|
||||
MOZ_ASSERT(NS_SUCCEEDED(aThread->IsOnCurrentThread(&isOnCurrentThread)));
|
||||
MOZ_ASSERT(isOnCurrentThread);
|
||||
#ifdef DEBUG
|
||||
{
|
||||
bool isOnCurrentThread;
|
||||
MOZ_ASSERT(NS_SUCCEEDED(aThread->IsOnCurrentThread(&isOnCurrentThread)));
|
||||
MOZ_ASSERT(isOnCurrentThread);
|
||||
}
|
||||
#endif
|
||||
|
||||
MOZ_ASSERT(!mPRThread);
|
||||
mPRThread = PRThreadFromThread(aThread);
|
||||
|
@ -5866,9 +5854,10 @@ WorkerPrivate::SetThread(nsIThread* aThread)
|
|||
else {
|
||||
MOZ_ASSERT(mPRThread);
|
||||
}
|
||||
#endif
|
||||
|
||||
nsCOMPtr<nsIThread> doomedThread;
|
||||
const WorkerThreadFriendKey friendKey;
|
||||
|
||||
nsRefPtr<WorkerThread> doomedThread;
|
||||
|
||||
{ // Scope so that |doomedThread| is released without holding the lock.
|
||||
MutexAutoLock lock(mMutex);
|
||||
|
@ -5878,18 +5867,21 @@ WorkerPrivate::SetThread(nsIThread* aThread)
|
|||
MOZ_ASSERT(mStatus == Pending);
|
||||
|
||||
mThread = aThread;
|
||||
mThread->SetWorker(friendKey, this);
|
||||
|
||||
if (!mPreStartRunnables.IsEmpty()) {
|
||||
for (uint32_t index = 0; index < mPreStartRunnables.Length(); index++) {
|
||||
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mThread->Dispatch(
|
||||
mPreStartRunnables[index],
|
||||
NS_DISPATCH_NORMAL)));
|
||||
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(
|
||||
mThread->Dispatch(friendKey, mPreStartRunnables[index])));
|
||||
}
|
||||
mPreStartRunnables.Clear();
|
||||
}
|
||||
}
|
||||
else {
|
||||
MOZ_ASSERT(mThread);
|
||||
|
||||
mThread->SetWorker(friendKey, nullptr);
|
||||
|
||||
mThread.swap(doomedThread);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,9 +49,7 @@ class Function;
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
struct PRThread;
|
||||
#endif
|
||||
|
||||
BEGIN_WORKERS_NAMESPACE
|
||||
|
||||
|
@ -59,10 +57,11 @@ class AutoSyncLoopHolder;
|
|||
class MessagePort;
|
||||
class SharedWorker;
|
||||
class WorkerControlRunnable;
|
||||
class WorkerDebugger;
|
||||
class WorkerGlobalScope;
|
||||
class WorkerPrivate;
|
||||
class WorkerRunnable;
|
||||
class WorkerDebugger;
|
||||
class WorkerThread;
|
||||
|
||||
// If you change this, the corresponding list in nsIWorkerDebugger.idl needs to
|
||||
// be updated too.
|
||||
|
@ -779,6 +778,8 @@ class WorkerPrivate : public WorkerPrivateParent<WorkerPrivate>
|
|||
class MemoryReporter;
|
||||
friend class MemoryReporter;
|
||||
|
||||
friend class WorkerThread;
|
||||
|
||||
enum GCTimerMode
|
||||
{
|
||||
PeriodicTimer = 0,
|
||||
|
@ -794,7 +795,8 @@ class WorkerPrivate : public WorkerPrivateParent<WorkerPrivate>
|
|||
JSContext* mJSContext;
|
||||
nsRefPtr<WorkerCrossThreadDispatcher> mCrossThreadDispatcher;
|
||||
nsTArray<nsCOMPtr<nsIRunnable>> mUndispatchedRunnablesForSyncLoop;
|
||||
nsCOMPtr<nsIThread> mThread;
|
||||
nsRefPtr<WorkerThread> mThread;
|
||||
PRThread* mPRThread;
|
||||
|
||||
// Things touched on worker thread only.
|
||||
nsRefPtr<WorkerGlobalScope> mScope;
|
||||
|
@ -844,11 +846,6 @@ class WorkerPrivate : public WorkerPrivateParent<WorkerPrivate>
|
|||
bool mPeriodicGCTimerRunning;
|
||||
bool mIdleGCTimerRunning;
|
||||
bool mWorkerScriptExecutedSuccessfully;
|
||||
|
||||
#ifdef DEBUG
|
||||
PRThread* mPRThread;
|
||||
#endif
|
||||
|
||||
bool mPreferences[WORKERPREF_COUNT];
|
||||
bool mOnLine;
|
||||
|
||||
|
@ -1045,7 +1042,7 @@ public:
|
|||
}
|
||||
|
||||
void
|
||||
SetThread(nsIThread* aThread);
|
||||
SetThread(WorkerThread* aThread);
|
||||
|
||||
void
|
||||
AssertIsOnWorkerThread() const
|
||||
|
|
|
@ -30,6 +30,20 @@ const uint32_t kWorkerStackSize = 256 * sizeof(size_t) * 1024;
|
|||
|
||||
} // anonymous namespace
|
||||
|
||||
#ifdef NS_BUILD_REFCNT_LOGGING
|
||||
|
||||
WorkerThreadFriendKey::WorkerThreadFriendKey()
|
||||
{
|
||||
MOZ_COUNT_CTOR(WorkerThreadFriendKey);
|
||||
}
|
||||
|
||||
WorkerThreadFriendKey::~WorkerThreadFriendKey()
|
||||
{
|
||||
MOZ_COUNT_DTOR(WorkerThreadFriendKey);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
class WorkerThread::Observer MOZ_FINAL
|
||||
: public nsIThreadObserver
|
||||
{
|
||||
|
@ -55,21 +69,24 @@ private:
|
|||
};
|
||||
|
||||
WorkerThread::WorkerThread()
|
||||
: nsThread(nsThread::NOT_MAIN_THREAD, kWorkerStackSize),
|
||||
mWorkerPrivate(nullptr)
|
||||
#ifdef DEBUG
|
||||
: nsThread(nsThread::NOT_MAIN_THREAD, kWorkerStackSize)
|
||||
, mWorkerPrivateCondVar(mLock, "WorkerThread::mWorkerPrivateCondVar")
|
||||
, mWorkerPrivate(nullptr)
|
||||
, mOtherThreadDispatchingViaEventTarget(false)
|
||||
, mAcceptingNonWorkerRunnables(true)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
WorkerThread::~WorkerThread()
|
||||
{
|
||||
MOZ_ASSERT(!mWorkerPrivate);
|
||||
MOZ_ASSERT(!mOtherThreadDispatchingViaEventTarget);
|
||||
MOZ_ASSERT(mAcceptingNonWorkerRunnables);
|
||||
}
|
||||
|
||||
// static
|
||||
already_AddRefed<WorkerThread>
|
||||
WorkerThread::Create()
|
||||
WorkerThread::Create(const WorkerThreadFriendKey& /* aKey */)
|
||||
{
|
||||
MOZ_ASSERT(nsThreadManager::get());
|
||||
|
||||
|
@ -79,40 +96,106 @@ WorkerThread::Create()
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
NS_SetThreadName(thread, "DOM Worker");
|
||||
|
||||
return thread.forget();
|
||||
}
|
||||
|
||||
void
|
||||
WorkerThread::SetWorker(WorkerPrivate* aWorkerPrivate)
|
||||
WorkerThread::SetWorker(const WorkerThreadFriendKey& /* aKey */,
|
||||
WorkerPrivate* aWorkerPrivate)
|
||||
{
|
||||
MOZ_ASSERT(PR_GetCurrentThread() == mThread);
|
||||
MOZ_ASSERT_IF(aWorkerPrivate, !mWorkerPrivate);
|
||||
MOZ_ASSERT_IF(!aWorkerPrivate, mWorkerPrivate);
|
||||
|
||||
// No need to lock here because mWorkerPrivate is only modified on mThread.
|
||||
if (aWorkerPrivate) {
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
if (mWorkerPrivate) {
|
||||
MOZ_ASSERT(mObserver);
|
||||
MOZ_ASSERT(!mWorkerPrivate);
|
||||
MOZ_ASSERT(mAcceptingNonWorkerRunnables);
|
||||
|
||||
mWorkerPrivate = aWorkerPrivate;
|
||||
mAcceptingNonWorkerRunnables = false;
|
||||
}
|
||||
|
||||
mObserver = new Observer(aWorkerPrivate);
|
||||
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(AddObserver(mObserver)));
|
||||
} else {
|
||||
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(RemoveObserver(mObserver)));
|
||||
|
||||
mObserver = nullptr;
|
||||
mWorkerPrivate->SetThread(nullptr);
|
||||
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
MOZ_ASSERT(mWorkerPrivate);
|
||||
MOZ_ASSERT(!mAcceptingNonWorkerRunnables);
|
||||
MOZ_ASSERT(!mOtherThreadDispatchingViaEventTarget,
|
||||
"XPCOM Dispatch hapenning at the same time our thread is "
|
||||
"being unset! This should not be possible!");
|
||||
|
||||
while (mOtherThreadDispatchingViaEventTarget) {
|
||||
mWorkerPrivateCondVar.Wait();
|
||||
}
|
||||
|
||||
mAcceptingNonWorkerRunnables = true;
|
||||
mWorkerPrivate = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
WorkerThread::DispatchPrimaryRunnable(const WorkerThreadFriendKey& /* aKey */,
|
||||
nsIRunnable* aRunnable)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
MOZ_ASSERT(PR_GetCurrentThread() != mThread);
|
||||
MOZ_ASSERT(aRunnable);
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
MOZ_ASSERT(!mWorkerPrivate);
|
||||
MOZ_ASSERT(mAcceptingNonWorkerRunnables);
|
||||
}
|
||||
#endif
|
||||
|
||||
nsresult rv = nsThread::Dispatch(aRunnable, NS_DISPATCH_NORMAL);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
mWorkerPrivate = aWorkerPrivate;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
if (mWorkerPrivate) {
|
||||
mWorkerPrivate->SetThread(this);
|
||||
nsresult
|
||||
WorkerThread::Dispatch(const WorkerThreadFriendKey& /* aKey */,
|
||||
WorkerRunnable* aWorkerRunnable)
|
||||
{
|
||||
// May be called on any thread!
|
||||
|
||||
nsRefPtr<Observer> observer = new Observer(mWorkerPrivate);
|
||||
#ifdef DEBUG
|
||||
{
|
||||
const bool onWorkerThread = PR_GetCurrentThread() == mThread;
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(AddObserver(observer)));
|
||||
MOZ_ASSERT(mWorkerPrivate);
|
||||
MOZ_ASSERT(!mAcceptingNonWorkerRunnables);
|
||||
|
||||
mObserver.swap(observer);
|
||||
if (onWorkerThread) {
|
||||
mWorkerPrivate->AssertIsOnWorkerThread();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
nsresult rv = nsThread::Dispatch(aWorkerRunnable, NS_DISPATCH_NORMAL);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// We don't need to notify the worker's condition variable here because we're
|
||||
// being called from worker-controlled code and it will make sure to wake up
|
||||
// the worker thread if needed.
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS_INHERITED0(WorkerThread, nsThread)
|
||||
|
@ -122,37 +205,82 @@ WorkerThread::Dispatch(nsIRunnable* aRunnable, uint32_t aFlags)
|
|||
{
|
||||
// May be called on any thread!
|
||||
|
||||
#ifdef DEBUG
|
||||
if (PR_GetCurrentThread() == mThread) {
|
||||
MOZ_ASSERT(mWorkerPrivate);
|
||||
mWorkerPrivate->AssertIsOnWorkerThread();
|
||||
// Workers only support asynchronous dispatch.
|
||||
if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) {
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
else if (aRunnable && !IsAcceptingNonWorkerRunnables()) {
|
||||
// Only enforce cancelable runnables after we've started the worker loop.
|
||||
|
||||
const bool onWorkerThread = PR_GetCurrentThread() == mThread;
|
||||
|
||||
#ifdef DEBUG
|
||||
if (aRunnable && !onWorkerThread) {
|
||||
nsCOMPtr<nsICancelableRunnable> cancelable = do_QueryInterface(aRunnable);
|
||||
MOZ_ASSERT(cancelable,
|
||||
"Should have been wrapped by the worker's event target!");
|
||||
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
// Only enforce cancelable runnables after we've started the worker loop.
|
||||
if (!mAcceptingNonWorkerRunnables) {
|
||||
MOZ_ASSERT(cancelable,
|
||||
"Only nsICancelableRunnable may be dispatched to a worker!");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Workers only support asynchronous dispatch for now.
|
||||
if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) {
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
WorkerPrivate* workerPrivate = nullptr;
|
||||
if (onWorkerThread) {
|
||||
// No need to lock here because it is only modified on this thread.
|
||||
MOZ_ASSERT(mWorkerPrivate);
|
||||
mWorkerPrivate->AssertIsOnWorkerThread();
|
||||
|
||||
workerPrivate = mWorkerPrivate;
|
||||
} else {
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
MOZ_ASSERT(!mOtherThreadDispatchingViaEventTarget);
|
||||
|
||||
if (mWorkerPrivate) {
|
||||
workerPrivate = mWorkerPrivate;
|
||||
|
||||
// Setting this flag will make the worker thread sleep if it somehow tries
|
||||
// to unset mWorkerPrivate while we're using it.
|
||||
mOtherThreadDispatchingViaEventTarget = true;
|
||||
}
|
||||
}
|
||||
|
||||
nsIRunnable* runnableToDispatch;
|
||||
nsRefPtr<WorkerRunnable> workerRunnable;
|
||||
|
||||
if (aRunnable && PR_GetCurrentThread() == mThread) {
|
||||
// No need to lock here because mWorkerPrivate is only modified on mThread.
|
||||
workerRunnable = mWorkerPrivate->MaybeWrapAsWorkerRunnable(aRunnable);
|
||||
if (aRunnable && onWorkerThread) {
|
||||
workerRunnable = workerPrivate->MaybeWrapAsWorkerRunnable(aRunnable);
|
||||
runnableToDispatch = workerRunnable;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
runnableToDispatch = aRunnable;
|
||||
}
|
||||
|
||||
nsresult rv = nsThread::Dispatch(runnableToDispatch, NS_DISPATCH_NORMAL);
|
||||
|
||||
if (!onWorkerThread && workerPrivate) {
|
||||
// We need to wake the worker thread if we're not already on the right
|
||||
// thread and the dispatch succeeded.
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
MutexAutoLock workerLock(workerPrivate->mMutex);
|
||||
|
||||
workerPrivate->mCondVar.Notify();
|
||||
}
|
||||
|
||||
// Now unset our waiting flag.
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
|
||||
MOZ_ASSERT(mOtherThreadDispatchingViaEventTarget);
|
||||
mOtherThreadDispatchingViaEventTarget = false;
|
||||
|
||||
mWorkerPrivateCondVar.Notify();
|
||||
}
|
||||
}
|
||||
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#define mozilla_dom_workers_WorkerThread_h__
|
||||
|
||||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/CondVar.h"
|
||||
#include "mozilla/DebugOnly.h"
|
||||
#include "nsISupportsImpl.h"
|
||||
#include "nsRefPtr.h"
|
||||
|
@ -19,54 +20,70 @@ namespace mozilla {
|
|||
namespace dom {
|
||||
namespace workers {
|
||||
|
||||
class RuntimeService;
|
||||
class WorkerPrivate;
|
||||
template <class> class WorkerPrivateParent;
|
||||
class WorkerRunnable;
|
||||
|
||||
// This class lets us restrict the public methods that can be called on
|
||||
// WorkerThread to RuntimeService and WorkerPrivate without letting them gain
|
||||
// full access to private methods (as would happen if they were simply friends).
|
||||
class WorkerThreadFriendKey
|
||||
{
|
||||
friend class RuntimeService;
|
||||
friend class WorkerPrivate;
|
||||
friend class WorkerPrivateParent<WorkerPrivate>;
|
||||
|
||||
#ifdef NS_BUILD_REFCNT_LOGGING
|
||||
WorkerThreadFriendKey();
|
||||
~WorkerThreadFriendKey();
|
||||
#endif
|
||||
};
|
||||
|
||||
class WorkerThread MOZ_FINAL
|
||||
: public nsThread
|
||||
{
|
||||
class Observer;
|
||||
|
||||
CondVar mWorkerPrivateCondVar;
|
||||
|
||||
// Protected by nsThread::mLock.
|
||||
WorkerPrivate* mWorkerPrivate;
|
||||
|
||||
// Only touched on the target thread.
|
||||
nsRefPtr<Observer> mObserver;
|
||||
|
||||
#ifdef DEBUG
|
||||
// Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
|
||||
bool mOtherThreadDispatchingViaEventTarget;
|
||||
|
||||
// Protected by nsThread::mLock.
|
||||
bool mAcceptingNonWorkerRunnables;
|
||||
#endif
|
||||
DebugOnly<bool> mAcceptingNonWorkerRunnables;
|
||||
|
||||
public:
|
||||
static already_AddRefed<WorkerThread>
|
||||
Create();
|
||||
Create(const WorkerThreadFriendKey& aKey);
|
||||
|
||||
void
|
||||
SetWorker(WorkerPrivate* aWorkerPrivate);
|
||||
SetWorker(const WorkerThreadFriendKey& aKey, WorkerPrivate* aWorkerPrivate);
|
||||
|
||||
nsresult
|
||||
DispatchPrimaryRunnable(const WorkerThreadFriendKey& aKey,
|
||||
nsIRunnable* aRunnable);
|
||||
|
||||
nsresult
|
||||
Dispatch(const WorkerThreadFriendKey& aKey,
|
||||
WorkerRunnable* aWorkerRunnable);
|
||||
|
||||
NS_DECL_ISUPPORTS_INHERITED
|
||||
|
||||
NS_IMETHOD
|
||||
Dispatch(nsIRunnable* aRunnable, uint32_t aFlags) MOZ_OVERRIDE;
|
||||
|
||||
#ifdef DEBUG
|
||||
bool
|
||||
IsAcceptingNonWorkerRunnables()
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
return mAcceptingNonWorkerRunnables;
|
||||
}
|
||||
|
||||
void
|
||||
SetAcceptingNonWorkerRunnables(bool aAcceptingNonWorkerRunnables)
|
||||
{
|
||||
MutexAutoLock lock(mLock);
|
||||
mAcceptingNonWorkerRunnables = aAcceptingNonWorkerRunnables;
|
||||
}
|
||||
#endif
|
||||
|
||||
private:
|
||||
WorkerThread();
|
||||
|
||||
~WorkerThread();
|
||||
|
||||
// This should only be called by consumers that have an
|
||||
// nsIEventTarget/nsIThread pointer.
|
||||
NS_IMETHOD
|
||||
Dispatch(nsIRunnable* aRunnable, uint32_t aFlags) MOZ_OVERRIDE;
|
||||
};
|
||||
|
||||
} // namespace workers
|
||||
|
|
Загрузка…
Ссылка в новой задаче