systhr.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /*
  39. * systhr.c: Abstracted threading mechanisms
  40. *
  41. * Rob McCool
  42. */
  43. #include "systhr.h"
  44. #define USE_NSPR
  45. #ifdef USE_NSPR
  46. #include "nspr.h"
  47. #include "private/prpriv.h"
  48. extern "C" {
  49. int32 PR_GetSysfdTableMax(void);
  50. int32 PR_SetSysfdTableSize(int table_size);
  51. }
  52. #endif
  53. #include "systems.h"
  54. #ifdef THREAD_WIN32
  55. #include <process.h>
  56. typedef struct {
  57. HANDLE hand;
  58. DWORD id;
  59. } sys_thread_s;
  60. #endif
  61. #if defined (USE_NSPR)
  62. #if defined(__hpux) && defined(__ia64)
  63. #define DEFAULT_STACKSIZE (256*1024)
  64. #else
  65. #define DEFAULT_STACKSIZE (64*1024)
  66. #endif
  67. static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;
  68. NSAPI_PUBLIC
  69. void systhread_set_default_stacksize(unsigned long size)
  70. {
  71. _systhr_stacksize = size;
  72. }
  73. NSPR_BEGIN_EXTERN_C
  74. NSAPI_PUBLIC SYS_THREAD
  75. #ifdef UnixWare /* for ANSI C++ standard, see base/systrh.h */
  76. systhread_start(int prio, int stksz, ArgFn_systhread_start fn, void *arg)
  77. #else
  78. systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
  79. #endif
  80. {
  81. #if (defined(Linux) || defined(SNI) || defined(UnixWare)) && !defined(USE_PTHREADS)
  82. prio /= 8; /* quick and dirty fix for user thread priority scale problem */
  83. if (prio > 3) prio = 3;
  84. #endif
  85. PRThread *ret = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))fn,
  86. (void *)arg, (PRThreadPriority)prio,
  87. PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  88. stksz ? stksz : _systhr_stacksize);
  89. return (void *) ret;
  90. }
  91. NSPR_END_EXTERN_C
  92. NSAPI_PUBLIC SYS_THREAD systhread_current(void)
  93. {
  94. return PR_GetCurrentThread();
  95. }
  96. NSAPI_PUBLIC void systhread_yield(void)
  97. {
  98. /* PR_Yield(); */
  99. PR_Sleep(PR_INTERVAL_NO_WAIT);
  100. }
  101. NSAPI_PUBLIC void systhread_timerset(int usec)
  102. {
  103. /* This is an interesting problem. If you ever do turn on interrupts
  104. * on the server, you're in for lots of fun with NSPR Threads
  105. PR_StartEvents(usec); */
  106. }
  107. NSAPI_PUBLIC
  108. SYS_THREAD systhread_attach(void)
  109. {
  110. PRThread *ret;
  111. ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);
  112. return (void *) ret;
  113. }
  114. NSAPI_PUBLIC
  115. void systhread_detach(SYS_THREAD thr)
  116. {
  117. /* XXXMB - this is not correct! */
  118. PR_DetachThread();
  119. }
  120. NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
  121. {
  122. /* Should never be here. PR_DestroyThread is no
  123. * longer used. */
  124. PR_ASSERT(0);
  125. /* PR_DestroyThread((PRThread *) thr); */
  126. }
  127. NSAPI_PUBLIC void systhread_sleep(int milliseconds)
  128. {
  129. PR_Sleep(milliseconds);
  130. }
  131. NSAPI_PUBLIC void systhread_init(char *name)
  132. {
  133. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
  134. #ifdef XP_UNIX
  135. /* XXXrobm allocate all the fd's we can... */
  136. PR_SetSysfdTableSize(PR_GetSysfdTableMax());
  137. #endif
  138. }
  139. NSAPI_PUBLIC int systhread_newkey()
  140. {
  141. uintn newkey;
  142. PR_NewThreadPrivateIndex(&newkey, NULL);
  143. return (newkey);
  144. }
  145. NSAPI_PUBLIC void *systhread_getdata(int key)
  146. {
  147. return PR_GetThreadPrivate(key);
  148. }
  149. NSAPI_PUBLIC void systhread_setdata(int key, void *data)
  150. {
  151. PR_SetThreadPrivate(key, data);
  152. }
  153. /*
  154. * Drag in the Java code, so our dynamic library full of it works
  155. * i.e. force these symbols to load.
  156. */
  157. NSAPI_PUBLIC void systhread_dummy(void)
  158. {
  159. #ifndef NSPR20
  160. /* nspr/gc.c */
  161. PR_InitGC(0,0);
  162. /* nspr/prsystem.c */
  163. PR_GetSystemInfo(PR_SI_SYSNAME, 0, 0);
  164. /* nspr/linker.c */
  165. PR_GetLibName(0, 0);
  166. /* nspr/file.c */
  167. PR_Mkdir(0, 0);
  168. /* nspr/prnetdb.c */
  169. PR_gethostbyname(0, 0, 0, 0, 0);
  170. /* nspr/longlong.c */
  171. LL_TO_S(LL_ZERO, 0, NULL, 0);
  172. #endif /* NSPR20 */
  173. }
  174. #elif defined(THREAD_WIN32)
  175. #include <nspr/prthread.h>
  176. #define DEFAULT_STACKSIZE 262144
  177. NSPR_BEGIN_EXTERN_C
  178. NSAPI_PUBLIC
  179. SYS_THREAD systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
  180. {
  181. sys_thread_s *ret = (sys_thread_s *) MALLOC(sizeof(sys_thread_s));
  182. if ((ret->hand = (HANDLE)_beginthreadex(NULL, stksz, (unsigned (__stdcall *)(void *))fn,
  183. arg, 0, &ret->id)) == 0) {
  184. FREE(ret);
  185. return NULL;
  186. }
  187. return (void *)ret;
  188. }
  189. NSPR_END_EXTERN_C
  190. NSAPI_PUBLIC SYS_THREAD systhread_current(void)
  191. {
  192. /* XXXrobm this is busted.... */
  193. return GetCurrentThread();
  194. }
  195. NSAPI_PUBLIC void systhread_timerset(int usec)
  196. {
  197. }
  198. NSAPI_PUBLIC SYS_THREAD systhread_attach(void)
  199. {
  200. return NULL;
  201. }
  202. NSAPI_PUBLIC void systhread_yield(void)
  203. {
  204. systhread_sleep(0);
  205. }
  206. NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
  207. {
  208. TerminateThread(((sys_thread_s *)thr)->hand, 0);
  209. }
  210. NSAPI_PUBLIC void systhread_sleep(int milliseconds)
  211. {
  212. /* XXXrobm there must be a better way to do this */
  213. HANDLE sem = CreateSemaphore(NULL, 1, 4, "sleeper");
  214. WaitForSingleObject(sem, INFINITE);
  215. WaitForSingleObject(sem, milliseconds);
  216. CloseHandle(sem);
  217. }
  218. NSAPI_PUBLIC void systhread_init(char *name)
  219. {
  220. PR_Init(PR_USER_THREAD, 1, 0);
  221. }
  222. NSAPI_PUBLIC int systhread_newkey()
  223. {
  224. return TlsAlloc();
  225. }
  226. NSAPI_PUBLIC void *systhread_getdata(int key)
  227. {
  228. return (void *)TlsGetValue(key);
  229. }
  230. NSAPI_PUBLIC void systhread_setdata(int key, void *data)
  231. {
  232. TlsSetValue(key, data);
  233. }
  234. #endif