1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/many_cv.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "prinit.h" 1.10 +#include "prprf.h" 1.11 +#include "prthread.h" 1.12 +#include "prcvar.h" 1.13 +#include "prlock.h" 1.14 +#include "prlog.h" 1.15 +#include "prmem.h" 1.16 + 1.17 +#include "primpl.h" 1.18 + 1.19 +#include "plgetopt.h" 1.20 + 1.21 +#include <stdlib.h> 1.22 + 1.23 +static PRInt32 RandomNum(void) 1.24 +{ 1.25 + PRInt32 ran = rand() >> 16; 1.26 + return ran; 1.27 +} /* RandomNum */ 1.28 + 1.29 +static void Help(void) 1.30 +{ 1.31 + PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); 1.32 + PR_fprintf(err, "many_cv usage: [-c n] [-l n] [-h]\n"); 1.33 + PR_fprintf(err, "\t-c n Number of conditions per lock (default: 10)\n"); 1.34 + PR_fprintf(err, "\t-l n Number of times to loop the test (default: 1)\n"); 1.35 + PR_fprintf(err, "\t-h This message and nothing else\n"); 1.36 +} /* Help */ 1.37 + 1.38 +static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv ) 1.39 +{ 1.40 + PLOptStatus os; 1.41 + PRIntn index, nl; 1.42 + PRLock *ml = NULL; 1.43 + PRCondVar **cv = NULL; 1.44 + PRBool stats = PR_FALSE; 1.45 + PRIntn nc, loops = 1, cvs = 10; 1.46 + PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); 1.47 + PLOptState *opt = PL_CreateOptState(argc, argv, "hsc:l:"); 1.48 + 1.49 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.50 + { 1.51 + if (PL_OPT_BAD == os) continue; 1.52 + switch (opt->option) 1.53 + { 1.54 + case 's': /* number of CVs to association with lock */ 1.55 + stats = PR_TRUE; 1.56 + break; 1.57 + case 'c': /* number of CVs to association with lock */ 1.58 + cvs = atoi(opt->value); 1.59 + break; 1.60 + case 'l': /* number of times to run the tests */ 1.61 + loops = atoi(opt->value); 1.62 + break; 1.63 + case 'h': /* user wants some guidance */ 1.64 + default: 1.65 + Help(); /* so give him an earful */ 1.66 + return 2; /* but not a lot else */ 1.67 + } 1.68 + } 1.69 + PL_DestroyOptState(opt); 1.70 + 1.71 + PR_fprintf(err, "Settings\n"); 1.72 + PR_fprintf(err, "\tConditions / lock: %d\n", cvs); 1.73 + PR_fprintf(err, "\tLoops to run test: %d\n", loops); 1.74 + 1.75 + ml = PR_NewLock(); 1.76 + PR_ASSERT(NULL != ml); 1.77 + 1.78 + cv = (PRCondVar**)PR_CALLOC(sizeof(PRCondVar*) * cvs); 1.79 + PR_ASSERT(NULL != cv); 1.80 + 1.81 + for (index = 0; index < cvs; ++index) 1.82 + { 1.83 + cv[index] = PR_NewCondVar(ml); 1.84 + PR_ASSERT(NULL != cv[index]); 1.85 + } 1.86 + 1.87 + for (index = 0; index < loops; ++index) 1.88 + { 1.89 + PR_Lock(ml); 1.90 + for (nl = 0; nl < cvs; ++nl) 1.91 + { 1.92 + PRInt32 ran = RandomNum() % 8; 1.93 + if (0 == ran) PR_NotifyAllCondVar(cv[nl]); 1.94 + else for (nc = 0; nc < ran; ++nc) 1.95 + PR_NotifyCondVar(cv[nl]); 1.96 + } 1.97 + PR_Unlock(ml); 1.98 + } 1.99 + 1.100 + for (index = 0; index < cvs; ++index) 1.101 + PR_DestroyCondVar(cv[index]); 1.102 + 1.103 + PR_DELETE(cv); 1.104 + 1.105 + PR_DestroyLock(ml); 1.106 + 1.107 + printf("PASS\n"); 1.108 + 1.109 + PT_FPrintStats(err, "\nPThread Statistics\n"); 1.110 + return 0; 1.111 +} 1.112 + 1.113 + 1.114 +int main(int argc, char **argv) 1.115 +{ 1.116 + PRIntn rv; 1.117 + 1.118 + PR_STDIO_INIT(); 1.119 + rv = PR_Initialize(RealMain, argc, argv, 0); 1.120 + return rv; 1.121 +} /* main */