Bug 596580: Fix mozJSSubScriptLoader's version finding. (r=brendan)
[mozilla-central.git] / xpcom / tests / TestThreadPoolListener.cpp
blob13eee1ceda6fa442389d12e15a23751dbc659361
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
13 * License.
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.
22 * Contributor(s):
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"
46 #include "pratom.h"
47 #include "prinrval.h"
48 #include "prmon.h"
49 #include "prthread.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;
68 #ifdef DEBUG
69 #define TEST_ASSERTION(_test, _msg) \
70 NS_ASSERTION(_test, _msg);
71 #else
72 #define TEST_ASSERTION(_test, _msg) \
73 PR_BEGIN_MACRO \
74 if (!(_test)) { \
75 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
76 } \
77 PR_END_MACRO
78 #endif
80 class Listener : public nsIThreadPoolListener
82 public:
83 NS_DECL_ISUPPORTS
84 NS_DECL_NSITHREADPOOLLISTENER
87 NS_IMPL_THREADSAFE_ISUPPORTS1(Listener, nsIThreadPoolListener)
89 NS_IMETHODIMP
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) {
98 mon.Wait();
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!");
105 if (!thread) {
106 gCreatedThreadList[i] = current;
107 if (i == (NUMBER_OF_THREADS - 1)) {
108 gAllThreadsCreated = PR_TRUE;
109 mon.NotifyAll();
111 return NS_OK;
115 TEST_ASSERTION(PR_FALSE, "Too many threads!");
116 return NS_ERROR_FAILURE;
119 NS_IMETHODIMP
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!");
131 if (!thread) {
132 gShutDownThreadList[i] = current;
133 if (i == (NUMBER_OF_THREADS - 1)) {
134 gAllThreadsShutDown = PR_TRUE;
135 mon.NotifyAll();
137 return NS_OK;
141 TEST_ASSERTION(PR_FALSE, "Too many threads!");
142 return NS_ERROR_FAILURE;
145 class AutoCreateAndDestroyMonitor
147 public:
148 AutoCreateAndDestroyMonitor(Monitor** aMonitorPtr)
149 : mMonitorPtr(aMonitorPtr) {
150 *aMonitorPtr = new Monitor("TestThreadPoolListener::AutoMon");
151 TEST_ASSERTION(*aMonitorPtr, "Out of memory!");
154 ~AutoCreateAndDestroyMonitor() {
155 if (*mMonitorPtr) {
156 delete *mMonitorPtr;
157 *mMonitorPtr = nsnull;
161 private:
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);
179 nsresult rv;
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;
219 mon.NotifyAll();
223 MonitorAutoEnter mon(*gMonitor);
224 while (!gAllThreadsCreated) {
225 mon.Wait();
229 rv = pool->Shutdown();
230 NS_ENSURE_SUCCESS(rv, 1);
233 MonitorAutoEnter mon(*gMonitor);
234 while (!gAllThreadsShutDown) {
235 mon.Wait();
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) {
249 match = PR_TRUE;
250 break;
254 NS_ENSURE_TRUE(match, 1);
257 return 0;