nsprpub/pr/tests/many_cv.c

branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
equal deleted inserted replaced
-1:000000000000 0:9e9e366071d0
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 "prinit.h"
7 #include "prprf.h"
8 #include "prthread.h"
9 #include "prcvar.h"
10 #include "prlock.h"
11 #include "prlog.h"
12 #include "prmem.h"
13
14 #include "primpl.h"
15
16 #include "plgetopt.h"
17
18 #include <stdlib.h>
19
20 static PRInt32 RandomNum(void)
21 {
22 PRInt32 ran = rand() >> 16;
23 return ran;
24 } /* RandomNum */
25
26 static void Help(void)
27 {
28 PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
29 PR_fprintf(err, "many_cv usage: [-c n] [-l n] [-h]\n");
30 PR_fprintf(err, "\t-c n Number of conditions per lock (default: 10)\n");
31 PR_fprintf(err, "\t-l n Number of times to loop the test (default: 1)\n");
32 PR_fprintf(err, "\t-h This message and nothing else\n");
33 } /* Help */
34
35 static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
36 {
37 PLOptStatus os;
38 PRIntn index, nl;
39 PRLock *ml = NULL;
40 PRCondVar **cv = NULL;
41 PRBool stats = PR_FALSE;
42 PRIntn nc, loops = 1, cvs = 10;
43 PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
44 PLOptState *opt = PL_CreateOptState(argc, argv, "hsc:l:");
45
46 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
47 {
48 if (PL_OPT_BAD == os) continue;
49 switch (opt->option)
50 {
51 case 's': /* number of CVs to association with lock */
52 stats = PR_TRUE;
53 break;
54 case 'c': /* number of CVs to association with lock */
55 cvs = atoi(opt->value);
56 break;
57 case 'l': /* number of times to run the tests */
58 loops = atoi(opt->value);
59 break;
60 case 'h': /* user wants some guidance */
61 default:
62 Help(); /* so give him an earful */
63 return 2; /* but not a lot else */
64 }
65 }
66 PL_DestroyOptState(opt);
67
68 PR_fprintf(err, "Settings\n");
69 PR_fprintf(err, "\tConditions / lock: %d\n", cvs);
70 PR_fprintf(err, "\tLoops to run test: %d\n", loops);
71
72 ml = PR_NewLock();
73 PR_ASSERT(NULL != ml);
74
75 cv = (PRCondVar**)PR_CALLOC(sizeof(PRCondVar*) * cvs);
76 PR_ASSERT(NULL != cv);
77
78 for (index = 0; index < cvs; ++index)
79 {
80 cv[index] = PR_NewCondVar(ml);
81 PR_ASSERT(NULL != cv[index]);
82 }
83
84 for (index = 0; index < loops; ++index)
85 {
86 PR_Lock(ml);
87 for (nl = 0; nl < cvs; ++nl)
88 {
89 PRInt32 ran = RandomNum() % 8;
90 if (0 == ran) PR_NotifyAllCondVar(cv[nl]);
91 else for (nc = 0; nc < ran; ++nc)
92 PR_NotifyCondVar(cv[nl]);
93 }
94 PR_Unlock(ml);
95 }
96
97 for (index = 0; index < cvs; ++index)
98 PR_DestroyCondVar(cv[index]);
99
100 PR_DELETE(cv);
101
102 PR_DestroyLock(ml);
103
104 printf("PASS\n");
105
106 PT_FPrintStats(err, "\nPThread Statistics\n");
107 return 0;
108 }
109
110
111 int main(int argc, char **argv)
112 {
113 PRIntn rv;
114
115 PR_STDIO_INIT();
116 rv = PR_Initialize(RealMain, argc, argv, 0);
117 return rv;
118 } /* main */

mercurial