Bug 1237414: Switch AsyncCubebOperation to a SharedThreadPool r=padenot

--HG--
extra : commitid : Ir2a54CKDuu
This commit is contained in:
Randell Jesup 2016-01-21 11:51:36 -05:00
parent cbdb5359ca
commit 5a17e5fbbe
2 changed files with 43 additions and 20 deletions

View File

@ -5,6 +5,8 @@
#include <MediaStreamGraphImpl.h>
#include "mozilla/dom/AudioContext.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/ClearOnShutdown.h"
#include "CubebUtils.h"
#ifdef XP_MACOSX
@ -30,6 +32,8 @@ extern mozilla::LazyLogModule gMediaStreamGraphLog;
namespace mozilla {
StaticRefPtr<nsIThreadPool> AsyncCubebTask::sThreadPool;
struct AutoProfilerUnregisterThread
{
// The empty ctor is used to silence a pre-4.8.0 GCC unused variable warning.
@ -469,20 +473,39 @@ AsyncCubebTask::~AsyncCubebTask()
{
}
/* static */
nsresult
AsyncCubebTask::EnsureThread()
{
if (!sThreadPool) {
nsCOMPtr<nsIThreadPool> threadPool =
SharedThreadPool::Get(NS_LITERAL_CSTRING("CubebOperation"), 1);
sThreadPool = threadPool;
// Need to null this out before xpcom-shutdown-threads Observers run
// since we don't know the order that the shutdown-threads observers
// will run. ClearOnShutdown guarantees it runs first.
if (!NS_IsMainThread()) {
NS_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
ClearOnShutdown(&sThreadPool, ShutdownPhase::ShutdownThreads);
}));
} else {
ClearOnShutdown(&sThreadPool, ShutdownPhase::ShutdownThreads);
}
const uint32_t kIdleThreadTimeoutMs = 2000;
nsresult rv = sThreadPool->SetIdleThreadTimeout(PR_MillisecondsToInterval(kIdleThreadTimeoutMs));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
return NS_OK;
}
NS_IMETHODIMP
AsyncCubebTask::Run()
{
MOZ_ASSERT(mThread);
if (NS_IsMainThread()) {
mThread->Shutdown(); // can't shutdown from the thread itself, darn
// don't null out mThread!
// See bug 999104. we must hold a ref to the thread across Dispatch()
// since the internal mthread ref could be released while processing
// the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it
// assumes the caller does.
return NS_OK;
}
MOZ_ASSERT(mDriver);
switch(mOperation) {
@ -506,9 +529,7 @@ AsyncCubebTask::Run()
MOZ_CRASH("Operation not implemented.");
}
// and now kill this thread
NS_DispatchToMainThread(this);
// The thread will kill itself after a bit
return NS_OK;
}

View File

@ -13,6 +13,8 @@
#include "AudioSegment.h"
#include "SelfRef.h"
#include "mozilla/Atomics.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/StaticPtr.h"
struct cubeb_stream;
@ -549,11 +551,9 @@ public:
nsresult Dispatch()
{
// Can't add 'this' as the event to run, since mThread may not be set yet
nsresult rv = NS_NewNamedThread("CubebOperation", getter_AddRefs(mThread));
if (NS_SUCCEEDED(rv)) {
// Note: event must not null out mThread!
rv = mThread->Dispatch(this, NS_DISPATCH_NORMAL);
nsresult rv = EnsureThread();
if (!NS_FAILED(rv)) {
rv = sThreadPool->Dispatch(this, NS_DISPATCH_NORMAL);
}
return rv;
}
@ -562,8 +562,10 @@ protected:
virtual ~AsyncCubebTask();
private:
static nsresult EnsureThread();
NS_IMETHOD Run() override final;
nsCOMPtr<nsIThread> mThread;
static StaticRefPtr<nsIThreadPool> sThreadPool;
RefPtr<AudioCallbackDriver> mDriver;
AsyncCubebOperation mOperation;
RefPtr<MediaStreamGraphImpl> mShutdownGrip;