systhr.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * systhr.c: Abstracted threading mechanisms
  43. *
  44. * Rob McCool
  45. */
  46. #include "systhr.h"
  47. #define USE_NSPR
  48. #ifdef USE_NSPR
  49. #include "nspr.h"
  50. #include "private/prpriv.h"
  51. #ifdef LINUX
  52. # include <sys/time.h>
  53. # include <sys/resource.h>
  54. #else
  55. /* This declaration should be removed when NSPR newer than v4.6 is picked up,
  56. which should have the fix for bug 326110
  57. */
  58. extern "C" {
  59. int32 PR_GetSysfdTableMax(void);
  60. int32 PR_SetSysfdTableSize(int table_size);
  61. }
  62. #endif
  63. #endif
  64. #include "systems.h"
  65. #ifdef THREAD_WIN32
  66. #include <process.h>
  67. typedef struct {
  68. HANDLE hand;
  69. DWORD id;
  70. } sys_thread_s;
  71. #endif
  72. #if defined (USE_NSPR)
  73. #if defined(__hpux) && defined(__ia64)
  74. #define DEFAULT_STACKSIZE (256*1024)
  75. #else
  76. #define DEFAULT_STACKSIZE (64*1024)
  77. #endif
  78. static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;
  79. NSAPI_PUBLIC
  80. void systhread_set_default_stacksize(unsigned long size)
  81. {
  82. _systhr_stacksize = size;
  83. }
  84. NSPR_BEGIN_EXTERN_C
  85. NSAPI_PUBLIC SYS_THREAD
  86. #ifdef UnixWare /* for ANSI C++ standard, see base/systrh.h */
  87. systhread_start(int prio, int stksz, ArgFn_systhread_start fn, void *arg)
  88. #else
  89. systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
  90. #endif
  91. {
  92. #if (defined(Linux) || defined(SNI) || defined(UnixWare)) && !defined(USE_PTHREADS)
  93. prio /= 8; /* quick and dirty fix for user thread priority scale problem */
  94. if (prio > 3) prio = 3;
  95. #endif
  96. PRThread *ret = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))fn,
  97. (void *)arg, (PRThreadPriority)prio,
  98. PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  99. stksz ? stksz : _systhr_stacksize);
  100. return (void *) ret;
  101. }
  102. NSPR_END_EXTERN_C
  103. NSAPI_PUBLIC SYS_THREAD systhread_current(void)
  104. {
  105. return PR_GetCurrentThread();
  106. }
  107. NSAPI_PUBLIC void systhread_yield(void)
  108. {
  109. /* PR_Yield(); */
  110. PR_Sleep(PR_INTERVAL_NO_WAIT);
  111. }
  112. NSAPI_PUBLIC void systhread_timerset(int usec)
  113. {
  114. /* This is an interesting problem. If you ever do turn on interrupts
  115. * on the server, you're in for lots of fun with NSPR Threads
  116. PR_StartEvents(usec); */
  117. }
  118. NSAPI_PUBLIC
  119. SYS_THREAD systhread_attach(void)
  120. {
  121. PRThread *ret;
  122. ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);
  123. return (void *) ret;
  124. }
  125. NSAPI_PUBLIC
  126. void systhread_detach(SYS_THREAD thr)
  127. {
  128. /* XXXMB - this is not correct! */
  129. PR_DetachThread();
  130. }
  131. NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
  132. {
  133. /* Should never be here. PR_DestroyThread is no
  134. * longer used. */
  135. PR_ASSERT(0);
  136. /* PR_DestroyThread((PRThread *) thr); */
  137. }
  138. NSAPI_PUBLIC void systhread_sleep(int milliseconds)
  139. {
  140. PR_Sleep(milliseconds);
  141. }
  142. NSAPI_PUBLIC void systhread_init(char *name)
  143. {
  144. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
  145. #ifdef XP_UNIX
  146. #ifdef LINUX
  147. /*
  148. * NSPR 4.6 does not export PR_SetSysfdTableSize
  149. * and PR_GetSysfdTableMax by mistake (NSPR Bugzilla
  150. * bug 326110) on platforms that use GCC with symbol
  151. * visibility, so we have to call the system calls
  152. * directly.
  153. */
  154. {
  155. struct rlimit rlim;
  156. if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
  157. return;
  158. rlim.rlim_cur = rlim.rlim_max;
  159. (void) setrlimit(RLIMIT_NOFILE, &rlim);
  160. }
  161. #else
  162. PR_SetSysfdTableSize(PR_GetSysfdTableMax());
  163. #endif
  164. #endif
  165. }
  166. NSAPI_PUBLIC int systhread_newkey()
  167. {
  168. uintn newkey;
  169. PR_NewThreadPrivateIndex(&newkey, NULL);
  170. return (newkey);
  171. }
  172. NSAPI_PUBLIC void *systhread_getdata(int key)
  173. {
  174. return PR_GetThreadPrivate(key);
  175. }
  176. NSAPI_PUBLIC void systhread_setdata(int key, void *data)
  177. {
  178. PR_SetThreadPrivate(key, data);
  179. }
  180. /*
  181. * Drag in the Java code, so our dynamic library full of it works
  182. * i.e. force these symbols to load.
  183. */
  184. NSAPI_PUBLIC void systhread_dummy(void)
  185. {
  186. }
  187. #elif defined(THREAD_WIN32)
  188. #include <nspr/prthread.h>
  189. #define DEFAULT_STACKSIZE 262144
  190. NSPR_BEGIN_EXTERN_C
  191. NSAPI_PUBLIC
  192. SYS_THREAD systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
  193. {
  194. sys_thread_s *ret = (sys_thread_s *) MALLOC(sizeof(sys_thread_s));
  195. if ((ret->hand = (HANDLE)_beginthreadex(NULL, stksz, (unsigned (__stdcall *)(void *))fn,
  196. arg, 0, &ret->id)) == 0) {
  197. FREE(ret);
  198. return NULL;
  199. }
  200. return (void *)ret;
  201. }
  202. NSPR_END_EXTERN_C
  203. NSAPI_PUBLIC SYS_THREAD systhread_current(void)
  204. {
  205. /* XXXrobm this is busted.... */
  206. return GetCurrentThread();
  207. }
  208. NSAPI_PUBLIC void systhread_timerset(int usec)
  209. {
  210. }
  211. NSAPI_PUBLIC SYS_THREAD systhread_attach(void)
  212. {
  213. return NULL;
  214. }
  215. NSAPI_PUBLIC void systhread_yield(void)
  216. {
  217. systhread_sleep(0);
  218. }
  219. NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
  220. {
  221. TerminateThread(((sys_thread_s *)thr)->hand, 0);
  222. }
  223. NSAPI_PUBLIC void systhread_sleep(int milliseconds)
  224. {
  225. /* XXXrobm there must be a better way to do this */
  226. HANDLE sem = CreateSemaphore(NULL, 1, 4, "sleeper");
  227. WaitForSingleObject(sem, INFINITE);
  228. WaitForSingleObject(sem, milliseconds);
  229. CloseHandle(sem);
  230. }
  231. NSAPI_PUBLIC void systhread_init(char *name)
  232. {
  233. PR_Init(PR_USER_THREAD, 1, 0);
  234. }
  235. NSAPI_PUBLIC int systhread_newkey()
  236. {
  237. return TlsAlloc();
  238. }
  239. NSAPI_PUBLIC void *systhread_getdata(int key)
  240. {
  241. return (void *)TlsGetValue(key);
  242. }
  243. NSAPI_PUBLIC void systhread_setdata(int key, void *data)
  244. {
  245. TlsSetValue(key, data);
  246. }
  247. #endif