1 /* -*- Mode: C++; tab-width: 2; 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 mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsThreadUtils.h"
44 #include "nsIServiceManager.h"
47 class nsRunner
: public nsIRunnable
{
52 nsCOMPtr
<nsIThread
> thread
;
53 nsresult rv
= NS_GetCurrentThread(getter_AddRefs(thread
));
55 printf("failed to get current thread\n");
58 printf("running %d on thread %p\n", mNum
, (void *)thread
.get());
60 // if we don't do something slow, we'll never see the other
62 PR_Sleep(PR_MillisecondsToInterval(100));
67 nsRunner(int num
) : mNum(num
) {
74 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunner
, nsIRunnable
)
81 nsCOMPtr
<nsIRunnable
> event
= new nsRunner(0);
83 return NS_ERROR_OUT_OF_MEMORY
;
85 nsCOMPtr
<nsIThread
> runner
;
86 rv
= NS_NewThread(getter_AddRefs(runner
), event
);
88 printf("failed to create thread\n");
92 nsCOMPtr
<nsIThread
> thread
;
93 rv
= NS_GetCurrentThread(getter_AddRefs(thread
));
95 printf("failed to get current thread\n");
99 rv
= runner
->Shutdown(); // wait for the runner to die before quitting
101 printf("join failed\n");
104 PR_Sleep(PR_MillisecondsToInterval(100)); // hopefully the runner will quit here
109 class nsStressRunner
: public nsIRunnable
{
114 NS_ASSERTION(!mWasRun
, "run twice!");
117 if (!PR_AtomicDecrement(&gNum
)) {
118 printf(" last thread was %d\n", mNum
);
123 nsStressRunner(int num
) : mNum(num
), mWasRun(PR_FALSE
) {
124 PR_AtomicIncrement(&gNum
);
127 static PRInt32
GetGlobalCount() {return gNum
;}
131 NS_ASSERTION(mWasRun
, "never run!");
140 PRInt32
nsStressRunner::gNum
= 0;
142 NS_IMPL_THREADSAFE_ISUPPORTS1(nsStressRunner
, nsIRunnable
)
144 static int Stress(int loops
, int threads
)
147 for (int i
= 0; i
< loops
; i
++) {
148 printf("Loop %d of %d\n", i
+1, loops
);
151 nsIThread
** array
= new nsIThread
*[threads
];
152 NS_ASSERTION(array
, "out of memory");
154 NS_ASSERTION(!nsStressRunner::GetGlobalCount(), "bad count of runnables");
156 for (k
= 0; k
< threads
; k
++) {
157 nsCOMPtr
<nsIThread
> t
;
158 nsresult rv
= NS_NewThread(getter_AddRefs(t
), new nsStressRunner(k
));
160 NS_ERROR("can't create thread");
163 NS_ADDREF(array
[k
] = t
);
166 for (k
= threads
-1; k
>= 0; k
--) {
167 array
[k
]->Shutdown();
168 NS_RELEASE(array
[k
]);
175 static void threadProc(void *arg
)
177 // printf(" running thread %d\n", (int) arg);
179 PR_ASSERT(PR_JOINABLE_THREAD
== PR_GetThreadState(PR_GetCurrentThread()));
182 static int StressNSPR(int loops
, int threads
)
185 for (int i
= 0; i
< loops
; i
++) {
186 printf("Loop %d of %d\n", i
+1, loops
);
189 PRThread
** array
= new PRThread
*[threads
];
192 for (k
= 0; k
< threads
; k
++) {
193 array
[k
] = PR_CreateThread(PR_USER_THREAD
,
194 threadProc
, (void*) k
,
202 for (k
= 0; k
< threads
; k
++) {
203 PR_ASSERT(PR_JOINABLE_THREAD
== PR_GetThreadState(array
[k
]));
206 for (k
= threads
-1; k
>= 0; k
--) {
207 PR_JoinThread(array
[k
]);
216 main(int argc
, char** argv
)
221 rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
222 if (NS_FAILED(rv
)) return -1;
224 if (argc
> 1 && !strcmp(argv
[1], "-stress")) {
227 if (argc
!= 4 || *argv
[2] != '-' || *argv
[3] != '-' ||
228 !(loops
= atoi(argv
[2]+1)) || !(threads
= atoi(argv
[3]+1))) {
229 printf("To use -stress you must pass loop count and thread count...\n"
230 " TestThreads -stress -1000 -50\n");
232 printf("Running stress test with %d loops of %d threads each\n",
234 retval
= Stress(loops
, threads
);
236 } else if (argc
> 1 && !strcmp(argv
[1], "-stress-nspr")) {
239 if (argc
!= 4 || *argv
[2] != '-' || *argv
[3] != '-' ||
240 !(loops
= atoi(argv
[2]+1)) || !(threads
= atoi(argv
[3]+1))) {
241 printf("To use -stress-nspr you must pass loop count and thread count...\n"
242 " TestThreads -stress -1000 -50\n");
244 printf("Running stress test with %d loops of %d threads each\n",
246 retval
= StressNSPR(loops
, threads
);
250 if (NS_FAILED(rv
)) return -1;
253 rv
= NS_ShutdownXPCOM(nsnull
);
254 if (NS_FAILED(rv
)) return -1;