mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 443874 - "Need a hook for thread creation and destruction in thread pool". r=bsmedberg.
This commit is contained in:
parent
ea9d6c0eb3
commit
591253915b
@ -79,6 +79,7 @@ CPPSRCS = \
|
||||
TestPipe.cpp \
|
||||
TestRegistrationOrder.cpp \
|
||||
TestProxies.cpp \
|
||||
TestThreadPoolListener.cpp \
|
||||
$(NULL)
|
||||
|
||||
ifndef MOZ_ENABLE_LIBXUL
|
||||
@ -140,6 +141,7 @@ CPP_UNIT_TESTS = \
|
||||
TestPipe \
|
||||
TestServMgr \
|
||||
TestTextFormatter \
|
||||
TestThreadPoolListener \
|
||||
$(NULL)
|
||||
|
||||
ifndef MOZ_ENABLE_LIBXUL
|
||||
|
235
xpcom/tests/TestThreadPoolListener.cpp
Normal file
235
xpcom/tests/TestThreadPoolListener.cpp
Normal file
@ -0,0 +1,235 @@
|
||||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Proxy Test Code.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Ben Turner <bent.mozilla@gmail.com>.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2008
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
#include "TestHarness.h"
|
||||
|
||||
#include "nsIThread.h"
|
||||
#include "nsIThreadPool.h"
|
||||
|
||||
#include "nsAutoLock.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsXPCOMCIDInternal.h"
|
||||
#include "pratom.h"
|
||||
#include "prinrval.h"
|
||||
#include "prmon.h"
|
||||
#include "prthread.h"
|
||||
|
||||
#define NUMBER_OF_THREADS 4
|
||||
|
||||
// One hour... because test boxes can be slow!
|
||||
#define IDLE_THREAD_TIMEOUT 3600000
|
||||
|
||||
static nsIThread** gCreatedThreadList = nsnull;
|
||||
static nsIThread** gShutDownThreadList = nsnull;
|
||||
|
||||
static PRMonitor* gMonitor = nsnull;
|
||||
|
||||
static PRBool gAllRunnablesPosted = PR_FALSE;
|
||||
static PRBool gAllThreadsCreated = PR_FALSE;
|
||||
static PRBool gAllThreadsShutDown = PR_FALSE;
|
||||
|
||||
class Listener : public nsIThreadPoolListener
|
||||
{
|
||||
public:
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_NSITHREADPOOLLISTENER
|
||||
};
|
||||
|
||||
NS_IMPL_THREADSAFE_ISUPPORTS1(Listener, nsIThreadPoolListener)
|
||||
|
||||
NS_IMETHODIMP
|
||||
Listener::OnThreadCreated()
|
||||
{
|
||||
nsIThread* current = NS_GetCurrentThread();
|
||||
NS_ASSERTION(current, "Couldn't get current thread!");
|
||||
|
||||
nsAutoMonitor mon(gMonitor);
|
||||
|
||||
while (!gAllRunnablesPosted) {
|
||||
mon.Wait();
|
||||
}
|
||||
|
||||
for (PRUint32 i = 0; i < NUMBER_OF_THREADS; i++) {
|
||||
nsIThread* thread = gCreatedThreadList[i];
|
||||
NS_ASSERTION(thread != current, "Saw the same thread twice!");
|
||||
|
||||
if (!thread) {
|
||||
gCreatedThreadList[i] = current;
|
||||
if (i == (NUMBER_OF_THREADS - 1)) {
|
||||
gAllThreadsCreated = PR_TRUE;
|
||||
mon.NotifyAll();
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
NS_NOTREACHED("Too many threads!");
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
Listener::OnThreadShuttingDown()
|
||||
{
|
||||
nsIThread* current = NS_GetCurrentThread();
|
||||
NS_ASSERTION(current, "Couldn't get current thread!");
|
||||
|
||||
nsAutoMonitor mon(gMonitor);
|
||||
|
||||
for (PRUint32 i = 0; i < NUMBER_OF_THREADS; i++) {
|
||||
nsIThread* thread = gShutDownThreadList[i];
|
||||
NS_ASSERTION(thread != current, "Saw the same thread twice!");
|
||||
|
||||
if (!thread) {
|
||||
gShutDownThreadList[i] = current;
|
||||
if (i == (NUMBER_OF_THREADS - 1)) {
|
||||
gAllThreadsShutDown = PR_TRUE;
|
||||
mon.NotifyAll();
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
NS_NOTREACHED("Too many threads!");
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
class AutoCreateAndDestroyMonitor
|
||||
{
|
||||
public:
|
||||
AutoCreateAndDestroyMonitor(PRMonitor** aMonitorPtr)
|
||||
: mMonitorPtr(aMonitorPtr) {
|
||||
*aMonitorPtr = nsAutoMonitor::NewMonitor("TestThreadPoolListener::AutoMon");
|
||||
NS_ASSERTION(*aMonitorPtr, "Out of memory!");
|
||||
}
|
||||
|
||||
~AutoCreateAndDestroyMonitor() {
|
||||
if (*mMonitorPtr) {
|
||||
nsAutoMonitor::DestroyMonitor(*mMonitorPtr);
|
||||
*mMonitorPtr = nsnull;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
PRMonitor** mMonitorPtr;
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ScopedXPCOM xpcom("ThreadPoolListener");
|
||||
NS_ENSURE_FALSE(xpcom.failed(), 1);
|
||||
|
||||
nsIThread* createdThreadList[NUMBER_OF_THREADS] = { nsnull };
|
||||
gCreatedThreadList = createdThreadList;
|
||||
|
||||
nsIThread* shutDownThreadList[NUMBER_OF_THREADS] = { nsnull };
|
||||
gShutDownThreadList = shutDownThreadList;
|
||||
|
||||
AutoCreateAndDestroyMonitor newMon(&gMonitor);
|
||||
NS_ENSURE_TRUE(gMonitor, 1);
|
||||
|
||||
nsresult rv;
|
||||
nsCOMPtr<nsIThreadPool> pool =
|
||||
do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
|
||||
rv = pool->SetThreadLimit(NUMBER_OF_THREADS);
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
|
||||
rv = pool->SetIdleThreadLimit(NUMBER_OF_THREADS);
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
|
||||
rv = pool->SetIdleThreadTimeout(IDLE_THREAD_TIMEOUT);
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
|
||||
nsCOMPtr<nsIThreadPoolListener> listener = new Listener();
|
||||
NS_ENSURE_TRUE(listener, 1);
|
||||
|
||||
rv = pool->SetListener(listener);
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
|
||||
{
|
||||
nsAutoMonitor mon(gMonitor);
|
||||
|
||||
for (PRUint32 i = 0; i < NUMBER_OF_THREADS; i++) {
|
||||
nsCOMPtr<nsIRunnable> runnable = new nsRunnable();
|
||||
NS_ENSURE_TRUE(runnable, 1);
|
||||
|
||||
rv = pool->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
}
|
||||
|
||||
gAllRunnablesPosted = PR_TRUE;
|
||||
mon.NotifyAll();
|
||||
}
|
||||
|
||||
{
|
||||
nsAutoMonitor mon(gMonitor);
|
||||
while (!gAllThreadsCreated) {
|
||||
mon.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
rv = pool->Shutdown();
|
||||
NS_ENSURE_SUCCESS(rv, 1);
|
||||
|
||||
{
|
||||
nsAutoMonitor mon(gMonitor);
|
||||
while (!gAllThreadsShutDown) {
|
||||
mon.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
for (PRUint32 i = 0; i < NUMBER_OF_THREADS; i++) {
|
||||
nsIThread* created = gCreatedThreadList[i];
|
||||
NS_ENSURE_TRUE(created, 1);
|
||||
|
||||
PRBool match = PR_FALSE;
|
||||
for (PRUint32 j = 0; j < NUMBER_OF_THREADS; j++) {
|
||||
nsIThread* destroyed = gShutDownThreadList[j];
|
||||
NS_ENSURE_TRUE(destroyed, 1);
|
||||
|
||||
if (destroyed == created) {
|
||||
match = PR_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
NS_ENSURE_TRUE(match, 1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -38,6 +38,22 @@
|
||||
|
||||
#include "nsIEventTarget.idl"
|
||||
|
||||
[scriptable, uuid(ef194cab-3f86-4b61-b132-e5e96a79e5d1)]
|
||||
interface nsIThreadPoolListener : nsISupports
|
||||
{
|
||||
/**
|
||||
* Called when a new thread is created by the thread pool. The notification
|
||||
* happens on the newly-created thread.
|
||||
*/
|
||||
void onThreadCreated();
|
||||
|
||||
/**
|
||||
* Called when a thread is about to be destroyed by the thread pool. The
|
||||
* notification happens on the thread that is about to be destroyed.
|
||||
*/
|
||||
void onThreadShuttingDown();
|
||||
};
|
||||
|
||||
/**
|
||||
* An interface to a thread pool. A thread pool creates a limited number of
|
||||
* anonymous (unnamed) worker threads. An event dispatched to the thread pool
|
||||
@ -70,4 +86,21 @@ interface nsIThreadPool : nsIEventTarget
|
||||
* destroyed.
|
||||
*/
|
||||
attribute unsigned long idleThreadTimeout;
|
||||
|
||||
/**
|
||||
* An optional listener that will be notified when a thread is created or
|
||||
* destroyed in the course of the thread pool's operation.
|
||||
*
|
||||
* A listener will only receive notifications about threads created after the
|
||||
* listener is set so it is recommended that the consumer set the listener
|
||||
* before dispatching the first event. A listener that receives an
|
||||
* onThreadCreated() notification is guaranteed to always receive the
|
||||
* corresponding onThreadShuttingDown() notification.
|
||||
*
|
||||
* The thread pool takes ownership of the listener and releases it when the
|
||||
* shutdown() method is called. Threads created after the listener is set will
|
||||
* also take ownership of the listener so that the listener will be kept alive
|
||||
* long enough to receive the guaranteed onThreadShuttingDown() notification.
|
||||
*/
|
||||
attribute nsIThreadPoolListener listener;
|
||||
};
|
||||
|
@ -162,6 +162,16 @@ nsThreadPool::Run()
|
||||
PRBool wasIdle = PR_FALSE;
|
||||
PRIntervalTime idleSince;
|
||||
|
||||
nsCOMPtr<nsIThreadPoolListener> listener;
|
||||
{
|
||||
nsAutoMonitor mon(mEvents.Monitor());
|
||||
listener = mListener;
|
||||
}
|
||||
|
||||
if (listener) {
|
||||
listener->OnThreadCreated();
|
||||
}
|
||||
|
||||
do {
|
||||
nsCOMPtr<nsIRunnable> event;
|
||||
{
|
||||
@ -210,8 +220,12 @@ nsThreadPool::Run()
|
||||
}
|
||||
} while (!exitThread);
|
||||
|
||||
if (shutdownThreadOnExit)
|
||||
if (shutdownThreadOnExit) {
|
||||
if (listener) {
|
||||
listener->OnThreadShuttingDown();
|
||||
}
|
||||
ShutdownThread(current);
|
||||
}
|
||||
|
||||
LOG(("THRD-P(%p) leave\n", this));
|
||||
return NS_OK;
|
||||
@ -257,12 +271,18 @@ NS_IMETHODIMP
|
||||
nsThreadPool::Shutdown()
|
||||
{
|
||||
nsCOMArray<nsIThread> threads;
|
||||
nsCOMPtr<nsIThreadPoolListener> listener;
|
||||
{
|
||||
nsAutoMonitor mon(mEvents.Monitor());
|
||||
mShutdown = PR_TRUE;
|
||||
mon.NotifyAll();
|
||||
|
||||
threads.AppendObjects(mThreads);
|
||||
|
||||
// Swap in a null listener so that we release the listener at the end of
|
||||
// this method. The listener will be kept alive as long as the other threads
|
||||
// that were created when it was set.
|
||||
mListener.swap(listener);
|
||||
}
|
||||
|
||||
// It's important that we shutdown the threads while outside the event queue
|
||||
@ -326,3 +346,22 @@ nsThreadPool::SetIdleThreadTimeout(PRUint32 value)
|
||||
mon.NotifyAll(); // wake up threads so they observe this change
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsThreadPool::GetListener(nsIThreadPoolListener** aListener)
|
||||
{
|
||||
nsAutoMonitor mon(mEvents.Monitor());
|
||||
NS_IF_ADDREF(*aListener = mListener);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsThreadPool::SetListener(nsIThreadPoolListener* aListener)
|
||||
{
|
||||
nsCOMPtr<nsIThreadPoolListener> swappedListener(aListener);
|
||||
{
|
||||
nsAutoMonitor mon(mEvents.Monitor());
|
||||
mListener.swap(swappedListener);
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include "nsIRunnable.h"
|
||||
#include "nsEventQueue.h"
|
||||
#include "nsCOMArray.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
class nsThreadPool : public nsIThreadPool, public nsIRunnable
|
||||
{
|
||||
@ -67,6 +68,7 @@ private:
|
||||
PRUint32 mIdleThreadLimit;
|
||||
PRUint32 mIdleThreadTimeout;
|
||||
PRUint32 mIdleCount;
|
||||
nsCOMPtr<nsIThreadPoolListener> mListener;
|
||||
PRBool mShutdown;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user