tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

many_cv.c (2954B)


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