1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Thread Pool Listener Test Code.
17 * The Initial Developer of the Original Code is
18 * Ben Turner <bent.mozilla@gmail.com>.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
40 #include "nsIProxyObjectManager.h"
41 #include "nsIThread.h"
42 #include "nsIThreadPool.h"
44 #include "nsAutoLock.h"
45 #include "nsThreadUtils.h"
46 #include "nsXPCOMCIDInternal.h"
52 #define NUMBER_OF_THREADS 4
54 // One hour... because test boxes can be slow!
55 #define IDLE_THREAD_TIMEOUT 3600000
57 static nsIThread
** gCreatedThreadList
= nsnull
;
58 static nsIThread
** gShutDownThreadList
= nsnull
;
60 static PRMonitor
* gMonitor
= nsnull
;
62 static PRBool gAllRunnablesPosted
= PR_FALSE
;
63 static PRBool gAllThreadsCreated
= PR_FALSE
;
64 static PRBool gAllThreadsShutDown
= PR_FALSE
;
67 #define TEST_ASSERTION(_test, _msg) \
68 NS_ASSERTION(_test, _msg);
70 #define TEST_ASSERTION(_test, _msg) \
73 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
78 class Listener
: public nsIThreadPoolListener
82 NS_DECL_NSITHREADPOOLLISTENER
85 NS_IMPL_THREADSAFE_ISUPPORTS1(Listener
, nsIThreadPoolListener
)
88 Listener::OnThreadCreated()
90 nsCOMPtr
<nsIThread
> current(do_GetCurrentThread());
91 TEST_ASSERTION(current
, "Couldn't get current thread!");
93 nsAutoMonitor
mon(gMonitor
);
95 while (!gAllRunnablesPosted
) {
99 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
100 nsIThread
* thread
= gCreatedThreadList
[i
];
101 TEST_ASSERTION(thread
!= current
, "Saw the same thread twice!");
104 gCreatedThreadList
[i
] = current
;
105 if (i
== (NUMBER_OF_THREADS
- 1)) {
106 gAllThreadsCreated
= PR_TRUE
;
113 TEST_ASSERTION(PR_FALSE
, "Too many threads!");
114 return NS_ERROR_FAILURE
;
118 Listener::OnThreadShuttingDown()
120 nsCOMPtr
<nsIThread
> current(do_GetCurrentThread());
121 TEST_ASSERTION(current
, "Couldn't get current thread!");
123 nsAutoMonitor
mon(gMonitor
);
125 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
126 nsIThread
* thread
= gShutDownThreadList
[i
];
127 TEST_ASSERTION(thread
!= current
, "Saw the same thread twice!");
130 gShutDownThreadList
[i
] = current
;
131 if (i
== (NUMBER_OF_THREADS
- 1)) {
132 gAllThreadsShutDown
= PR_TRUE
;
139 TEST_ASSERTION(PR_FALSE
, "Too many threads!");
140 return NS_ERROR_FAILURE
;
143 class AutoCreateAndDestroyMonitor
146 AutoCreateAndDestroyMonitor(PRMonitor
** aMonitorPtr
)
147 : mMonitorPtr(aMonitorPtr
) {
148 *aMonitorPtr
= nsAutoMonitor::NewMonitor("TestThreadPoolListener::AutoMon");
149 TEST_ASSERTION(*aMonitorPtr
, "Out of memory!");
152 ~AutoCreateAndDestroyMonitor() {
154 nsAutoMonitor::DestroyMonitor(*mMonitorPtr
);
155 *mMonitorPtr
= nsnull
;
160 PRMonitor
** mMonitorPtr
;
163 int main(int argc
, char** argv
)
165 ScopedXPCOM
xpcom("ThreadPoolListener");
166 NS_ENSURE_FALSE(xpcom
.failed(), 1);
168 nsIThread
* createdThreadList
[NUMBER_OF_THREADS
] = { nsnull
};
169 gCreatedThreadList
= createdThreadList
;
171 nsIThread
* shutDownThreadList
[NUMBER_OF_THREADS
] = { nsnull
};
172 gShutDownThreadList
= shutDownThreadList
;
174 AutoCreateAndDestroyMonitor
newMon(&gMonitor
);
175 NS_ENSURE_TRUE(gMonitor
, 1);
179 // Grab the proxy service before messing with the thread pool. This is a
180 // workaround for bug 449822 where the thread pool shutdown can create two
181 // instances of the proxy service and hang.
182 nsCOMPtr
<nsIProxyObjectManager
> proxyObjMgr
=
183 do_GetService(NS_XPCOMPROXY_CONTRACTID
, &rv
);
184 NS_ENSURE_SUCCESS(rv
, rv
);
186 nsCOMPtr
<nsIThreadPool
> pool
=
187 do_CreateInstance(NS_THREADPOOL_CONTRACTID
, &rv
);
188 NS_ENSURE_SUCCESS(rv
, 1);
190 rv
= pool
->SetThreadLimit(NUMBER_OF_THREADS
);
191 NS_ENSURE_SUCCESS(rv
, 1);
193 rv
= pool
->SetIdleThreadLimit(NUMBER_OF_THREADS
);
194 NS_ENSURE_SUCCESS(rv
, 1);
196 rv
= pool
->SetIdleThreadTimeout(IDLE_THREAD_TIMEOUT
);
197 NS_ENSURE_SUCCESS(rv
, 1);
199 nsCOMPtr
<nsIThreadPoolListener
> listener
= new Listener();
200 NS_ENSURE_TRUE(listener
, 1);
202 rv
= pool
->SetListener(listener
);
203 NS_ENSURE_SUCCESS(rv
, 1);
206 nsAutoMonitor
mon(gMonitor
);
208 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
209 nsCOMPtr
<nsIRunnable
> runnable
= new nsRunnable();
210 NS_ENSURE_TRUE(runnable
, 1);
212 rv
= pool
->Dispatch(runnable
, NS_DISPATCH_NORMAL
);
213 NS_ENSURE_SUCCESS(rv
, 1);
216 gAllRunnablesPosted
= PR_TRUE
;
221 nsAutoMonitor
mon(gMonitor
);
222 while (!gAllThreadsCreated
) {
227 rv
= pool
->Shutdown();
228 NS_ENSURE_SUCCESS(rv
, 1);
231 nsAutoMonitor
mon(gMonitor
);
232 while (!gAllThreadsShutDown
) {
237 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
238 nsIThread
* created
= gCreatedThreadList
[i
];
239 NS_ENSURE_TRUE(created
, 1);
241 PRBool match
= PR_FALSE
;
242 for (PRUint32 j
= 0; j
< NUMBER_OF_THREADS
; j
++) {
243 nsIThread
* destroyed
= gShutDownThreadList
[j
];
244 NS_ENSURE_TRUE(destroyed
, 1);
246 if (destroyed
== created
) {
252 NS_ENSURE_TRUE(match
, 1);