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/. */
20 static PRInt32 RandomNum(void)
22 PRInt32 ran = rand() >> 16;
26 static void Help(void)
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");
35 static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
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:");
46 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
48 if (PL_OPT_BAD == os) continue;
51 case 's': /* number of CVs to association with lock */
54 case 'c': /* number of CVs to association with lock */
55 cvs = atoi(opt->value);
57 case 'l': /* number of times to run the tests */
58 loops = atoi(opt->value);
60 case 'h': /* user wants some guidance */
62 Help(); /* so give him an earful */
63 return 2; /* but not a lot else */
66 PL_DestroyOptState(opt);
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);
73 PR_ASSERT(NULL != ml);
75 cv = (PRCondVar**)PR_CALLOC(sizeof(PRCondVar*) * cvs);
76 PR_ASSERT(NULL != cv);
78 for (index = 0; index < cvs; ++index)
80 cv[index] = PR_NewCondVar(ml);
81 PR_ASSERT(NULL != cv[index]);
84 for (index = 0; index < loops; ++index)
87 for (nl = 0; nl < cvs; ++nl)
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]);
97 for (index = 0; index < cvs; ++index)
98 PR_DestroyCondVar(cv[index]);
106 PT_FPrintStats(err, "\nPThread Statistics\n");
111 int main(int argc, char **argv)
116 rv = PR_Initialize(RealMain, argc, argv, 0);