systhr.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * systhr.c: Abstracted threading mechanisms
  14. *
  15. * Rob McCool
  16. */
  17. #include "systhr.h"
  18. #define USE_NSPR
  19. #ifdef USE_NSPR
  20. #include "nspr.h"
  21. #include "private/prpriv.h"
  22. #ifdef LINUX
  23. # include <sys/time.h>
  24. # include <sys/resource.h>
  25. #else
  26. /* This declaration should be removed when NSPR newer than v4.6 is picked up,
  27. which should have the fix for bug 326110
  28. */
  29. extern "C" {
  30. int32 PR_GetSysfdTableMax(void);
  31. int32 PR_SetSysfdTableSize(int table_size);
  32. }
  33. #endif
  34. #endif
  35. #include "systems.h"
  36. #if defined (USE_NSPR)
  37. #if defined(__hpux) && defined(__ia64)
  38. #define DEFAULT_STACKSIZE (256*1024)
  39. #else
  40. #define DEFAULT_STACKSIZE (64*1024)
  41. #endif
  42. static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;
  43. NSAPI_PUBLIC
  44. void systhread_set_default_stacksize(unsigned long size)
  45. {
  46. _systhr_stacksize = size;
  47. }
  48. NSPR_BEGIN_EXTERN_C
  49. NSAPI_PUBLIC SYS_THREAD
  50. systhread_start(int prio, int stksz, void (*fn)(void *), void *arg)
  51. {
  52. #if defined(Linux) && !defined(USE_PTHREADS)
  53. prio /= 8; /* quick and dirty fix for user thread priority scale problem */
  54. if (prio > 3) prio = 3;
  55. #endif
  56. PRThread *ret = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))fn,
  57. (void *)arg, (PRThreadPriority)prio,
  58. PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  59. stksz ? stksz : _systhr_stacksize);
  60. return (void *) ret;
  61. }
  62. NSPR_END_EXTERN_C
  63. NSAPI_PUBLIC SYS_THREAD systhread_current(void)
  64. {
  65. return PR_GetCurrentThread();
  66. }
  67. NSAPI_PUBLIC void systhread_yield(void)
  68. {
  69. /* PR_Yield(); */
  70. PR_Sleep(PR_INTERVAL_NO_WAIT);
  71. }
  72. NSAPI_PUBLIC void systhread_timerset(int usec)
  73. {
  74. /* This is an interesting problem. If you ever do turn on interrupts
  75. * on the server, you're in for lots of fun with NSPR Threads
  76. PR_StartEvents(usec); */
  77. }
  78. NSAPI_PUBLIC
  79. SYS_THREAD systhread_attach(void)
  80. {
  81. PRThread *ret;
  82. ret = PR_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL);
  83. return (void *) ret;
  84. }
  85. NSAPI_PUBLIC
  86. void systhread_detach(SYS_THREAD thr)
  87. {
  88. /* XXXMB - this is not correct! */
  89. PR_DetachThread();
  90. }
  91. NSAPI_PUBLIC void systhread_terminate(SYS_THREAD thr)
  92. {
  93. /* Should never be here. PR_DestroyThread is no
  94. * longer used. */
  95. PR_ASSERT(0);
  96. /* PR_DestroyThread((PRThread *) thr); */
  97. }
  98. NSAPI_PUBLIC void systhread_sleep(int milliseconds)
  99. {
  100. PR_Sleep(milliseconds);
  101. }
  102. NSAPI_PUBLIC void systhread_init(char *name)
  103. {
  104. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
  105. #ifdef LINUX
  106. /*
  107. * NSPR 4.6 does not export PR_SetSysfdTableSize
  108. * and PR_GetSysfdTableMax by mistake (NSPR Bugzilla
  109. * bug 326110) on platforms that use GCC with symbol
  110. * visibility, so we have to call the system calls
  111. * directly.
  112. */
  113. {
  114. struct rlimit rlim;
  115. if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
  116. return;
  117. rlim.rlim_cur = rlim.rlim_max;
  118. (void) setrlimit(RLIMIT_NOFILE, &rlim);
  119. }
  120. #else
  121. PR_SetSysfdTableSize(PR_GetSysfdTableMax());
  122. #endif
  123. }
  124. NSAPI_PUBLIC int systhread_newkey()
  125. {
  126. uintn newkey;
  127. PR_NewThreadPrivateIndex(&newkey, NULL);
  128. return (newkey);
  129. }
  130. NSAPI_PUBLIC void *systhread_getdata(int key)
  131. {
  132. return PR_GetThreadPrivate(key);
  133. }
  134. NSAPI_PUBLIC void systhread_setdata(int key, void *data)
  135. {
  136. PR_SetThreadPrivate(key, data);
  137. }
  138. /*
  139. * Drag in the Java code, so our dynamic library full of it works
  140. * i.e. force these symbols to load.
  141. */
  142. NSAPI_PUBLIC void systhread_dummy(void)
  143. {
  144. }
  145. #endif