Bug 1286487 - WorkerProxyToMainThreadRunnable must keep alive workers using WorkerHolder, r=bkelly

This commit is contained in:
Andrea Marchesini 2016-07-23 08:31:31 +02:00
parent 602a0147b1
commit 0ba6bcc1bb
2 changed files with 41 additions and 3 deletions

View File

@ -677,13 +677,13 @@ WorkerProxyToMainThreadRunnable::Dispatch()
{
mWorkerPrivate->AssertIsOnWorkerThread();
if (NS_WARN_IF(!mWorkerPrivate->ModifyBusyCountFromWorker(true))) {
if (NS_WARN_IF(!HoldWorker())) {
RunBackOnWorkerThread();
return false;
}
if (NS_WARN_IF(NS_FAILED(NS_DispatchToMainThread(this)))) {
mWorkerPrivate->ModifyBusyCountFromWorker(false);
ReleaseWorker();
RunBackOnWorkerThread();
return false;
}
@ -733,7 +733,7 @@ WorkerProxyToMainThreadRunnable::PostDispatchOnMainThread()
mRunnable->RunBackOnWorkerThread();
// Let's release the worker thread.
aWorkerPrivate->ModifyBusyCountFromWorker(false);
mRunnable->ReleaseWorker();
return true;
}
@ -746,3 +746,37 @@ WorkerProxyToMainThreadRunnable::PostDispatchOnMainThread()
new ReleaseRunnable(mWorkerPrivate, this);
NS_WARN_IF(!runnable->Dispatch());
}
bool
WorkerProxyToMainThreadRunnable::HoldWorker()
{
mWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(!mWorkerHolder);
class SimpleWorkerHolder final : public WorkerHolder
{
public:
bool Notify(Status aStatus) override
{
// We don't care about the notification. We just want to keep the
// mWorkerPrivate alive.
return true;
}
};
UniquePtr<WorkerHolder> workerHolder(new SimpleWorkerHolder());
if (NS_WARN_IF(!workerHolder->HoldWorker(mWorkerPrivate))) {
return false;
}
mWorkerHolder = Move(workerHolder);
return true;
}
void
WorkerProxyToMainThreadRunnable::ReleaseWorker()
{
mWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(mWorkerHolder);
mWorkerHolder = nullptr;
}

View File

@ -433,8 +433,12 @@ private:
void PostDispatchOnMainThread();
bool HoldWorker();
void ReleaseWorker();
protected:
WorkerPrivate* mWorkerPrivate;
UniquePtr<WorkerHolder> mWorkerHolder;
};
// Class for checking API exposure. This totally violates the "MUST" in the