VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestThreads.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsIThread.h"
40#include "nsIRunnable.h"
41#include <stdio.h>
42#include <stdlib.h>
43#include "nspr.h"
44#include "nsCOMPtr.h"
45#include "nsIServiceManager.h"
46
47class nsRunner : public nsIRunnable {
48public:
49 NS_DECL_ISUPPORTS
50
51 NS_IMETHOD Run() {
52 nsCOMPtr<nsIThread> thread;
53 nsresult rv = nsIThread::GetCurrent(getter_AddRefs(thread));
54 if (NS_FAILED(rv)) {
55 printf("failed to get current thread\n");
56 return rv;
57 }
58 printf("running %d on thread %p\n", mNum, (void *)thread.get());
59
60 // if we don't do something slow, we'll never see the other
61 // worker threads run
62 PR_Sleep(PR_MillisecondsToInterval(100));
63
64 return rv;
65 }
66
67 nsRunner(int num) : mNum(num) {
68 }
69
70protected:
71 int mNum;
72};
73
74NS_IMPL_THREADSAFE_ISUPPORTS1(nsRunner, nsIRunnable)
75
76nsresult
77TestThreads()
78{
79 nsresult rv;
80
81 nsCOMPtr<nsIThread> runner;
82 rv = NS_NewThread(getter_AddRefs(runner), new nsRunner(0), 0, PR_JOINABLE_THREAD);
83 if (NS_FAILED(rv)) {
84 printf("failed to create thread\n");
85 return rv;
86 }
87
88 nsCOMPtr<nsIThread> thread;
89 rv = nsIThread::GetCurrent(getter_AddRefs(thread));
90 if (NS_FAILED(rv)) {
91 printf("failed to get current thread\n");
92 return rv;
93 }
94
95 PRThreadScope scope;
96 rv = runner->GetScope(&scope);
97 if (NS_FAILED(rv)) {
98 printf("runner already exited\n");
99 }
100
101 rv = runner->Join(); // wait for the runner to die before quitting
102 if (NS_FAILED(rv)) {
103 printf("join failed\n");
104 }
105
106 rv = runner->GetScope(&scope); // this should fail after Join
107 if (NS_SUCCEEDED(rv)) {
108 printf("get scope failed\n");
109 }
110
111 rv = runner->Interrupt(); // this should fail after Join
112 if (NS_SUCCEEDED(rv)) {
113 printf("interrupt failed\n");
114 }
115
116 ////////////////////////////////////////////////////////////////////////////
117 // try an unjoinable thread
118 rv = NS_NewThread(getter_AddRefs(runner), new nsRunner(1));
119 if (NS_FAILED(rv)) {
120 printf("failed to create thread\n");
121 return rv;
122 }
123
124 rv = runner->Join(); // wait for the runner to die before quitting
125 if (NS_SUCCEEDED(rv)) {
126 printf("shouldn't have been able to join an unjoinable thread\n");
127 }
128
129 PR_Sleep(PR_MillisecondsToInterval(100)); // hopefully the runner will quit here
130
131 return NS_OK;
132}
133
134class nsStressRunner : public nsIRunnable {
135public:
136 NS_DECL_ISUPPORTS
137
138 NS_IMETHOD Run() {
139 NS_ASSERTION(!mWasRun, "run twice!");
140 mWasRun = PR_TRUE;
141 PR_Sleep(1);
142 if (!PR_AtomicDecrement(&gNum)) {
143 printf(" last thread was %d\n", mNum);
144 }
145 return NS_OK;
146 }
147
148 nsStressRunner(int num) : mNum(num), mWasRun(PR_FALSE) {
149 PR_AtomicIncrement(&gNum);
150 }
151
152 static PRInt32 GetGlobalCount() {return gNum;}
153
154private:
155 ~nsStressRunner() {
156 NS_ASSERTION(mWasRun, "never run!");
157 }
158
159protected:
160 static PRInt32 gNum;
161 PRInt32 mNum;
162 PRBool mWasRun;
163};
164
165PRInt32 nsStressRunner::gNum = 0;
166
167NS_IMPL_THREADSAFE_ISUPPORTS1(nsStressRunner, nsIRunnable)
168
169static int Stress(int loops, int threads)
170{
171
172 for (int i = 0; i < loops; i++) {
173 printf("Loop %d of %d\n", i+1, loops);
174
175 int k;
176 nsIThread** array = new nsIThread*[threads];
177 NS_ASSERTION(array, "out of memory");
178
179 NS_ASSERTION(!nsStressRunner::GetGlobalCount(), "bad count of runnables");
180
181 for (k = 0; k < threads; k++) {
182 nsCOMPtr<nsIThread> t;
183 nsresult rv = NS_NewThread(getter_AddRefs(t),
184 new nsStressRunner(k),
185 0, PR_JOINABLE_THREAD);
186 NS_ASSERTION(NS_SUCCEEDED(rv), "can't create thread");
187 NS_ADDREF(array[k] = t);
188 }
189
190 for (k = threads-1; k >= 0; k--) {
191 array[k]->Join();
192 NS_RELEASE(array[k]);
193 }
194 delete [] array;
195 }
196 return 0;
197}
198
199PR_STATIC_CALLBACK(void) threadProc(void *arg)
200{
201 // printf(" running thread %d\n", (int) arg);
202 PR_Sleep(1);
203 PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(PR_GetCurrentThread()));
204}
205
206static int StressNSPR(int loops, int threads)
207{
208
209 for (int i = 0; i < loops; i++) {
210 printf("Loop %d of %d\n", i+1, loops);
211
212 int k;
213 PRThread** array = new PRThread*[threads];
214 PR_ASSERT(array);
215
216 for (k = 0; k < threads; k++) {
217 array[k] = PR_CreateThread(PR_USER_THREAD,
218 threadProc, (void*) k,
219 PR_PRIORITY_NORMAL,
220 PR_GLOBAL_THREAD,
221 PR_JOINABLE_THREAD,
222 0);
223 PR_ASSERT(array[k]);
224 }
225
226 for (k = 0; k < threads; k++) {
227 PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(array[k]));
228 }
229
230 for (k = threads-1; k >= 0; k--) {
231 PR_JoinThread(array[k]);
232 }
233 delete [] array;
234 }
235 return 0;
236}
237
238
239int
240main(int argc, char** argv)
241{
242 int retval = 0;
243 nsresult rv;
244
245 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
246 if (NS_FAILED(rv)) return -1;
247
248 if (argc > 1 && !strcmp(argv[1], "-stress")) {
249 int loops;
250 int threads;
251 if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' ||
252 !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) {
253 printf("To use -stress you must pass loop count and thread count...\n"
254 " TestThreads -stress -1000 -50\n");
255 } else {
256 printf("Running stress test with %d loops of %d threads each\n",
257 loops, threads);
258 retval = Stress(loops, threads);
259 }
260 } else if (argc > 1 && !strcmp(argv[1], "-stress-nspr")) {
261 int loops;
262 int threads;
263 if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' ||
264 !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) {
265 printf("To use -stress-nspr you must pass loop count and thread count...\n"
266 " TestThreads -stress -1000 -50\n");
267 } else {
268 printf("Running stress test with %d loops of %d threads each\n",
269 loops, threads);
270 retval = StressNSPR(loops, threads);
271 }
272 } else {
273 rv = TestThreads();
274 if (NS_FAILED(rv)) return -1;
275 }
276
277 rv = NS_ShutdownXPCOM(nsnull);
278 if (NS_FAILED(rv)) return -1;
279 return retval;
280}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use