attach.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. ** 1996 - Netscape Communications Corporation
  7. **
  8. ** Name: attach.c
  9. **
  10. ** Description: Platform-specific code to create a native thread. The native thread will
  11. ** repeatedly call PR_AttachThread and PR_DetachThread. The
  12. ** primordial thread waits for this new thread to finish.
  13. **
  14. ** Modification History:
  15. ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  16. ** The debug mode will print all of the printfs associated with this test.
  17. ** The regress mode will be the default mode. Since the regress tool limits
  18. ** the output to a one line status:PASS or FAIL,all of the printf statements
  19. ** have been handled with an if (debug_mode) statement.
  20. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  21. ** recognize the return code from tha main program.
  22. ** 12-June-97 Revert to return code 0 and 1.
  23. ***********************************************************************/
  24. /***********************************************************************
  25. ** Includes
  26. ***********************************************************************/
  27. /* Used to get the command line option */
  28. #include "nspr.h"
  29. #include "pprthred.h"
  30. #include "plgetopt.h"
  31. #include <stdio.h>
  32. #ifdef WIN32
  33. #include <windows.h>
  34. #include <process.h>
  35. #elif defined(_PR_PTHREADS)
  36. #include <pthread.h>
  37. #include "md/_pth.h"
  38. #elif defined(SOLARIS)
  39. #include <thread.h>
  40. #elif defined(OS2)
  41. #define INCL_DOS
  42. #define INCL_ERRORS
  43. #include <os2.h>
  44. #include <process.h>
  45. #endif
  46. #define DEFAULT_COUNT 1000
  47. PRIntn failed_already=0;
  48. PRIntn debug_mode;
  49. int count;
  50. static void
  51. AttachDetach(void)
  52. {
  53. PRThread *me;
  54. PRInt32 index;
  55. for (index=0; index<count; index++) {
  56. me = PR_AttachThread(PR_USER_THREAD,
  57. PR_PRIORITY_NORMAL,
  58. NULL);
  59. if (!me) {
  60. fprintf(stderr, "Error attaching thread %d: PR_AttachThread failed\n",
  61. count);
  62. failed_already = 1;
  63. return;
  64. }
  65. PR_DetachThread();
  66. }
  67. }
  68. /************************************************************************/
  69. static void Measure(void (*func)(void), const char *msg)
  70. {
  71. PRIntervalTime start, stop;
  72. double d;
  73. start = PR_IntervalNow();
  74. (*func)();
  75. stop = PR_IntervalNow();
  76. d = (double)PR_IntervalToMicroseconds(stop - start);
  77. if (debug_mode) {
  78. printf("%40s: %6.2f usec\n", msg, d / count);
  79. }
  80. }
  81. #ifdef WIN32
  82. static unsigned __stdcall threadStartFunc(void *arg)
  83. #else
  84. static void * threadStartFunc(void *arg)
  85. #endif
  86. {
  87. Measure(AttachDetach, "Attach/Detach");
  88. return 0;
  89. }
  90. int main(int argc, char **argv)
  91. {
  92. #ifdef _PR_PTHREADS
  93. int rv;
  94. pthread_t threadID;
  95. pthread_attr_t attr;
  96. #elif defined(SOLARIS)
  97. int rv;
  98. thread_t threadID;
  99. #elif defined(WIN32)
  100. DWORD rv;
  101. unsigned threadID;
  102. HANDLE hThread;
  103. #elif defined(OS2)
  104. int rv;
  105. TID threadID;
  106. #endif
  107. /* The command line argument: -d is used to determine if the test is being run
  108. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  109. All of the printfs associated with this test has been handled with a if (debug_mode)
  110. test.
  111. Usage: test_name [-d] [-c n]
  112. */
  113. PLOptStatus os;
  114. PLOptState *opt = PL_CreateOptState(argc, argv, "dc:");
  115. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  116. {
  117. if (PL_OPT_BAD == os) {
  118. continue;
  119. }
  120. switch (opt->option)
  121. {
  122. case 'd': /* debug mode */
  123. debug_mode = 1;
  124. break;
  125. case 'c': /* loop count */
  126. count = atoi(opt->value);
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. PL_DestroyOptState(opt);
  133. #if defined(WIN16)
  134. printf("attach: This test is not valid for Win16\n");
  135. goto exit_now;
  136. #endif
  137. if(0 == count) {
  138. count = DEFAULT_COUNT;
  139. }
  140. /*
  141. * To force the implicit initialization of nspr20
  142. */
  143. PR_SetError(0, 0);
  144. PR_STDIO_INIT();
  145. /*
  146. * Platform-specific code to create a native thread. The native
  147. * thread will repeatedly call PR_AttachThread and PR_DetachThread.
  148. * The primordial thread waits for this new thread to finish.
  149. */
  150. #ifdef _PR_PTHREADS
  151. rv = _PT_PTHREAD_ATTR_INIT(&attr);
  152. if (debug_mode) {
  153. PR_ASSERT(0 == rv);
  154. }
  155. else if (0 != rv) {
  156. failed_already=1;
  157. goto exit_now;
  158. }
  159. rv = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  160. if (debug_mode) {
  161. PR_ASSERT(0 == rv);
  162. }
  163. else if (0 != rv) {
  164. failed_already=1;
  165. goto exit_now;
  166. }
  167. rv = _PT_PTHREAD_CREATE(&threadID, attr, threadStartFunc, NULL);
  168. if (rv != 0) {
  169. fprintf(stderr, "thread creation failed: error code %d\n", rv);
  170. failed_already=1;
  171. goto exit_now;
  172. }
  173. else {
  174. if (debug_mode) {
  175. printf ("thread creation succeeded \n");
  176. }
  177. }
  178. rv = _PT_PTHREAD_ATTR_DESTROY(&attr);
  179. if (debug_mode) {
  180. PR_ASSERT(0 == rv);
  181. }
  182. else if (0 != rv) {
  183. failed_already=1;
  184. goto exit_now;
  185. }
  186. rv = pthread_join(threadID, NULL);
  187. if (debug_mode) {
  188. PR_ASSERT(0 == rv);
  189. }
  190. else if (0 != rv) {
  191. failed_already=1;
  192. goto exit_now;
  193. }
  194. #elif defined(SOLARIS)
  195. rv = thr_create(NULL, 0, threadStartFunc, NULL, 0, &threadID);
  196. if (rv != 0) {
  197. if(!debug_mode) {
  198. failed_already=1;
  199. goto exit_now;
  200. } else {
  201. fprintf(stderr, "thread creation failed: error code %d\n", rv);
  202. }
  203. }
  204. rv = thr_join(threadID, NULL, NULL);
  205. if (debug_mode) {
  206. PR_ASSERT(0 == rv);
  207. }
  208. else if (0 != rv)
  209. {
  210. failed_already=1;
  211. goto exit_now;
  212. }
  213. #elif defined(WIN32)
  214. hThread = (HANDLE) _beginthreadex(NULL, 0, threadStartFunc, NULL,
  215. STACK_SIZE_PARAM_IS_A_RESERVATION, &threadID);
  216. if (hThread == 0) {
  217. fprintf(stderr, "thread creation failed: error code %d\n",
  218. GetLastError());
  219. failed_already=1;
  220. goto exit_now;
  221. }
  222. rv = WaitForSingleObject(hThread, INFINITE);
  223. if (debug_mode) {
  224. PR_ASSERT(rv != WAIT_FAILED);
  225. }
  226. else if (rv == WAIT_FAILED) {
  227. failed_already=1;
  228. goto exit_now;
  229. }
  230. #elif defined(OS2)
  231. threadID = (TID) _beginthread((void *)threadStartFunc, NULL,
  232. 32768, NULL);
  233. if (threadID == -1) {
  234. fprintf(stderr, "thread creation failed: error code %d\n", errno);
  235. failed_already=1;
  236. goto exit_now;
  237. }
  238. rv = DosWaitThread(&threadID, DCWW_WAIT);
  239. if (debug_mode) {
  240. PR_ASSERT(rv == NO_ERROR);
  241. } else if (rv != NO_ERROR) {
  242. failed_already=1;
  243. goto exit_now;
  244. }
  245. #else
  246. if (!debug_mode) {
  247. failed_already=1;
  248. }
  249. else
  250. printf("The attach test does not apply to this platform because\n"
  251. "either this platform does not have native threads or the\n"
  252. "test needs to be written for this platform.\n");
  253. goto exit_now;
  254. #endif
  255. exit_now:
  256. if(failed_already) {
  257. return 1;
  258. }
  259. else {
  260. return 0;
  261. }
  262. }