|
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/. */ |
|
5 |
|
6 #include "prprf.h" |
|
7 #include "prio.h" |
|
8 #include "prinit.h" |
|
9 #include "prthread.h" |
|
10 #include "prinrval.h" |
|
11 |
|
12 #include "plgetopt.h" |
|
13 |
|
14 #include <stdlib.h> |
|
15 |
|
16 static void PR_CALLBACK Thread(void *sleep) |
|
17 { |
|
18 PR_Sleep(PR_SecondsToInterval((PRUint32)sleep)); |
|
19 printf("Thread exiting\n"); |
|
20 } |
|
21 |
|
22 static void Help(void) |
|
23 { |
|
24 PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); |
|
25 PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n"); |
|
26 PR_fprintf(err, "\t-c Call cleanup before exiting (default: false)\n"); |
|
27 PR_fprintf(err, "\t-G Use global threads only (default: local)\n"); |
|
28 PR_fprintf(err, "\t-t n Number of threads involved (default: 1)\n"); |
|
29 PR_fprintf(err, "\t-s n Seconds thread(s) should dally (defaut: 10)\n"); |
|
30 PR_fprintf(err, "\t-S n Seconds main() should dally (defaut: 5)\n"); |
|
31 PR_fprintf(err, "\t-C n Value to set concurrency (default 1)\n"); |
|
32 PR_fprintf(err, "\t-h This message and nothing else\n"); |
|
33 } /* Help */ |
|
34 |
|
35 int main(int argc, char **argv) |
|
36 { |
|
37 PLOptStatus os; |
|
38 PRBool cleanup = PR_FALSE; |
|
39 PRThreadScope type = PR_LOCAL_THREAD; |
|
40 PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); |
|
41 PLOptState *opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:"); |
|
42 PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1; |
|
43 |
|
44 PR_STDIO_INIT(); |
|
45 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
|
46 { |
|
47 if (PL_OPT_BAD == os) continue; |
|
48 switch (opt->option) |
|
49 { |
|
50 case 'c': /* call PR_Cleanup() before exiting */ |
|
51 cleanup = PR_TRUE; |
|
52 break; |
|
53 case 'G': /* local vs global threads */ |
|
54 type = PR_GLOBAL_THREAD; |
|
55 break; |
|
56 case 's': /* time to sleep */ |
|
57 child_sleep = atoi(opt->value); |
|
58 break; |
|
59 case 'S': /* time to sleep */ |
|
60 main_sleep = atoi(opt->value); |
|
61 break; |
|
62 case 'C': /* number of cpus to create */ |
|
63 concurrency = atoi(opt->value); |
|
64 break; |
|
65 case 't': /* number of threads to create */ |
|
66 threads = atoi(opt->value); |
|
67 break; |
|
68 case 'h': /* user wants some guidance */ |
|
69 Help(); /* so give him an earful */ |
|
70 return 2; /* but not a lot else */ |
|
71 break; |
|
72 default: |
|
73 break; |
|
74 } |
|
75 } |
|
76 PL_DestroyOptState(opt); |
|
77 |
|
78 PR_fprintf(err, "Cleanup settings\n"); |
|
79 PR_fprintf(err, "\tThread type: %s\n", |
|
80 (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL"); |
|
81 PR_fprintf(err, "\tConcurrency: %d\n", concurrency); |
|
82 PR_fprintf(err, "\tNumber of threads: %d\n", threads); |
|
83 PR_fprintf(err, "\tThread sleep: %d\n", child_sleep); |
|
84 PR_fprintf(err, "\tMain sleep: %d\n", main_sleep); |
|
85 PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT "); |
|
86 |
|
87 PR_SetConcurrency(concurrency); |
|
88 |
|
89 while (threads-- > 0) |
|
90 (void)PR_CreateThread( |
|
91 PR_USER_THREAD, Thread, (void*)child_sleep, PR_PRIORITY_NORMAL, |
|
92 type, PR_UNJOINABLE_THREAD, 0); |
|
93 PR_Sleep(PR_SecondsToInterval(main_sleep)); |
|
94 |
|
95 if (cleanup) PR_Cleanup(); |
|
96 |
|
97 PR_fprintf(err, "main() exiting\n"); |
|
98 return 0; |
|
99 } /* main */ |