cleanup.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "prprf.h"
  6. #include "prio.h"
  7. #include "prinit.h"
  8. #include "prthread.h"
  9. #include "prinrval.h"
  10. #include "plgetopt.h"
  11. #include <stdlib.h>
  12. static void PR_CALLBACK Thread(void *sleep)
  13. {
  14. PR_Sleep(PR_SecondsToInterval((PRUint32)sleep));
  15. printf("Thread exiting\n");
  16. }
  17. static void Help(void)
  18. {
  19. PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
  20. PR_fprintf(err, "Cleanup usage: [-g] [-s n] [-t n] [-c n] [-h]\n");
  21. PR_fprintf(err, "\t-c Call cleanup before exiting (default: false)\n");
  22. PR_fprintf(err, "\t-G Use global threads only (default: local)\n");
  23. PR_fprintf(err, "\t-t n Number of threads involved (default: 1)\n");
  24. PR_fprintf(err, "\t-s n Seconds thread(s) should dally (defaut: 10)\n");
  25. PR_fprintf(err, "\t-S n Seconds main() should dally (defaut: 5)\n");
  26. PR_fprintf(err, "\t-C n Value to set concurrency (default 1)\n");
  27. PR_fprintf(err, "\t-h This message and nothing else\n");
  28. } /* Help */
  29. int main(int argc, char **argv)
  30. {
  31. PLOptStatus os;
  32. PRBool cleanup = PR_FALSE;
  33. PRThreadScope type = PR_LOCAL_THREAD;
  34. PRFileDesc *err = PR_GetSpecialFD(PR_StandardError);
  35. PLOptState *opt = PL_CreateOptState(argc, argv, "Ghs:S:t:cC:");
  36. PRIntn concurrency = 1, child_sleep = 10, main_sleep = 5, threads = 1;
  37. PR_STDIO_INIT();
  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 'c': /* call PR_Cleanup() before exiting */
  46. cleanup = PR_TRUE;
  47. break;
  48. case 'G': /* local vs global threads */
  49. type = PR_GLOBAL_THREAD;
  50. break;
  51. case 's': /* time to sleep */
  52. child_sleep = atoi(opt->value);
  53. break;
  54. case 'S': /* time to sleep */
  55. main_sleep = atoi(opt->value);
  56. break;
  57. case 'C': /* number of cpus to create */
  58. concurrency = atoi(opt->value);
  59. break;
  60. case 't': /* number of threads to create */
  61. threads = atoi(opt->value);
  62. break;
  63. case 'h': /* user wants some guidance */
  64. Help(); /* so give him an earful */
  65. return 2; /* but not a lot else */
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. PL_DestroyOptState(opt);
  72. PR_fprintf(err, "Cleanup settings\n");
  73. PR_fprintf(err, "\tThread type: %s\n",
  74. (PR_LOCAL_THREAD == type) ? "LOCAL" : "GLOBAL");
  75. PR_fprintf(err, "\tConcurrency: %d\n", concurrency);
  76. PR_fprintf(err, "\tNumber of threads: %d\n", threads);
  77. PR_fprintf(err, "\tThread sleep: %d\n", child_sleep);
  78. PR_fprintf(err, "\tMain sleep: %d\n", main_sleep);
  79. PR_fprintf(err, "\tCleanup will %sbe called\n\n", (cleanup) ? "" : "NOT ");
  80. PR_SetConcurrency(concurrency);
  81. while (threads-- > 0)
  82. (void)PR_CreateThread(
  83. PR_USER_THREAD, Thread, (void*)child_sleep, PR_PRIORITY_NORMAL,
  84. type, PR_UNJOINABLE_THREAD, 0);
  85. PR_Sleep(PR_SecondsToInterval(main_sleep));
  86. if (cleanup) {
  87. PR_Cleanup();
  88. }
  89. PR_fprintf(err, "main() exiting\n");
  90. return 0;
  91. } /* main */