Bug 1186745 part 3 - Make nsThreadSyncDispatch leak the sync task by default when Run() is not called. r=froydnj

--HG--
extra : source : 7eda5d8e2da47a0ff4f15e6a4d20f1ca108e5931
This commit is contained in:
Xidorn Quan 2015-10-06 13:00:59 +11:00
parent d98cc8899a
commit 0c51f5c14f

View File

@ -8,20 +8,21 @@
#define nsThreadSyncDispatch_h_
#include "nsThreadUtils.h"
#include "LeakRefPtr.h"
class nsThreadSyncDispatch : public nsRunnable
{
public:
nsThreadSyncDispatch(nsIThread* aOrigin, already_AddRefed<nsIRunnable>&& aTask)
: mOrigin(aOrigin)
, mSyncTask(aTask)
, mSyncTask(mozilla::Move(aTask))
, mResult(NS_ERROR_NOT_INITIALIZED)
{
}
bool IsPending()
{
return mSyncTask != nullptr;
return !!mSyncTask;
}
nsresult Result()
@ -32,9 +33,11 @@ public:
private:
NS_IMETHOD Run() override
{
if (mSyncTask) {
mResult = mSyncTask->Run();
mSyncTask = nullptr;
if (nsIRunnable* task = mSyncTask.get()) {
mResult = task->Run();
// We must release the task here to ensure that when the original
// thread is unblocked, this task has been released.
mSyncTask.release();
// unblock the origin thread
mOrigin->Dispatch(this, NS_DISPATCH_NORMAL);
}
@ -42,7 +45,9 @@ private:
}
nsCOMPtr<nsIThread> mOrigin;
nsCOMPtr<nsIRunnable> mSyncTask;
// The task is leaked by default when Run() is not called, because
// otherwise we may release it in an incorrect thread.
mozilla::LeakRefPtr<nsIRunnable> mSyncTask;
nsresult mResult;
};