1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "prprf.h"
|
---|
39 | #include "prio.h"
|
---|
40 | #include "prinit.h"
|
---|
41 | #include "prthread.h"
|
---|
42 | #include "prinrval.h"
|
---|
43 |
|
---|
44 | #include "plgetopt.h"
|
---|
45 |
|
---|
46 | #include <stdlib.h>
|
---|
47 |
|
---|
48 | static void PR_CALLBACK Thread(void *sleep)
|
---|
49 | {
|
---|
50 | PR_Sleep(PR_SecondsToInterval((PRUint32)sleep));
|
---|
51 | printf("Thread exiting\n");
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void Help(void)
|
---|
55 | {
|
---|
56 | PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
|
---|
57 | PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n");
|
---|
58 | PR_fprintf(err, "\t-c Call cleanup before exiting (default: false)\n");
|
---|
59 | PR_fprintf(err, "\t-G Use global threads only (default: local)\n");
|
---|
60 | PR_fprintf(err, "\t-t n Number of threads involved (default: 1)\n");
|
---|
61 | PR_fprintf(err, "\t-s n Seconds thread(s) should dally (defaut: 10)\n");
|
---|
62 | PR_fprintf(err, "\t-S n Seconds main() should dally (defaut: 5)\n");
|
---|
63 | PR_fprintf(err, "\t-C n Value to set concurrency (default 1)\n");
|
---|
64 | PR_fprintf(err, "\t-h This message and nothing else\n");
|
---|
65 | } /* Help */
|
---|
66 |
|
---|
67 | PRIntn main(PRIntn argc, char **argv)
|
---|
68 | {
|
---|
69 | PLOptStatus os;
|
---|
70 | PRBool cleanup = PR_FALSE;
|
---|
71 | PRThreadScope type = PR_LOCAL_THREAD;
|
---|
72 | PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
|
---|
73 | PLOptState *opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:");
|
---|
74 | PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1;
|
---|
75 |
|
---|
76 | PR_STDIO_INIT();
|
---|
77 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
78 | {
|
---|
79 | if (PL_OPT_BAD == os) continue;
|
---|
80 | switch (opt->option)
|
---|
81 | {
|
---|
82 | case 'c': /* call PR_Cleanup() before exiting */
|
---|
83 | cleanup = PR_TRUE;
|
---|
84 | break;
|
---|
85 | case 'G': /* local vs global threads */
|
---|
86 | type = PR_GLOBAL_THREAD;
|
---|
87 | break;
|
---|
88 | case 's': /* time to sleep */
|
---|
89 | child_sleep = atoi(opt->value);
|
---|
90 | break;
|
---|
91 | case 'S': /* time to sleep */
|
---|
92 | main_sleep = atoi(opt->value);
|
---|
93 | break;
|
---|
94 | case 'C': /* number of cpus to create */
|
---|
95 | concurrency = atoi(opt->value);
|
---|
96 | break;
|
---|
97 | case 't': /* number of threads to create */
|
---|
98 | threads = atoi(opt->value);
|
---|
99 | break;
|
---|
100 | case 'h': /* user wants some guidance */
|
---|
101 | Help(); /* so give him an earful */
|
---|
102 | return 2; /* but not a lot else */
|
---|
103 | break;
|
---|
104 | default:
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | PL_DestroyOptState(opt);
|
---|
109 |
|
---|
110 | PR_fprintf(err, "Cleanup settings\n");
|
---|
111 | PR_fprintf(err, "\tThread type: %s\n",
|
---|
112 | (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL");
|
---|
113 | PR_fprintf(err, "\tConcurrency: %d\n", concurrency);
|
---|
114 | PR_fprintf(err, "\tNumber of threads: %d\n", threads);
|
---|
115 | PR_fprintf(err, "\tThread sleep: %d\n", child_sleep);
|
---|
116 | PR_fprintf(err, "\tMain sleep: %d\n", main_sleep);
|
---|
117 | PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT ");
|
---|
118 |
|
---|
119 | PR_SetConcurrency(concurrency);
|
---|
120 |
|
---|
121 | while (threads-- > 0)
|
---|
122 | (void)PR_CreateThread(
|
---|
123 | PR_USER_THREAD, Thread, (void*)child_sleep, PR_PRIORITY_NORMAL,
|
---|
124 | type, PR_UNJOINABLE_THREAD, 0);
|
---|
125 | PR_Sleep(PR_SecondsToInterval(main_sleep));
|
---|
126 |
|
---|
127 | if (cleanup) PR_Cleanup();
|
---|
128 |
|
---|
129 | PR_fprintf(err, "main() exiting\n");
|
---|
130 | return 0;
|
---|
131 | } /* main */
|
---|