michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "prinit.h" michael@0: #include "prprf.h" michael@0: #include "prthread.h" michael@0: #include "prcvar.h" michael@0: #include "prlock.h" michael@0: #include "prlog.h" michael@0: #include "prmem.h" michael@0: michael@0: #include "primpl.h" michael@0: michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: michael@0: static PRInt32 RandomNum(void) michael@0: { michael@0: PRInt32 ran = rand() >> 16; michael@0: return ran; michael@0: } /* RandomNum */ michael@0: michael@0: static void Help(void) michael@0: { michael@0: PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); michael@0: PR_fprintf(err, "many_cv usage: [-c n] [-l n] [-h]\n"); michael@0: PR_fprintf(err, "\t-c n Number of conditions per lock (default: 10)\n"); michael@0: PR_fprintf(err, "\t-l n Number of times to loop the test (default: 1)\n"); michael@0: PR_fprintf(err, "\t-h This message and nothing else\n"); michael@0: } /* Help */ michael@0: michael@0: static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv ) michael@0: { michael@0: PLOptStatus os; michael@0: PRIntn index, nl; michael@0: PRLock *ml = NULL; michael@0: PRCondVar **cv = NULL; michael@0: PRBool stats = PR_FALSE; michael@0: PRIntn nc, loops = 1, cvs = 10; michael@0: PRFileDesc *err = PR_GetSpecialFD(PR_StandardError); michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "hsc:l:"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 's': /* number of CVs to association with lock */ michael@0: stats = PR_TRUE; michael@0: break; michael@0: case 'c': /* number of CVs to association with lock */ michael@0: cvs = atoi(opt->value); michael@0: break; michael@0: case 'l': /* number of times to run the tests */ michael@0: loops = atoi(opt->value); michael@0: break; michael@0: case 'h': /* user wants some guidance */ michael@0: default: michael@0: Help(); /* so give him an earful */ michael@0: return 2; /* but not a lot else */ michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_fprintf(err, "Settings\n"); michael@0: PR_fprintf(err, "\tConditions / lock: %d\n", cvs); michael@0: PR_fprintf(err, "\tLoops to run test: %d\n", loops); michael@0: michael@0: ml = PR_NewLock(); michael@0: PR_ASSERT(NULL != ml); michael@0: michael@0: cv = (PRCondVar**)PR_CALLOC(sizeof(PRCondVar*) * cvs); michael@0: PR_ASSERT(NULL != cv); michael@0: michael@0: for (index = 0; index < cvs; ++index) michael@0: { michael@0: cv[index] = PR_NewCondVar(ml); michael@0: PR_ASSERT(NULL != cv[index]); michael@0: } michael@0: michael@0: for (index = 0; index < loops; ++index) michael@0: { michael@0: PR_Lock(ml); michael@0: for (nl = 0; nl < cvs; ++nl) michael@0: { michael@0: PRInt32 ran = RandomNum() % 8; michael@0: if (0 == ran) PR_NotifyAllCondVar(cv[nl]); michael@0: else for (nc = 0; nc < ran; ++nc) michael@0: PR_NotifyCondVar(cv[nl]); michael@0: } michael@0: PR_Unlock(ml); michael@0: } michael@0: michael@0: for (index = 0; index < cvs; ++index) michael@0: PR_DestroyCondVar(cv[index]); michael@0: michael@0: PR_DELETE(cv); michael@0: michael@0: PR_DestroyLock(ml); michael@0: michael@0: printf("PASS\n"); michael@0: michael@0: PT_FPrintStats(err, "\nPThread Statistics\n"); michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRIntn rv; michael@0: michael@0: PR_STDIO_INIT(); michael@0: rv = PR_Initialize(RealMain, argc, argv, 0); michael@0: return rv; michael@0: } /* main */