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
) {
151 case 'd': /* debug mode */
152 debug_mode
= PR_TRUE
;
154 case 'c': /* loop counter */
155 count
= atoi(opt
->value
);
157 case 'i': /* loop counter */
158 iterations
= atoi(opt
->value
);
164 PL_DestroyOptState(opt
);
170 if (0 == iterations
) {
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");
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"));