many_cv.c 3.2 KB

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