Bug 1024027 - Cannot safely dispatch to nsStreamTransportService from a non-main thread. r=bsmedberg,mcmanus

This commit is contained in:
Shihua Zheng 2014-06-21 03:26:43 -07:00
parent a554e34894
commit 36767b73f6
3 changed files with 39 additions and 5 deletions

View File

@ -498,15 +498,31 @@ NS_IMPL_ISUPPORTS(nsStreamTransportService,
NS_IMETHODIMP
nsStreamTransportService::Dispatch(nsIRunnable *task, uint32_t flags)
{
NS_ENSURE_TRUE(mPool, NS_ERROR_NOT_INITIALIZED);
return mPool->Dispatch(task, flags);
nsCOMPtr<nsIThreadPool> pool;
{
mozilla::MutexAutoLock lock(mShutdownLock);
if (mIsShutdown) {
return NS_ERROR_NOT_INITIALIZED;
}
pool = mPool;
}
NS_ENSURE_TRUE(pool, NS_ERROR_NOT_INITIALIZED);
return pool->Dispatch(task, flags);
}
NS_IMETHODIMP
nsStreamTransportService::IsOnCurrentThread(bool *result)
{
NS_ENSURE_TRUE(mPool, NS_ERROR_NOT_INITIALIZED);
return mPool->IsOnCurrentThread(result);
nsCOMPtr<nsIThreadPool> pool;
{
mozilla::MutexAutoLock lock(mShutdownLock);
if (mIsShutdown) {
return NS_ERROR_NOT_INITIALIZED;
}
pool = mPool;
}
NS_ENSURE_TRUE(pool, NS_ERROR_NOT_INITIALIZED);
return pool->IsOnCurrentThread(result);
}
NS_IMETHODIMP
@ -545,6 +561,11 @@ nsStreamTransportService::Observe(nsISupports *subject, const char *topic,
{
NS_ASSERTION(strcmp(topic, "xpcom-shutdown-threads") == 0, "oops");
{
mozilla::MutexAutoLock lock(mShutdownLock);
mIsShutdown = true;
}
if (mPool) {
mPool->Shutdown();
mPool = nullptr;

View File

@ -6,7 +6,9 @@
#include "nsIEventTarget.h"
#include "nsIObserver.h"
#include "nsCOMPtr.h"
#include "nsThreadUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/Mutex.h"
class nsIThreadPool;
@ -22,10 +24,14 @@ public:
nsresult Init();
nsStreamTransportService() {}
nsStreamTransportService() : mShutdownLock("nsStreamTransportService.mShutdownLock"),
mIsShutdown(false) {}
private:
~nsStreamTransportService();
nsCOMPtr<nsIThreadPool> mPool;
mozilla::Mutex mShutdownLock;
bool mIsShutdown;
};

View File

@ -76,6 +76,9 @@ nsThreadPool::PutEvent(nsIRunnable* aEvent)
{
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
if (NS_WARN_IF(mShutdown)) {
return NS_ERROR_NOT_AVAILABLE;
}
LOG(("THRD-P(%p) put [%d %d %d]\n", this, mIdleCount, mThreads.Count(),
mThreadLimit));
MOZ_ASSERT(mIdleCount <= (uint32_t)mThreads.Count(), "oops");
@ -264,6 +267,10 @@ NS_IMETHODIMP
nsThreadPool::IsOnCurrentThread(bool* aResult)
{
ReentrantMonitorAutoEnter mon(mEvents.GetReentrantMonitor());
if (NS_WARN_IF(mShutdown)) {
return NS_ERROR_NOT_AVAILABLE;
}
nsIThread* thread = NS_GetCurrentThread();
for (uint32_t i = 0; i < static_cast<uint32_t>(mThreads.Count()); ++i) {
if (mThreads[i] == thread) {