backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / nsprpub / pr / tests / threads.c
blob1b9b133d950d1a2126582ad1d22a5bc598c80433
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nspr.h"
7 #include "prinrval.h"
8 #include "plgetopt.h"
9 #include "pprthred.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 PRMonitor *mon;
15 PRInt32 count, iterations, alive;
17 PRBool debug_mode = PR_FALSE, passed = PR_TRUE;
19 void
20 PR_CALLBACK
21 ReallyDumbThread(void *arg)
23 return;
26 void
27 PR_CALLBACK
28 DumbThread(void *arg)
30 PRInt32 tmp = (PRInt32)arg;
31 PRThreadScope scope = (PRThreadScope)tmp;
32 PRThread *thr;
34 thr = PR_CreateThread(PR_USER_THREAD,
35 ReallyDumbThread,
36 NULL,
37 PR_PRIORITY_NORMAL,
38 scope,
39 PR_JOINABLE_THREAD,
40 0);
42 if (!thr) {
43 if (debug_mode) {
44 printf("Could not create really dumb thread (%d, %d)!\n",
45 PR_GetError(), PR_GetOSError());
47 passed = PR_FALSE;
48 } else {
49 PR_JoinThread(thr);
51 PR_EnterMonitor(mon);
52 alive--;
53 PR_Notify(mon);
54 PR_ExitMonitor(mon);
57 static void CreateThreads(PRThreadScope scope1, PRThreadScope scope2)
59 PRThread *thr;
60 int n;
62 alive = 0;
63 mon = PR_NewMonitor();
65 alive = count;
66 for (n=0; n<count; n++) {
67 thr = PR_CreateThread(PR_USER_THREAD,
68 DumbThread,
69 (void *)scope2,
70 PR_PRIORITY_NORMAL,
71 scope1,
72 PR_UNJOINABLE_THREAD,
73 0);
74 if (!thr) {
75 if (debug_mode) {
76 printf("Could not create dumb thread (%d, %d)!\n",
77 PR_GetError(), PR_GetOSError());
79 passed = PR_FALSE;
80 alive--;
83 PR_Sleep(0);
86 /* Wait for all threads to exit */
87 PR_EnterMonitor(mon);
88 while (alive) {
89 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
92 PR_ExitMonitor(mon);
93 PR_DestroyMonitor(mon);
96 static void CreateThreadsUU(void)
98 CreateThreads(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
101 static void CreateThreadsUK(void)
103 CreateThreads(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
106 static void CreateThreadsKU(void)
108 CreateThreads(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
111 static void CreateThreadsKK(void)
113 CreateThreads(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
116 /************************************************************************/
118 static void Measure(void (*func)(void), const char *msg)
120 PRIntervalTime start, stop;
121 double d;
123 start = PR_IntervalNow();
124 (*func)();
125 stop = PR_IntervalNow();
127 if (debug_mode)
129 d = (double)PR_IntervalToMicroseconds(stop - start);
130 printf("%40s: %6.2f usec\n", msg, d / count);
134 int main(int argc, char **argv)
136 int index;
138 PR_STDIO_INIT();
139 PR_Init(PR_USER_THREAD, PR_PRIORITY_HIGH, 0);
142 PLOptStatus os;
143 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:i:");
144 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
146 if (PL_OPT_BAD == os) continue;
147 switch (opt->option)
149 case 'd': /* debug mode */
150 debug_mode = PR_TRUE;
151 break;
152 case 'c': /* loop counter */
153 count = atoi(opt->value);
154 break;
155 case 'i': /* loop counter */
156 iterations = atoi(opt->value);
157 break;
158 default:
159 break;
162 PL_DestroyOptState(opt);
165 if (0 == count) count = 50;
166 if (0 == iterations) iterations = 10;
168 if (debug_mode)
170 printf("\
171 ** Tests lots of thread creations. \n\
172 ** Create %ld native threads %ld times. \n\
173 ** Create %ld user threads %ld times \n", iterations,count,iterations,count);
176 for (index=0; index<iterations; index++) {
177 Measure(CreateThreadsUU, "Create user/user threads");
178 Measure(CreateThreadsUK, "Create user/native threads");
179 Measure(CreateThreadsKU, "Create native/user threads");
180 Measure(CreateThreadsKK, "Create native/native threads");
183 if (debug_mode) printf("\nNow switch to recycling threads \n\n");
184 PR_SetThreadRecycleMode(1);
186 for (index=0; index<iterations; index++) {
187 Measure(CreateThreadsUU, "Create user/user threads");
188 Measure(CreateThreadsUK, "Create user/native threads");
189 Measure(CreateThreadsKU, "Create native/user threads");
190 Measure(CreateThreadsKK, "Create native/native threads");
194 printf("%s\n", ((passed) ? "PASS" : "FAIL"));
196 PR_Cleanup();
198 if (passed) {
199 return 0;
200 } else {
201 return 1;