Bug 1845715 - Check for failure when getting RegExp match result template r=iain
[gecko.git] / nsprpub / pr / tests / threads.c
blob4a28dc6e58509fc93c2dd8806374883ec5481187
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) {
147 continue;
149 switch (opt->option)
151 case 'd': /* debug mode */
152 debug_mode = PR_TRUE;
153 break;
154 case 'c': /* loop counter */
155 count = atoi(opt->value);
156 break;
157 case 'i': /* loop counter */
158 iterations = atoi(opt->value);
159 break;
160 default:
161 break;
164 PL_DestroyOptState(opt);
167 if (0 == count) {
168 count = 50;
170 if (0 == iterations) {
171 iterations = 10;
174 if (debug_mode)
176 printf("\
177 ** Tests lots of thread creations. \n\
178 ** Create %ld native threads %ld times. \n\
179 ** Create %ld user threads %ld times \n", iterations,count,iterations,count);
182 for (index=0; index<iterations; index++) {
183 Measure(CreateThreadsUU, "Create user/user threads");
184 Measure(CreateThreadsUK, "Create user/native threads");
185 Measure(CreateThreadsKU, "Create native/user threads");
186 Measure(CreateThreadsKK, "Create native/native threads");
189 if (debug_mode) {
190 printf("\nNow switch to recycling threads \n\n");
192 PR_SetThreadRecycleMode(1);
194 for (index=0; index<iterations; index++) {
195 Measure(CreateThreadsUU, "Create user/user threads");
196 Measure(CreateThreadsUK, "Create user/native threads");
197 Measure(CreateThreadsKU, "Create native/user threads");
198 Measure(CreateThreadsKK, "Create native/native threads");
202 printf("%s\n", ((passed) ? "PASS" : "FAIL"));
204 PR_Cleanup();
206 if (passed) {
207 return 0;
208 } else {
209 return 1;