1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
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 the Netscape Portable Runtime (NSPR).
|
---|
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-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
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.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nspr.h"
|
---|
39 | #include "prinrval.h"
|
---|
40 | #include "plgetopt.h"
|
---|
41 | #include "pprthred.h"
|
---|
42 | #include <stdio.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <string.h>
|
---|
45 |
|
---|
46 | #ifdef XP_MAC
|
---|
47 | #include "prlog.h"
|
---|
48 | #define printf PR_LogPrint
|
---|
49 | extern void SetupMacPrintfLog(char *logFile);
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | PRMonitor *mon;
|
---|
53 | PRInt32 count, iterations, alive;
|
---|
54 |
|
---|
55 | PRBool debug_mode = PR_FALSE, passed = PR_TRUE;
|
---|
56 |
|
---|
57 | void
|
---|
58 | PR_CALLBACK
|
---|
59 | ReallyDumbThread(void *arg)
|
---|
60 | {
|
---|
61 | return;
|
---|
62 | }
|
---|
63 |
|
---|
64 | void
|
---|
65 | PR_CALLBACK
|
---|
66 | DumbThread(void *arg)
|
---|
67 | {
|
---|
68 | PRInt32 tmp = (PRInt32)arg;
|
---|
69 | PRThreadScope scope = (PRThreadScope)tmp;
|
---|
70 | PRThread *thr;
|
---|
71 |
|
---|
72 | thr = PR_CreateThread(PR_USER_THREAD,
|
---|
73 | ReallyDumbThread,
|
---|
74 | NULL,
|
---|
75 | PR_PRIORITY_NORMAL,
|
---|
76 | scope,
|
---|
77 | PR_JOINABLE_THREAD,
|
---|
78 | 0);
|
---|
79 |
|
---|
80 | if (!thr) {
|
---|
81 | if (debug_mode) {
|
---|
82 | printf("Could not create really dumb thread (%d, %d)!\n",
|
---|
83 | PR_GetError(), PR_GetOSError());
|
---|
84 | }
|
---|
85 | passed = PR_FALSE;
|
---|
86 | } else {
|
---|
87 | PR_JoinThread(thr);
|
---|
88 | }
|
---|
89 | PR_EnterMonitor(mon);
|
---|
90 | alive--;
|
---|
91 | PR_Notify(mon);
|
---|
92 | PR_ExitMonitor(mon);
|
---|
93 | }
|
---|
94 |
|
---|
95 | static void CreateThreads(PRThreadScope scope1, PRThreadScope scope2)
|
---|
96 | {
|
---|
97 | PRThread *thr;
|
---|
98 | int n;
|
---|
99 |
|
---|
100 | alive = 0;
|
---|
101 | mon = PR_NewMonitor();
|
---|
102 |
|
---|
103 | alive = count;
|
---|
104 | for (n=0; n<count; n++) {
|
---|
105 | thr = PR_CreateThread(PR_USER_THREAD,
|
---|
106 | DumbThread,
|
---|
107 | (void *)scope2,
|
---|
108 | PR_PRIORITY_NORMAL,
|
---|
109 | scope1,
|
---|
110 | PR_UNJOINABLE_THREAD,
|
---|
111 | 0);
|
---|
112 | if (!thr) {
|
---|
113 | if (debug_mode) {
|
---|
114 | printf("Could not create dumb thread (%d, %d)!\n",
|
---|
115 | PR_GetError(), PR_GetOSError());
|
---|
116 | }
|
---|
117 | passed = PR_FALSE;
|
---|
118 | alive--;
|
---|
119 | }
|
---|
120 |
|
---|
121 | PR_Sleep(0);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Wait for all threads to exit */
|
---|
125 | PR_EnterMonitor(mon);
|
---|
126 | while (alive) {
|
---|
127 | PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
|
---|
128 | }
|
---|
129 |
|
---|
130 | PR_ExitMonitor(mon);
|
---|
131 | PR_DestroyMonitor(mon);
|
---|
132 | }
|
---|
133 |
|
---|
134 | static void CreateThreadsUU(void)
|
---|
135 | {
|
---|
136 | CreateThreads(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
|
---|
137 | }
|
---|
138 |
|
---|
139 | static void CreateThreadsUK(void)
|
---|
140 | {
|
---|
141 | CreateThreads(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
|
---|
142 | }
|
---|
143 |
|
---|
144 | static void CreateThreadsKU(void)
|
---|
145 | {
|
---|
146 | CreateThreads(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
|
---|
147 | }
|
---|
148 |
|
---|
149 | static void CreateThreadsKK(void)
|
---|
150 | {
|
---|
151 | CreateThreads(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /************************************************************************/
|
---|
155 |
|
---|
156 | static void Measure(void (*func)(void), const char *msg)
|
---|
157 | {
|
---|
158 | PRIntervalTime start, stop;
|
---|
159 | double d;
|
---|
160 |
|
---|
161 | start = PR_IntervalNow();
|
---|
162 | (*func)();
|
---|
163 | stop = PR_IntervalNow();
|
---|
164 |
|
---|
165 | if (debug_mode)
|
---|
166 | {
|
---|
167 | d = (double)PR_IntervalToMicroseconds(stop - start);
|
---|
168 | printf("%40s: %6.2f usec\n", msg, d / count);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | int main(int argc, char **argv)
|
---|
173 | {
|
---|
174 | int index;
|
---|
175 |
|
---|
176 | PR_STDIO_INIT();
|
---|
177 | PR_Init(PR_USER_THREAD, PR_PRIORITY_HIGH, 0);
|
---|
178 |
|
---|
179 | {
|
---|
180 | PLOptStatus os;
|
---|
181 | PLOptState *opt = PL_CreateOptState(argc, argv, "dc:i:");
|
---|
182 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
183 | {
|
---|
184 | if (PL_OPT_BAD == os) continue;
|
---|
185 | switch (opt->option)
|
---|
186 | {
|
---|
187 | case 'd': /* debug mode */
|
---|
188 | debug_mode = PR_TRUE;
|
---|
189 | break;
|
---|
190 | case 'c': /* loop counter */
|
---|
191 | count = atoi(opt->value);
|
---|
192 | break;
|
---|
193 | case 'i': /* loop counter */
|
---|
194 | iterations = atoi(opt->value);
|
---|
195 | break;
|
---|
196 | default:
|
---|
197 | break;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | PL_DestroyOptState(opt);
|
---|
201 | }
|
---|
202 |
|
---|
203 | #ifdef XP_MAC
|
---|
204 | SetupMacPrintfLog("threads.log");
|
---|
205 | count = 10;
|
---|
206 | iterations = 10;
|
---|
207 | debug_mode = PR_TRUE;
|
---|
208 | #else
|
---|
209 | if (0 == count) count = 50;
|
---|
210 | if (0 == iterations) iterations = 10;
|
---|
211 |
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | if (debug_mode)
|
---|
215 | {
|
---|
216 | printf("\
|
---|
217 | ** Tests lots of thread creations. \n\
|
---|
218 | ** Create %ld native threads %ld times. \n\
|
---|
219 | ** Create %ld user threads %ld times \n", iterations,count,iterations,count);
|
---|
220 | }
|
---|
221 |
|
---|
222 | for (index=0; index<iterations; index++) {
|
---|
223 | Measure(CreateThreadsUU, "Create user/user threads");
|
---|
224 | Measure(CreateThreadsUK, "Create user/native threads");
|
---|
225 | Measure(CreateThreadsKU, "Create native/user threads");
|
---|
226 | Measure(CreateThreadsKK, "Create native/native threads");
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (debug_mode) printf("\nNow switch to recycling threads \n\n");
|
---|
230 | PR_SetThreadRecycleMode(1);
|
---|
231 |
|
---|
232 | for (index=0; index<iterations; index++) {
|
---|
233 | Measure(CreateThreadsUU, "Create user/user threads");
|
---|
234 | Measure(CreateThreadsUK, "Create user/native threads");
|
---|
235 | Measure(CreateThreadsKU, "Create native/user threads");
|
---|
236 | Measure(CreateThreadsKK, "Create native/native threads");
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | printf("%s\n", ((passed) ? "PASS" : "FAIL"));
|
---|
241 |
|
---|
242 | PR_Cleanup();
|
---|
243 |
|
---|
244 | if (passed) {
|
---|
245 | return 0;
|
---|
246 | } else {
|
---|
247 | return 1;
|
---|
248 | }
|
---|
249 | }
|
---|