foreign.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. ** File: foreign.c
  7. ** Description: Testing various functions w/ foreign threads
  8. **
  9. ** We create a thread and get it to call exactly one runtime function.
  10. ** The thread is allowed to be created by some other environment that
  11. ** NSPR, but it does not announce itself to the runtime prior to calling
  12. ** in.
  13. **
  14. ** The goal: try to survive.
  15. **
  16. */
  17. #include "prcvar.h"
  18. #include "prenv.h"
  19. #include "prerror.h"
  20. #include "prinit.h"
  21. #include "prinrval.h"
  22. #include "prio.h"
  23. #include "prlock.h"
  24. #include "prlog.h"
  25. #include "prmem.h"
  26. #include "prthread.h"
  27. #include "prtypes.h"
  28. #include "prprf.h"
  29. #include "plgetopt.h"
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. static enum {
  33. thread_nspr, thread_pthread, thread_sproc, thread_win32
  34. } thread_provider;
  35. typedef void (*StartFn)(void*);
  36. typedef struct StartObject
  37. {
  38. StartFn start;
  39. void *arg;
  40. } StartObject;
  41. static PRFileDesc *output;
  42. static int _debug_on = 0;
  43. #define DEFAULT_THREAD_COUNT 10
  44. #define DPRINTF(arg) if (_debug_on) PR_fprintf arg
  45. #if defined(_PR_PTHREADS)
  46. #include <pthread.h>
  47. #include "md/_pth.h"
  48. static void *pthread_start(void *arg)
  49. {
  50. StartFn start = ((StartObject*)arg)->start;
  51. void *data = ((StartObject*)arg)->arg;
  52. PR_Free(arg);
  53. start(data);
  54. return NULL;
  55. } /* pthread_start */
  56. #endif /* defined(_PR_PTHREADS) */
  57. #if defined(WIN32)
  58. #include <windows.h>
  59. #include <process.h> /* for _beginthreadex() */
  60. static PRUintn __stdcall windows_start(void *arg)
  61. {
  62. StartObject *so = (StartObject*)arg;
  63. StartFn start = so->start;
  64. void *data = so->arg;
  65. PR_Free(so);
  66. start(data);
  67. return 0;
  68. } /* windows_start */
  69. #endif /* defined(WIN32) */
  70. static PRStatus NSPRPUB_TESTS_CreateThread(StartFn start, void *arg)
  71. {
  72. PRStatus rv;
  73. switch (thread_provider)
  74. {
  75. case thread_nspr:
  76. {
  77. PRThread *thread = PR_CreateThread(
  78. PR_USER_THREAD, start, arg,
  79. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  80. PR_UNJOINABLE_THREAD, 0);
  81. rv = (NULL == thread) ? PR_FAILURE : PR_SUCCESS;
  82. }
  83. break;
  84. case thread_pthread:
  85. #if defined(_PR_PTHREADS)
  86. {
  87. int rv;
  88. pthread_t id;
  89. pthread_attr_t tattr;
  90. StartObject *start_object;
  91. start_object = PR_NEW(StartObject);
  92. PR_ASSERT(NULL != start_object);
  93. start_object->start = start;
  94. start_object->arg = arg;
  95. rv = _PT_PTHREAD_ATTR_INIT(&tattr);
  96. PR_ASSERT(0 == rv);
  97. rv = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
  98. PR_ASSERT(0 == rv);
  99. rv = pthread_attr_setstacksize(&tattr, 64 * 1024);
  100. PR_ASSERT(0 == rv);
  101. rv = _PT_PTHREAD_CREATE(&id, tattr, pthread_start, start_object);
  102. (void)_PT_PTHREAD_ATTR_DESTROY(&tattr);
  103. return (0 == rv) ? PR_SUCCESS : PR_FAILURE;
  104. }
  105. #else
  106. PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
  107. rv = PR_FAILURE;
  108. break;
  109. #endif /* defined(_PR_PTHREADS) */
  110. case thread_sproc:
  111. PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
  112. rv = PR_FAILURE;
  113. break;
  114. case thread_win32:
  115. #if defined(WIN32)
  116. {
  117. void *th;
  118. PRUintn id;
  119. StartObject *start_object;
  120. start_object = PR_NEW(StartObject);
  121. PR_ASSERT(NULL != start_object);
  122. start_object->start = start;
  123. start_object->arg = arg;
  124. th = (void*)_beginthreadex(
  125. NULL, /* LPSECURITY_ATTRIBUTES - pointer to thread security attributes */
  126. 0U, /* DWORD - initial thread stack size, in bytes */
  127. windows_start, /* LPTHREAD_START_ROUTINE - pointer to thread function */
  128. start_object, /* LPVOID - argument for new thread */
  129. STACK_SIZE_PARAM_IS_A_RESERVATION, /*DWORD dwCreationFlags - creation flags */
  130. &id /* LPDWORD - pointer to returned thread identifier */ );
  131. rv = (NULL == th) ? PR_FAILURE : PR_SUCCESS;
  132. }
  133. #else
  134. PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
  135. rv = PR_FAILURE;
  136. #endif
  137. break;
  138. default:
  139. PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
  140. rv = PR_FAILURE;
  141. }
  142. return rv;
  143. } /* NSPRPUB_TESTS_CreateThread */
  144. static void PR_CALLBACK lazyEntry(void *arg)
  145. {
  146. PR_ASSERT(NULL == arg);
  147. } /* lazyEntry */
  148. static void OneShot(void *arg)
  149. {
  150. PRUintn pdkey;
  151. PRLock *lock;
  152. PRFileDesc *fd;
  153. PRDir *dir;
  154. PRFileDesc *pair[2];
  155. PRIntn test = (PRIntn)arg;
  156. for (test = 0; test < 12; ++test) {
  157. switch (test)
  158. {
  159. case 0:
  160. lock = PR_NewLock();
  161. DPRINTF((output,"Thread[0x%x] called PR_NewLock\n",
  162. PR_GetCurrentThread()));
  163. PR_DestroyLock(lock);
  164. break;
  165. case 1:
  166. (void)PR_SecondsToInterval(1);
  167. DPRINTF((output,"Thread[0x%x] called PR_SecondsToInterval\n",
  168. PR_GetCurrentThread()));
  169. break;
  170. case 2: (void)PR_CreateThread(
  171. PR_USER_THREAD, lazyEntry, NULL, PR_PRIORITY_NORMAL,
  172. PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
  173. DPRINTF((output,"Thread[0x%x] called PR_CreateThread\n",
  174. PR_GetCurrentThread()));
  175. break;
  176. case 3:
  177. fd = PR_Open("foreign.tmp", PR_CREATE_FILE | PR_RDWR, 0666);
  178. DPRINTF((output,"Thread[0x%x] called PR_Open\n",
  179. PR_GetCurrentThread()));
  180. PR_Close(fd);
  181. break;
  182. case 4:
  183. fd = PR_NewUDPSocket();
  184. DPRINTF((output,"Thread[0x%x] called PR_NewUDPSocket\n",
  185. PR_GetCurrentThread()));
  186. PR_Close(fd);
  187. break;
  188. case 5:
  189. fd = PR_NewTCPSocket();
  190. DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocket\n",
  191. PR_GetCurrentThread()));
  192. PR_Close(fd);
  193. break;
  194. case 6:
  195. #define TEMP_DIR "./tmp"
  196. PR_MkDir(TEMP_DIR, 0700);
  197. dir = PR_OpenDir(TEMP_DIR);
  198. DPRINTF((output,"Thread[0x%x] called PR_OpenDir\n",
  199. PR_GetCurrentThread()));
  200. PR_CloseDir(dir);
  201. break;
  202. case 7:
  203. (void)PR_NewThreadPrivateIndex(&pdkey, NULL);
  204. DPRINTF((output,"Thread[0x%x] called PR_NewThreadPrivateIndex\n",
  205. PR_GetCurrentThread()));
  206. break;
  207. case 8:
  208. (void)PR_GetEnv("PATH");
  209. DPRINTF((output,"Thread[0x%x] called PR_GetEnv\n",
  210. PR_GetCurrentThread()));
  211. break;
  212. case 9:
  213. (void)PR_NewTCPSocketPair(pair);
  214. DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocketPair\n",
  215. PR_GetCurrentThread()));
  216. PR_Close(pair[0]);
  217. PR_Close(pair[1]);
  218. break;
  219. case 10:
  220. PR_SetConcurrency(2);
  221. DPRINTF((output,"Thread[0x%x] called PR_SetConcurrency\n",
  222. PR_GetCurrentThread()));
  223. break;
  224. case 11:
  225. PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_HIGH);
  226. DPRINTF((output,"Thread[0x%x] called PR_SetThreadPriority\n",
  227. PR_GetCurrentThread()));
  228. break;
  229. default:
  230. break;
  231. } /* switch() */
  232. }
  233. } /* OneShot */
  234. int main(int argc, char **argv)
  235. {
  236. PRStatus rv;
  237. PRInt32 thread_cnt = DEFAULT_THREAD_COUNT;
  238. PLOptStatus os;
  239. PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
  240. #if defined(WIN32)
  241. thread_provider = thread_win32;
  242. #elif defined(_PR_PTHREADS)
  243. thread_provider = thread_pthread;
  244. #else
  245. thread_provider = thread_nspr;
  246. #endif
  247. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  248. {
  249. if (PL_OPT_BAD == os) {
  250. continue;
  251. }
  252. switch (opt->option)
  253. {
  254. case 'd': /* debug mode */
  255. _debug_on = 1;
  256. break;
  257. case 't': /* thread count */
  258. thread_cnt = atoi(opt->value);
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. PL_DestroyOptState(opt);
  265. PR_SetConcurrency(2);
  266. output = PR_GetSpecialFD(PR_StandardOutput);
  267. while (thread_cnt-- > 0)
  268. {
  269. rv = NSPRPUB_TESTS_CreateThread(OneShot, (void*)thread_cnt);
  270. PR_ASSERT(PR_SUCCESS == rv);
  271. PR_Sleep(PR_MillisecondsToInterval(5));
  272. }
  273. PR_Sleep(PR_SecondsToInterval(3));
  274. return (PR_SUCCESS == PR_Cleanup()) ? 0 : 1;
  275. } /* main */
  276. /* foreign.c */