Added cairoFontface destroy
[mozilla-central.git] / xpcom / tests / TestThreads.cpp
blob3f12cca748e7a5c998e95af062a11c805ff9a69f
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
13 * License.
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.
22 * Contributor(s):
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"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include "nspr.h"
43 #include "nsCOMPtr.h"
44 #include "nsIServiceManager.h"
45 #include "nsXPCOM.h"
47 class nsRunner : public nsIRunnable {
48 public:
49 NS_DECL_ISUPPORTS
51 NS_IMETHOD Run() {
52 nsCOMPtr<nsIThread> thread;
53 nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread));
54 if (NS_FAILED(rv)) {
55 printf("failed to get current thread\n");
56 return rv;
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
61 // worker threads run
62 PR_Sleep(PR_MillisecondsToInterval(100));
64 return rv;
67 nsRunner(int num) : mNum(num) {
70 protected:
71 int mNum;
74 NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunner, nsIRunnable)
76 nsresult
77 TestThreads()
79 nsresult rv;
81 nsCOMPtr<nsIRunnable> event = new nsRunner(0);
82 if (!event)
83 return NS_ERROR_OUT_OF_MEMORY;
85 nsCOMPtr<nsIThread> runner;
86 rv = NS_NewThread(getter_AddRefs(runner), event);
87 if (NS_FAILED(rv)) {
88 printf("failed to create thread\n");
89 return rv;
92 nsCOMPtr<nsIThread> thread;
93 rv = NS_GetCurrentThread(getter_AddRefs(thread));
94 if (NS_FAILED(rv)) {
95 printf("failed to get current thread\n");
96 return rv;
99 rv = runner->Shutdown(); // wait for the runner to die before quitting
100 if (NS_FAILED(rv)) {
101 printf("join failed\n");
104 PR_Sleep(PR_MillisecondsToInterval(100)); // hopefully the runner will quit here
106 return NS_OK;
109 class nsStressRunner : public nsIRunnable {
110 public:
111 NS_DECL_ISUPPORTS
113 NS_IMETHOD Run() {
114 NS_ASSERTION(!mWasRun, "run twice!");
115 mWasRun = PR_TRUE;
116 PR_Sleep(1);
117 if (!PR_AtomicDecrement(&gNum)) {
118 printf(" last thread was %d\n", mNum);
120 return NS_OK;
123 nsStressRunner(int num) : mNum(num), mWasRun(PR_FALSE) {
124 PR_AtomicIncrement(&gNum);
127 static PRInt32 GetGlobalCount() {return gNum;}
129 private:
130 ~nsStressRunner() {
131 NS_ASSERTION(mWasRun, "never run!");
134 protected:
135 static PRInt32 gNum;
136 PRInt32 mNum;
137 PRBool mWasRun;
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);
150 int k;
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));
159 if (NS_FAILED(rv)) {
160 NS_ERROR("can't create thread");
161 return -1;
163 NS_ADDREF(array[k] = t);
166 for (k = threads-1; k >= 0; k--) {
167 array[k]->Shutdown();
168 NS_RELEASE(array[k]);
170 delete [] array;
172 return 0;
175 PR_STATIC_CALLBACK(void) threadProc(void *arg)
177 // printf(" running thread %d\n", (int) arg);
178 PR_Sleep(1);
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);
188 int k;
189 PRThread** array = new PRThread*[threads];
190 PR_ASSERT(array);
192 for (k = 0; k < threads; k++) {
193 array[k] = PR_CreateThread(PR_USER_THREAD,
194 threadProc, (void*) k,
195 PR_PRIORITY_NORMAL,
196 PR_GLOBAL_THREAD,
197 PR_JOINABLE_THREAD,
199 PR_ASSERT(array[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]);
209 delete [] array;
211 return 0;
216 main(int argc, char** argv)
218 int retval = 0;
219 nsresult rv;
221 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
222 if (NS_FAILED(rv)) return -1;
224 if (argc > 1 && !strcmp(argv[1], "-stress")) {
225 int loops;
226 int threads;
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");
231 } else {
232 printf("Running stress test with %d loops of %d threads each\n",
233 loops, threads);
234 retval = Stress(loops, threads);
236 } else if (argc > 1 && !strcmp(argv[1], "-stress-nspr")) {
237 int loops;
238 int threads;
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");
243 } else {
244 printf("Running stress test with %d loops of %d threads each\n",
245 loops, threads);
246 retval = StressNSPR(loops, threads);
248 } else {
249 rv = TestThreads();
250 if (NS_FAILED(rv)) return -1;
253 rv = NS_ShutdownXPCOM(nsnull);
254 if (NS_FAILED(rv)) return -1;
255 return retval;