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