pprthred.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #ifndef pprthred_h___
  6. #define pprthred_h___
  7. /*
  8. ** API for PR private functions. These calls are to be used by internal
  9. ** developers only.
  10. */
  11. #include "nspr.h"
  12. #if defined(XP_OS2)
  13. #define INCL_DOS
  14. #define INCL_DOSERRORS
  15. #define INCL_WIN
  16. #include <os2.h>
  17. #endif
  18. PR_BEGIN_EXTERN_C
  19. /*---------------------------------------------------------------------------
  20. ** THREAD PRIVATE FUNCTIONS
  21. ---------------------------------------------------------------------------*/
  22. /*
  23. ** Associate a thread object with an existing native thread.
  24. ** "type" is the type of thread object to attach
  25. ** "priority" is the priority to assign to the thread
  26. ** "stack" defines the shape of the threads stack
  27. **
  28. ** This can return NULL if some kind of error occurs, or if memory is
  29. ** tight. This call invokes "start(obj,arg)" and returns when the
  30. ** function returns. The thread object is automatically destroyed.
  31. **
  32. ** This call is not normally needed unless you create your own native
  33. ** thread. PR_Init does this automatically for the primordial thread.
  34. */
  35. NSPR_API(PRThread*) PR_AttachThread(PRThreadType type,
  36. PRThreadPriority priority,
  37. PRThreadStack *stack);
  38. /*
  39. ** Detach the nspr thread from the currently executing native thread.
  40. ** The thread object will be destroyed and all related data attached
  41. ** to it. The exit procs will be invoked.
  42. **
  43. ** This call is not normally needed unless you create your own native
  44. ** thread. PR_Exit will automatially detach the nspr thread object
  45. ** created by PR_Init for the primordial thread.
  46. **
  47. ** This call returns after the nspr thread object is destroyed.
  48. */
  49. NSPR_API(void) PR_DetachThread(void);
  50. /*
  51. ** Get the id of the named thread. Each thread is assigned a unique id
  52. ** when it is created or attached.
  53. */
  54. NSPR_API(PRUint32) PR_GetThreadID(PRThread *thread);
  55. /*
  56. ** Set the procedure that is called when a thread is dumped. The procedure
  57. ** will be applied to the argument, arg, when called. Setting the procedure
  58. ** to NULL effectively removes it.
  59. */
  60. typedef void (*PRThreadDumpProc)(PRFileDesc *fd, PRThread *t, void *arg);
  61. NSPR_API(void) PR_SetThreadDumpProc(
  62. PRThread* thread, PRThreadDumpProc dump, void *arg);
  63. /*
  64. ** Get this thread's affinity mask. The affinity mask is a 32 bit quantity
  65. ** marking a bit for each processor this process is allowed to run on.
  66. ** The processor mask is returned in the mask argument.
  67. ** The least-significant-bit represents processor 0.
  68. **
  69. ** Returns 0 on success, -1 on failure.
  70. */
  71. NSPR_API(PRInt32) PR_GetThreadAffinityMask(PRThread *thread, PRUint32 *mask);
  72. /*
  73. ** Set this thread's affinity mask.
  74. **
  75. ** Returns 0 on success, -1 on failure.
  76. */
  77. NSPR_API(PRInt32) PR_SetThreadAffinityMask(PRThread *thread, PRUint32 mask );
  78. /*
  79. ** Set the default CPU Affinity mask.
  80. **
  81. */
  82. NSPR_API(PRInt32) PR_SetCPUAffinityMask(PRUint32 mask);
  83. /*
  84. ** Show status of all threads to standard error output.
  85. */
  86. NSPR_API(void) PR_ShowStatus(void);
  87. /*
  88. ** Set thread recycle mode to on (1) or off (0)
  89. */
  90. NSPR_API(void) PR_SetThreadRecycleMode(PRUint32 flag);
  91. /*---------------------------------------------------------------------------
  92. ** THREAD PRIVATE FUNCTIONS FOR GARBAGE COLLECTIBLE THREADS
  93. ---------------------------------------------------------------------------*/
  94. /*
  95. ** Only Garbage collectible threads participate in resume all, suspend all and
  96. ** enumeration operations. They are also different during creation when
  97. ** platform specific action may be needed (For example, all Solaris GC able
  98. ** threads are bound threads).
  99. */
  100. /*
  101. ** Same as PR_CreateThread except that the thread is marked as garbage
  102. ** collectible.
  103. */
  104. NSPR_API(PRThread*) PR_CreateThreadGCAble(PRThreadType type,
  105. void (*start)(void *arg),
  106. void *arg,
  107. PRThreadPriority priority,
  108. PRThreadScope scope,
  109. PRThreadState state,
  110. PRUint32 stackSize);
  111. /*
  112. ** Same as PR_AttachThread except that the thread being attached is marked as
  113. ** garbage collectible.
  114. */
  115. NSPR_API(PRThread*) PR_AttachThreadGCAble(PRThreadType type,
  116. PRThreadPriority priority,
  117. PRThreadStack *stack);
  118. /*
  119. ** Mark the thread as garbage collectible.
  120. */
  121. NSPR_API(void) PR_SetThreadGCAble(void);
  122. /*
  123. ** Unmark the thread as garbage collectible.
  124. */
  125. NSPR_API(void) PR_ClearThreadGCAble(void);
  126. /*
  127. ** This routine prevents all other GC able threads from running. This call is needed by
  128. ** the garbage collector.
  129. */
  130. NSPR_API(void) PR_SuspendAll(void);
  131. /*
  132. ** This routine unblocks all other GC able threads that were suspended from running by
  133. ** PR_SuspendAll(). This call is needed by the garbage collector.
  134. */
  135. NSPR_API(void) PR_ResumeAll(void);
  136. /*
  137. ** Return the thread stack pointer of the given thread.
  138. ** Needed by the garbage collector.
  139. */
  140. NSPR_API(void *) PR_GetSP(PRThread *thread);
  141. /*
  142. ** Save the registers that the GC would find interesting into the thread
  143. ** "t". isCurrent will be non-zero if the thread state that is being
  144. ** saved is the currently executing thread. Return the address of the
  145. ** first register to be scanned as well as the number of registers to
  146. ** scan in "np".
  147. **
  148. ** If "isCurrent" is non-zero then it is allowed for the thread context
  149. ** area to be used as scratch storage to hold just the registers
  150. ** necessary for scanning.
  151. **
  152. ** This function simply calls the internal function _MD_HomeGCRegisters().
  153. */
  154. NSPR_API(PRWord *) PR_GetGCRegisters(PRThread *t, int isCurrent, int *np);
  155. /*
  156. ** (Get|Set)ExecutionEnvironent
  157. **
  158. ** Used by Java to associate it's execution environment so garbage collector
  159. ** can find it. If return is NULL, then it's probably not a collectable thread.
  160. **
  161. ** There's no locking required around these calls.
  162. */
  163. NSPR_API(void*) GetExecutionEnvironment(PRThread *thread);
  164. NSPR_API(void) SetExecutionEnvironment(PRThread* thread, void *environment);
  165. /*
  166. ** Enumeration function that applies "func(thread,i,arg)" to each active
  167. ** thread in the process. The enumerator returns PR_SUCCESS if the enumeration
  168. ** should continue, any other value is considered failure, and enumeration
  169. ** stops, returning the failure value from PR_EnumerateThreads.
  170. ** Needed by the garbage collector.
  171. */
  172. typedef PRStatus (PR_CALLBACK *PREnumerator)(PRThread *t, int i, void *arg);
  173. NSPR_API(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg);
  174. /*
  175. ** Signature of a thread stack scanning function. It is applied to every
  176. ** contiguous group of potential pointers within a thread. Count denotes the
  177. ** number of pointers.
  178. */
  179. typedef PRStatus
  180. (PR_CALLBACK *PRScanStackFun)(PRThread* t,
  181. void** baseAddr, PRUword count, void* closure);
  182. /*
  183. ** Applies scanFun to all contiguous groups of potential pointers
  184. ** within a thread. This includes the stack, registers, and thread-local
  185. ** data. If scanFun returns a status value other than PR_SUCCESS the scan
  186. ** is aborted, and the status value is returned.
  187. */
  188. NSPR_API(PRStatus)
  189. PR_ThreadScanStackPointers(PRThread* t,
  190. PRScanStackFun scanFun, void* scanClosure);
  191. /*
  192. ** Calls PR_ThreadScanStackPointers for every thread.
  193. */
  194. NSPR_API(PRStatus)
  195. PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure);
  196. /*
  197. ** Returns a conservative estimate on the amount of stack space left
  198. ** on a thread in bytes, sufficient for making decisions about whether
  199. ** to continue recursing or not.
  200. */
  201. NSPR_API(PRUword)
  202. PR_GetStackSpaceLeft(PRThread* t);
  203. /*---------------------------------------------------------------------------
  204. ** THREAD CPU PRIVATE FUNCTIONS
  205. ---------------------------------------------------------------------------*/
  206. /*
  207. ** Get a pointer to the primordial CPU.
  208. */
  209. NSPR_API(struct _PRCPU *) _PR_GetPrimordialCPU(void);
  210. /*---------------------------------------------------------------------------
  211. ** THREAD SYNCHRONIZATION PRIVATE FUNCTIONS
  212. ---------------------------------------------------------------------------*/
  213. /*
  214. ** Create a new named monitor (named for debugging purposes).
  215. ** Monitors are re-entrant locks with a built-in condition variable.
  216. **
  217. ** This may fail if memory is tight or if some operating system resource
  218. ** is low.
  219. */
  220. NSPR_API(PRMonitor*) PR_NewNamedMonitor(const char* name);
  221. /*
  222. ** Test and then lock the lock if it's not already locked by some other
  223. ** thread. Return PR_FALSE if some other thread owned the lock at the
  224. ** time of the call.
  225. */
  226. NSPR_API(PRBool) PR_TestAndLock(PRLock *lock);
  227. /*
  228. ** Test and then enter the mutex associated with the monitor if it's not
  229. ** already entered by some other thread. Return PR_FALSE if some other
  230. ** thread owned the mutex at the time of the call.
  231. */
  232. NSPR_API(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon);
  233. /*
  234. ** Return the number of times that the current thread has entered the
  235. ** mutex. Returns zero if the current thread has not entered the mutex.
  236. */
  237. NSPR_API(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon);
  238. /*
  239. ** Just like PR_CEnterMonitor except that if the monitor is owned by
  240. ** another thread NULL is returned.
  241. */
  242. NSPR_API(PRMonitor*) PR_CTestAndEnterMonitor(void *address);
  243. /*---------------------------------------------------------------------------
  244. ** PLATFORM-SPECIFIC INITIALIZATION FUNCTIONS
  245. ---------------------------------------------------------------------------*/
  246. #if defined(XP_OS2)
  247. /*
  248. ** These functions need to be called at the start and end of a thread.
  249. ** An EXCEPTIONREGISTRATIONRECORD must be declared on the stack and its
  250. ** address passed to the two functions.
  251. */
  252. NSPR_API(void) PR_OS2_SetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e);
  253. NSPR_API(void) PR_OS2_UnsetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e);
  254. #endif /* XP_OS2 */
  255. /* I think PR_GetMonitorEntryCount is useless. All you really want is this... */
  256. #define PR_InMonitor(m) (PR_GetMonitorEntryCount(m) > 0)
  257. /*---------------------------------------------------------------------------
  258. ** Special X-Lock hack for client
  259. ---------------------------------------------------------------------------*/
  260. #ifdef XP_UNIX
  261. extern void PR_XLock(void);
  262. extern void PR_XUnlock(void);
  263. extern PRBool PR_XIsLocked(void);
  264. #endif /* XP_UNIX */
  265. PR_END_EXTERN_C
  266. #endif /* pprthred_h___ */