join.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. ***********************************************************************/
  20. /***********************************************************************
  21. ** Includes
  22. ***********************************************************************/
  23. /* Used to get the command line option */
  24. #include "plgetopt.h"
  25. #include "prttools.h"
  26. #include "nspr.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. /***********************************************************************
  31. ** PRIVATE FUNCTION: Test_Result
  32. ** DESCRIPTION: Used in conjunction with the regress tool, prints out the
  33. ** status of the test case.
  34. ** INPUTS: PASS/FAIL
  35. ** OUTPUTS: None
  36. ** RETURN: None
  37. ** SIDE EFFECTS:
  38. **
  39. ** RESTRICTIONS:
  40. ** None
  41. ** MEMORY: NA
  42. ** ALGORITHM: Determine what the status is and print accordingly.
  43. **
  44. ***********************************************************************/
  45. static void Test_Result (int result)
  46. {
  47. if (result == PASS) {
  48. printf ("PASS\n");
  49. }
  50. else {
  51. printf ("FAIL\n");
  52. }
  53. exit (1);
  54. }
  55. /*
  56. Program to test joining of threads. Two threads are created. One
  57. to be waited upon until it has started. The other to join after it has
  58. completed.
  59. */
  60. static void PR_CALLBACK lowPriority(void *arg)
  61. {
  62. }
  63. static void PR_CALLBACK highPriority(void *arg)
  64. {
  65. }
  66. static void PR_CALLBACK unjoinable(void *arg)
  67. {
  68. PR_Sleep(PR_INTERVAL_NO_TIMEOUT);
  69. }
  70. void runTest(PRThreadScope scope1, PRThreadScope scope2)
  71. {
  72. PRThread *low,*high;
  73. /* create the low and high priority threads */
  74. low = PR_CreateThread(PR_USER_THREAD,
  75. lowPriority, 0,
  76. PR_PRIORITY_LOW,
  77. scope1,
  78. PR_JOINABLE_THREAD,
  79. 0);
  80. if (!low) {
  81. if (debug_mode) {
  82. printf("\tcannot create low priority thread\n");
  83. }
  84. else {
  85. Test_Result(FAIL);
  86. }
  87. return;
  88. }
  89. high = PR_CreateThread(PR_USER_THREAD,
  90. highPriority, 0,
  91. PR_PRIORITY_HIGH,
  92. scope2,
  93. PR_JOINABLE_THREAD,
  94. 0);
  95. if (!high) {
  96. if (debug_mode) {
  97. printf("\tcannot create high priority thread\n");
  98. }
  99. else {
  100. Test_Result(FAIL);
  101. }
  102. return;
  103. }
  104. /* Do the joining for both threads */
  105. if (PR_JoinThread(low) == PR_FAILURE) {
  106. if (debug_mode) {
  107. printf("\tcannot join low priority thread\n");
  108. }
  109. else {
  110. Test_Result (FAIL);
  111. }
  112. return;
  113. } else {
  114. if (debug_mode) {
  115. printf("\tjoined low priority thread\n");
  116. }
  117. }
  118. if (PR_JoinThread(high) == PR_FAILURE) {
  119. if (debug_mode) {
  120. printf("\tcannot join high priority thread\n");
  121. }
  122. else {
  123. Test_Result(FAIL);
  124. }
  125. return;
  126. } else {
  127. if (debug_mode) {
  128. printf("\tjoined high priority thread\n");
  129. }
  130. }
  131. }
  132. void joinWithUnjoinable(void)
  133. {
  134. PRThread *thread;
  135. /* create the unjoinable thread */
  136. thread = PR_CreateThread(PR_USER_THREAD,
  137. unjoinable, 0,
  138. PR_PRIORITY_NORMAL,
  139. PR_GLOBAL_THREAD,
  140. PR_UNJOINABLE_THREAD,
  141. 0);
  142. if (!thread) {
  143. if (debug_mode) {
  144. printf("\tcannot create unjoinable thread\n");
  145. }
  146. else {
  147. Test_Result(FAIL);
  148. }
  149. return;
  150. }
  151. if (PR_JoinThread(thread) == PR_SUCCESS) {
  152. if (debug_mode) {
  153. printf("\tsuccessfully joined with unjoinable thread?!\n");
  154. }
  155. else {
  156. Test_Result(FAIL);
  157. }
  158. return;
  159. } else {
  160. if (debug_mode) {
  161. printf("\tcannot join with unjoinable thread, as expected\n");
  162. }
  163. if (PR_GetError() != PR_INVALID_ARGUMENT_ERROR) {
  164. if (debug_mode) {
  165. printf("\tWrong error code\n");
  166. }
  167. else {
  168. Test_Result(FAIL);
  169. }
  170. return;
  171. }
  172. }
  173. if (PR_Interrupt(thread) == PR_FAILURE) {
  174. if (debug_mode) {
  175. printf("\tcannot interrupt unjoinable thread\n");
  176. }
  177. else {
  178. Test_Result(FAIL);
  179. }
  180. return;
  181. } else {
  182. if (debug_mode) {
  183. printf("\tinterrupted unjoinable thread\n");
  184. }
  185. }
  186. }
  187. static PRIntn PR_CALLBACK RealMain(int argc, char **argv)
  188. {
  189. /* The command line argument: -d is used to determine if the test is being run
  190. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  191. All of the printfs associated with this test has been handled with a if (debug_mode)
  192. test.
  193. Usage: test_name -d
  194. */
  195. PLOptStatus os;
  196. PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  197. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  198. {
  199. if (PL_OPT_BAD == os) {
  200. continue;
  201. }
  202. switch (opt->option)
  203. {
  204. case 'd': /* debug mode */
  205. debug_mode = 1;
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. PL_DestroyOptState(opt);
  212. /* main test */
  213. printf("User-User test\n");
  214. runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
  215. printf("User-Kernel test\n");
  216. runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
  217. printf("Kernel-User test\n");
  218. runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
  219. printf("Kernel-Kernel test\n");
  220. runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
  221. printf("Join with unjoinable thread\n");
  222. joinWithUnjoinable();
  223. printf("PASSED\n");
  224. return 0;
  225. }
  226. int main(int argc, char **argv)
  227. {
  228. PRIntn rv;
  229. PR_STDIO_INIT();
  230. rv = PR_Initialize(RealMain, argc, argv, 0);
  231. return rv;
  232. } /* main */