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 "prinit.h"
|
---|
39 | #include "prprf.h"
|
---|
40 | #include "prthread.h"
|
---|
41 | #include "prcvar.h"
|
---|
42 | #include "prlock.h"
|
---|
43 | #include "prlog.h"
|
---|
44 | #include "prmem.h"
|
---|
45 |
|
---|
46 | #include "primpl.h"
|
---|
47 |
|
---|
48 | #include "plgetopt.h"
|
---|
49 |
|
---|
50 | #include <stdlib.h>
|
---|
51 |
|
---|
52 | static PRInt32 Random(void)
|
---|
53 | {
|
---|
54 | PRInt32 ran = rand() >> 16;
|
---|
55 | return ran;
|
---|
56 | } /* Random */
|
---|
57 |
|
---|
58 | static void Help(void)
|
---|
59 | {
|
---|
60 | PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
|
---|
61 | PR_fprintf(err, "many_cv usage: [-c n] [-l n] [-h]\n");
|
---|
62 | PR_fprintf(err, "\t-c n Number of conditions per lock (default: 10)\n");
|
---|
63 | PR_fprintf(err, "\t-l n Number of times to loop the test (default: 1)\n");
|
---|
64 | PR_fprintf(err, "\t-h This message and nothing else\n");
|
---|
65 | } /* Help */
|
---|
66 |
|
---|
67 | static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
|
---|
68 | {
|
---|
69 | PLOptStatus os;
|
---|
70 | PRIntn index, nl;
|
---|
71 | PRLock *ml = NULL;
|
---|
72 | PRCondVar **cv = NULL;
|
---|
73 | PRBool stats = PR_FALSE;
|
---|
74 | PRIntn nc, loops = 1, cvs = 10;
|
---|
75 | PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
|
---|
76 | PLOptState *opt = PL_CreateOptState(argc, argv, "hsc:l:");
|
---|
77 |
|
---|
78 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
79 | {
|
---|
80 | if (PL_OPT_BAD == os) continue;
|
---|
81 | switch (opt->option)
|
---|
82 | {
|
---|
83 | case 's': /* number of CVs to association with lock */
|
---|
84 | stats = PR_TRUE;
|
---|
85 | break;
|
---|
86 | case 'c': /* number of CVs to association with lock */
|
---|
87 | cvs = atoi(opt->value);
|
---|
88 | break;
|
---|
89 | case 'l': /* number of times to run the tests */
|
---|
90 | loops = atoi(opt->value);
|
---|
91 | break;
|
---|
92 | case 'h': /* user wants some guidance */
|
---|
93 | default:
|
---|
94 | Help(); /* so give him an earful */
|
---|
95 | return 2; /* but not a lot else */
|
---|
96 | }
|
---|
97 | }
|
---|
98 | PL_DestroyOptState(opt);
|
---|
99 |
|
---|
100 | PR_fprintf(err, "Settings\n");
|
---|
101 | PR_fprintf(err, "\tConditions / lock: %d\n", cvs);
|
---|
102 | PR_fprintf(err, "\tLoops to run test: %d\n", loops);
|
---|
103 |
|
---|
104 | ml = PR_NewLock();
|
---|
105 | PR_ASSERT(NULL != ml);
|
---|
106 |
|
---|
107 | cv = (PRCondVar**)PR_CALLOC(sizeof(PRCondVar*) * cvs);
|
---|
108 | PR_ASSERT(NULL != cv);
|
---|
109 |
|
---|
110 | for (index = 0; index < cvs; ++index)
|
---|
111 | {
|
---|
112 | cv[index] = PR_NewCondVar(ml);
|
---|
113 | PR_ASSERT(NULL != cv[index]);
|
---|
114 | }
|
---|
115 |
|
---|
116 | for (index = 0; index < loops; ++index)
|
---|
117 | {
|
---|
118 | PR_Lock(ml);
|
---|
119 | for (nl = 0; nl < cvs; ++nl)
|
---|
120 | {
|
---|
121 | PRInt32 ran = Random() % 8;
|
---|
122 | if (0 == ran) PR_NotifyAllCondVar(cv[nl]);
|
---|
123 | else for (nc = 0; nc < ran; ++nc)
|
---|
124 | PR_NotifyCondVar(cv[nl]);
|
---|
125 | }
|
---|
126 | PR_Unlock(ml);
|
---|
127 | }
|
---|
128 |
|
---|
129 | for (index = 0; index < cvs; ++index)
|
---|
130 | PR_DestroyCondVar(cv[index]);
|
---|
131 |
|
---|
132 | PR_DELETE(cv);
|
---|
133 |
|
---|
134 | PR_DestroyLock(ml);
|
---|
135 |
|
---|
136 | printf("PASS\n");
|
---|
137 |
|
---|
138 | PT_FPrintStats(err, "\nPThread Statistics\n");
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | PRIntn main(PRIntn argc, char **argv)
|
---|
144 | {
|
---|
145 | PRIntn rv;
|
---|
146 |
|
---|
147 | PR_STDIO_INIT();
|
---|
148 | rv = PR_Initialize(RealMain, argc, argv, 0);
|
---|
149 | return rv;
|
---|
150 | } /* main */
|
---|