joinku.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. **
  7. ** Name: dbmalloc1.c
  8. **
  9. ** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
  10. **
  11. ** Modification History:
  12. **
  13. ** 19-May-97 AGarcia - separate the four join tests into different unit test modules.
  14. ** AGarcia- Converted the test to accomodate the debug_mode flag.
  15. ** The debug mode will print all of the printfs associated with this test.
  16. ** The regress mode will be the default mode. Since the regress tool limits
  17. ** the output to a one line status:PASS or FAIL,all of the printf statements
  18. ** have been handled with an if (debug_mode) statement.
  19. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  20. ** recognize the return code from tha main program.
  21. ***********************************************************************/
  22. /***********************************************************************
  23. ** Includes
  24. ***********************************************************************/
  25. /* Used to get the command line option */
  26. #include "plgetopt.h"
  27. #include "nspr.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. PRIntn failed_already=0;
  32. PRIntn debug_mode;
  33. /*
  34. Program to test joining of threads. Two threads are created. One
  35. to be waited upon until it has started. The other to join after it has
  36. completed.
  37. */
  38. static void lowPriority(void *arg)
  39. {
  40. }
  41. static void highPriority(void *arg)
  42. {
  43. }
  44. void runTest(PRThreadScope scope1, PRThreadScope scope2)
  45. {
  46. PRThread *low,*high;
  47. /* create the low and high priority threads */
  48. low = PR_CreateThread(PR_USER_THREAD,
  49. lowPriority, 0,
  50. PR_PRIORITY_LOW,
  51. scope1,
  52. PR_JOINABLE_THREAD,
  53. 0);
  54. if (!low) {
  55. if (debug_mode) {
  56. printf("\tcannot create low priority thread\n");
  57. }
  58. else {
  59. failed_already=1;
  60. }
  61. return;
  62. }
  63. high = PR_CreateThread(PR_USER_THREAD,
  64. highPriority, 0,
  65. PR_PRIORITY_HIGH,
  66. scope2,
  67. PR_JOINABLE_THREAD,
  68. 0);
  69. if (!high) {
  70. if (debug_mode) {
  71. printf("\tcannot create high priority thread\n");
  72. }
  73. else {
  74. failed_already=1;
  75. }
  76. return;
  77. }
  78. /* Do the joining for both threads */
  79. if (PR_JoinThread(low) == PR_FAILURE) {
  80. if (debug_mode) {
  81. printf("\tcannot join low priority thread\n");
  82. }
  83. else {
  84. failed_already=1;
  85. }
  86. return;
  87. } else {
  88. if (debug_mode) {
  89. printf("\tjoined low priority thread\n");
  90. }
  91. }
  92. if (PR_JoinThread(high) == PR_FAILURE) {
  93. if (debug_mode) {
  94. printf("\tcannot join high priority thread\n");
  95. }
  96. else {
  97. failed_already=1;
  98. }
  99. return;
  100. } else {
  101. if (debug_mode) {
  102. printf("\tjoined high priority thread\n");
  103. }
  104. }
  105. }
  106. static PRIntn PR_CALLBACK RealMain( PRIntn argc, char **argv )
  107. {
  108. /* The command line argument: -d is used to determine if the test is being run
  109. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  110. All of the printfs associated with this test has been handled with a if (debug_mode)
  111. test.
  112. Usage: test_name -d
  113. */
  114. PLOptStatus os;
  115. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  116. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  117. {
  118. if (PL_OPT_BAD == os) {
  119. continue;
  120. }
  121. switch (opt->option)
  122. {
  123. case 'd': /* debug mode */
  124. debug_mode = 1;
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. PL_DestroyOptState(opt);
  131. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  132. PR_STDIO_INIT();
  133. /* main test */
  134. if (debug_mode) {
  135. printf("Kernel-User test\n");
  136. }
  137. runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
  138. if(failed_already)
  139. {
  140. printf("FAIL\n");
  141. return 1;
  142. }
  143. else
  144. {
  145. printf("PASS\n");
  146. return 0;
  147. }
  148. }
  149. int main(int argc, char **argv)
  150. {
  151. PRIntn rv;
  152. PR_STDIO_INIT();
  153. rv = PR_Initialize(RealMain, argc, argv, 0);
  154. return rv;
  155. } /* main */