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/. */
15 PRInt32 count
, iterations
, alive
;
17 PRBool debug_mode
= PR_FALSE
, passed
= PR_TRUE
;
21 ReallyDumbThread(void *arg
)
30 PRInt32 tmp
= (PRInt32
)arg
;
31 PRThreadScope scope
= (PRThreadScope
)tmp
;
34 thr
= PR_CreateThread(PR_USER_THREAD
,
44 printf("Could not create really dumb thread (%d, %d)!\n",
45 PR_GetError(), PR_GetOSError());
57 static void CreateThreads(PRThreadScope scope1
, PRThreadScope scope2
)
63 mon
= PR_NewMonitor();
66 for (n
=0; n
<count
; n
++) {
67 thr
= PR_CreateThread(PR_USER_THREAD
,
76 printf("Could not create dumb thread (%d, %d)!\n",
77 PR_GetError(), PR_GetOSError());
86 /* Wait for all threads to exit */
89 PR_Wait(mon
, PR_INTERVAL_NO_TIMEOUT
);
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
;
123 start
= PR_IntervalNow();
125 stop
= PR_IntervalNow();
129 d
= (double)PR_IntervalToMicroseconds(stop
- start
);
130 printf("%40s: %6.2f usec\n", msg
, d
/ count
);
134 int main(int argc
, char **argv
)
139 PR_Init(PR_USER_THREAD
, PR_PRIORITY_HIGH
, 0);
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;
149 case 'd': /* debug mode */
150 debug_mode
= PR_TRUE
;
152 case 'c': /* loop counter */
153 count
= atoi(opt
->value
);
155 case 'i': /* loop counter */
156 iterations
= atoi(opt
->value
);
162 PL_DestroyOptState(opt
);
165 if (0 == count
) count
= 50;
166 if (0 == iterations
) iterations
= 10;
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"));