Debian Codegraph @

nsprpub/pr/tests/cleanup.c

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