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 "nsThreadUtils.h"
45 #include "nsXPCOMCIDInternal.h"
51 #include "mozilla/Monitor.h"
52 using namespace mozilla
;
54 #define NUMBER_OF_THREADS 4
56 // One hour... because test boxes can be slow!
57 #define IDLE_THREAD_TIMEOUT 3600000
59 static nsIThread
** gCreatedThreadList
= nsnull
;
60 static nsIThread
** gShutDownThreadList
= nsnull
;
62 static Monitor
* gMonitor
= nsnull
;
64 static PRBool gAllRunnablesPosted
= PR_FALSE
;
65 static PRBool gAllThreadsCreated
= PR_FALSE
;
66 static PRBool gAllThreadsShutDown
= PR_FALSE
;
69 #define TEST_ASSERTION(_test, _msg) \
70 NS_ASSERTION(_test, _msg);
72 #define TEST_ASSERTION(_test, _msg) \
75 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
80 class Listener
: public nsIThreadPoolListener
84 NS_DECL_NSITHREADPOOLLISTENER
87 NS_IMPL_THREADSAFE_ISUPPORTS1(Listener
, nsIThreadPoolListener
)
90 Listener::OnThreadCreated()
92 nsCOMPtr
<nsIThread
> current(do_GetCurrentThread());
93 TEST_ASSERTION(current
, "Couldn't get current thread!");
95 MonitorAutoEnter
mon(*gMonitor
);
97 while (!gAllRunnablesPosted
) {
101 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
102 nsIThread
* thread
= gCreatedThreadList
[i
];
103 TEST_ASSERTION(thread
!= current
, "Saw the same thread twice!");
106 gCreatedThreadList
[i
] = current
;
107 if (i
== (NUMBER_OF_THREADS
- 1)) {
108 gAllThreadsCreated
= PR_TRUE
;
115 TEST_ASSERTION(PR_FALSE
, "Too many threads!");
116 return NS_ERROR_FAILURE
;
120 Listener::OnThreadShuttingDown()
122 nsCOMPtr
<nsIThread
> current(do_GetCurrentThread());
123 TEST_ASSERTION(current
, "Couldn't get current thread!");
125 MonitorAutoEnter
mon(*gMonitor
);
127 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
128 nsIThread
* thread
= gShutDownThreadList
[i
];
129 TEST_ASSERTION(thread
!= current
, "Saw the same thread twice!");
132 gShutDownThreadList
[i
] = current
;
133 if (i
== (NUMBER_OF_THREADS
- 1)) {
134 gAllThreadsShutDown
= PR_TRUE
;
141 TEST_ASSERTION(PR_FALSE
, "Too many threads!");
142 return NS_ERROR_FAILURE
;
145 class AutoCreateAndDestroyMonitor
148 AutoCreateAndDestroyMonitor(Monitor
** aMonitorPtr
)
149 : mMonitorPtr(aMonitorPtr
) {
150 *aMonitorPtr
= new Monitor("TestThreadPoolListener::AutoMon");
151 TEST_ASSERTION(*aMonitorPtr
, "Out of memory!");
154 ~AutoCreateAndDestroyMonitor() {
157 *mMonitorPtr
= nsnull
;
162 Monitor
** mMonitorPtr
;
165 int main(int argc
, char** argv
)
167 ScopedXPCOM
xpcom("ThreadPoolListener");
168 NS_ENSURE_FALSE(xpcom
.failed(), 1);
170 nsIThread
* createdThreadList
[NUMBER_OF_THREADS
] = { nsnull
};
171 gCreatedThreadList
= createdThreadList
;
173 nsIThread
* shutDownThreadList
[NUMBER_OF_THREADS
] = { nsnull
};
174 gShutDownThreadList
= shutDownThreadList
;
176 AutoCreateAndDestroyMonitor
newMon(&gMonitor
);
177 NS_ENSURE_TRUE(gMonitor
, 1);
181 // Grab the proxy service before messing with the thread pool. This is a
182 // workaround for bug 449822 where the thread pool shutdown can create two
183 // instances of the proxy service and hang.
184 nsCOMPtr
<nsIProxyObjectManager
> proxyObjMgr
=
185 do_GetService(NS_XPCOMPROXY_CONTRACTID
, &rv
);
186 NS_ENSURE_SUCCESS(rv
, rv
);
188 nsCOMPtr
<nsIThreadPool
> pool
=
189 do_CreateInstance(NS_THREADPOOL_CONTRACTID
, &rv
);
190 NS_ENSURE_SUCCESS(rv
, 1);
192 rv
= pool
->SetThreadLimit(NUMBER_OF_THREADS
);
193 NS_ENSURE_SUCCESS(rv
, 1);
195 rv
= pool
->SetIdleThreadLimit(NUMBER_OF_THREADS
);
196 NS_ENSURE_SUCCESS(rv
, 1);
198 rv
= pool
->SetIdleThreadTimeout(IDLE_THREAD_TIMEOUT
);
199 NS_ENSURE_SUCCESS(rv
, 1);
201 nsCOMPtr
<nsIThreadPoolListener
> listener
= new Listener();
202 NS_ENSURE_TRUE(listener
, 1);
204 rv
= pool
->SetListener(listener
);
205 NS_ENSURE_SUCCESS(rv
, 1);
208 MonitorAutoEnter
mon(*gMonitor
);
210 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
211 nsCOMPtr
<nsIRunnable
> runnable
= new nsRunnable();
212 NS_ENSURE_TRUE(runnable
, 1);
214 rv
= pool
->Dispatch(runnable
, NS_DISPATCH_NORMAL
);
215 NS_ENSURE_SUCCESS(rv
, 1);
218 gAllRunnablesPosted
= PR_TRUE
;
223 MonitorAutoEnter
mon(*gMonitor
);
224 while (!gAllThreadsCreated
) {
229 rv
= pool
->Shutdown();
230 NS_ENSURE_SUCCESS(rv
, 1);
233 MonitorAutoEnter
mon(*gMonitor
);
234 while (!gAllThreadsShutDown
) {
239 for (PRUint32 i
= 0; i
< NUMBER_OF_THREADS
; i
++) {
240 nsIThread
* created
= gCreatedThreadList
[i
];
241 NS_ENSURE_TRUE(created
, 1);
243 PRBool match
= PR_FALSE
;
244 for (PRUint32 j
= 0; j
< NUMBER_OF_THREADS
; j
++) {
245 nsIThread
* destroyed
= gShutDownThreadList
[j
];
246 NS_ENSURE_TRUE(destroyed
, 1);
248 if (destroyed
== created
) {
254 NS_ENSURE_TRUE(match
, 1);