pprthred.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef pprthred_h___
  38. #define pprthred_h___
  39. /*
  40. ** API for PR private functions. These calls are to be used by internal
  41. ** developers only.
  42. */
  43. #include "nspr.h"
  44. #if defined(XP_OS2)
  45. #define INCL_DOS
  46. #define INCL_DOSERRORS
  47. #define INCL_WIN
  48. #include <os2.h>
  49. #endif
  50. PR_BEGIN_EXTERN_C
  51. /*---------------------------------------------------------------------------
  52. ** THREAD PRIVATE FUNCTIONS
  53. ---------------------------------------------------------------------------*/
  54. /*
  55. ** Associate a thread object with an existing native thread.
  56. ** "type" is the type of thread object to attach
  57. ** "priority" is the priority to assign to the thread
  58. ** "stack" defines the shape of the threads stack
  59. **
  60. ** This can return NULL if some kind of error occurs, or if memory is
  61. ** tight. This call invokes "start(obj,arg)" and returns when the
  62. ** function returns. The thread object is automatically destroyed.
  63. **
  64. ** This call is not normally needed unless you create your own native
  65. ** thread. PR_Init does this automatically for the primordial thread.
  66. */
  67. NSPR_API(PRThread*) PR_AttachThread(PRThreadType type,
  68. PRThreadPriority priority,
  69. PRThreadStack *stack);
  70. /*
  71. ** Detach the nspr thread from the currently executing native thread.
  72. ** The thread object will be destroyed and all related data attached
  73. ** to it. The exit procs will be invoked.
  74. **
  75. ** This call is not normally needed unless you create your own native
  76. ** thread. PR_Exit will automatially detach the nspr thread object
  77. ** created by PR_Init for the primordial thread.
  78. **
  79. ** This call returns after the nspr thread object is destroyed.
  80. */
  81. NSPR_API(void) PR_DetachThread(void);
  82. /*
  83. ** Get the id of the named thread. Each thread is assigned a unique id
  84. ** when it is created or attached.
  85. */
  86. NSPR_API(PRUint32) PR_GetThreadID(PRThread *thread);
  87. /*
  88. ** Set the procedure that is called when a thread is dumped. The procedure
  89. ** will be applied to the argument, arg, when called. Setting the procedure
  90. ** to NULL effectively removes it.
  91. */
  92. typedef void (*PRThreadDumpProc)(PRFileDesc *fd, PRThread *t, void *arg);
  93. NSPR_API(void) PR_SetThreadDumpProc(
  94. PRThread* thread, PRThreadDumpProc dump, void *arg);
  95. /*
  96. ** Get this thread's affinity mask. The affinity mask is a 32 bit quantity
  97. ** marking a bit for each processor this process is allowed to run on.
  98. ** The processor mask is returned in the mask argument.
  99. ** The least-significant-bit represents processor 0.
  100. **
  101. ** Returns 0 on success, -1 on failure.
  102. */
  103. NSPR_API(PRInt32) PR_GetThreadAffinityMask(PRThread *thread, PRUint32 *mask);
  104. /*
  105. ** Set this thread's affinity mask.
  106. **
  107. ** Returns 0 on success, -1 on failure.
  108. */
  109. NSPR_API(PRInt32) PR_SetThreadAffinityMask(PRThread *thread, PRUint32 mask );
  110. /*
  111. ** Set the default CPU Affinity mask.
  112. **
  113. */
  114. NSPR_API(PRInt32) PR_SetCPUAffinityMask(PRUint32 mask);
  115. /*
  116. ** Show status of all threads to standard error output.
  117. */
  118. NSPR_API(void) PR_ShowStatus(void);
  119. /*
  120. ** Set thread recycle mode to on (1) or off (0)
  121. */
  122. NSPR_API(void) PR_SetThreadRecycleMode(PRUint32 flag);
  123. /*---------------------------------------------------------------------------
  124. ** THREAD PRIVATE FUNCTIONS FOR GARBAGE COLLECTIBLE THREADS
  125. ---------------------------------------------------------------------------*/
  126. /*
  127. ** Only Garbage collectible threads participate in resume all, suspend all and
  128. ** enumeration operations. They are also different during creation when
  129. ** platform specific action may be needed (For example, all Solaris GC able
  130. ** threads are bound threads).
  131. */
  132. /*
  133. ** Same as PR_CreateThread except that the thread is marked as garbage
  134. ** collectible.
  135. */
  136. NSPR_API(PRThread*) PR_CreateThreadGCAble(PRThreadType type,
  137. void (*start)(void *arg),
  138. void *arg,
  139. PRThreadPriority priority,
  140. PRThreadScope scope,
  141. PRThreadState state,
  142. PRUint32 stackSize);
  143. /*
  144. ** Same as PR_AttachThread except that the thread being attached is marked as
  145. ** garbage collectible.
  146. */
  147. NSPR_API(PRThread*) PR_AttachThreadGCAble(PRThreadType type,
  148. PRThreadPriority priority,
  149. PRThreadStack *stack);
  150. /*
  151. ** Mark the thread as garbage collectible.
  152. */
  153. NSPR_API(void) PR_SetThreadGCAble(void);
  154. /*
  155. ** Unmark the thread as garbage collectible.
  156. */
  157. NSPR_API(void) PR_ClearThreadGCAble(void);
  158. /*
  159. ** This routine prevents all other GC able threads from running. This call is needed by
  160. ** the garbage collector.
  161. */
  162. NSPR_API(void) PR_SuspendAll(void);
  163. /*
  164. ** This routine unblocks all other GC able threads that were suspended from running by
  165. ** PR_SuspendAll(). This call is needed by the garbage collector.
  166. */
  167. NSPR_API(void) PR_ResumeAll(void);
  168. /*
  169. ** Return the thread stack pointer of the given thread.
  170. ** Needed by the garbage collector.
  171. */
  172. NSPR_API(void *) PR_GetSP(PRThread *thread);
  173. /*
  174. ** Save the registers that the GC would find interesting into the thread
  175. ** "t". isCurrent will be non-zero if the thread state that is being
  176. ** saved is the currently executing thread. Return the address of the
  177. ** first register to be scanned as well as the number of registers to
  178. ** scan in "np".
  179. **
  180. ** If "isCurrent" is non-zero then it is allowed for the thread context
  181. ** area to be used as scratch storage to hold just the registers
  182. ** necessary for scanning.
  183. **
  184. ** This function simply calls the internal function _MD_HomeGCRegisters().
  185. */
  186. NSPR_API(PRWord *) PR_GetGCRegisters(PRThread *t, int isCurrent, int *np);
  187. /*
  188. ** (Get|Set)ExecutionEnvironent
  189. **
  190. ** Used by Java to associate it's execution environment so garbage collector
  191. ** can find it. If return is NULL, then it's probably not a collectable thread.
  192. **
  193. ** There's no locking required around these calls.
  194. */
  195. NSPR_API(void*) GetExecutionEnvironment(PRThread *thread);
  196. NSPR_API(void) SetExecutionEnvironment(PRThread* thread, void *environment);
  197. /*
  198. ** Enumeration function that applies "func(thread,i,arg)" to each active
  199. ** thread in the process. The enumerator returns PR_SUCCESS if the enumeration
  200. ** should continue, any other value is considered failure, and enumeration
  201. ** stops, returning the failure value from PR_EnumerateThreads.
  202. ** Needed by the garbage collector.
  203. */
  204. typedef PRStatus (PR_CALLBACK *PREnumerator)(PRThread *t, int i, void *arg);
  205. NSPR_API(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg);
  206. /*
  207. ** Signature of a thread stack scanning function. It is applied to every
  208. ** contiguous group of potential pointers within a thread. Count denotes the
  209. ** number of pointers.
  210. */
  211. typedef PRStatus
  212. (PR_CALLBACK *PRScanStackFun)(PRThread* t,
  213. void** baseAddr, PRUword count, void* closure);
  214. /*
  215. ** Applies scanFun to all contiguous groups of potential pointers
  216. ** within a thread. This includes the stack, registers, and thread-local
  217. ** data. If scanFun returns a status value other than PR_SUCCESS the scan
  218. ** is aborted, and the status value is returned.
  219. */
  220. NSPR_API(PRStatus)
  221. PR_ThreadScanStackPointers(PRThread* t,
  222. PRScanStackFun scanFun, void* scanClosure);
  223. /*
  224. ** Calls PR_ThreadScanStackPointers for every thread.
  225. */
  226. NSPR_API(PRStatus)
  227. PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure);
  228. /*
  229. ** Returns a conservative estimate on the amount of stack space left
  230. ** on a thread in bytes, sufficient for making decisions about whether
  231. ** to continue recursing or not.
  232. */
  233. NSPR_API(PRUword)
  234. PR_GetStackSpaceLeft(PRThread* t);
  235. /*---------------------------------------------------------------------------
  236. ** THREAD CPU PRIVATE FUNCTIONS
  237. ---------------------------------------------------------------------------*/
  238. /*
  239. ** Get a pointer to the primordial CPU.
  240. */
  241. NSPR_API(struct _PRCPU *) _PR_GetPrimordialCPU(void);
  242. /*---------------------------------------------------------------------------
  243. ** THREAD SYNCHRONIZATION PRIVATE FUNCTIONS
  244. ---------------------------------------------------------------------------*/
  245. /*
  246. ** Create a new named monitor (named for debugging purposes).
  247. ** Monitors are re-entrant locks with a built-in condition variable.
  248. **
  249. ** This may fail if memory is tight or if some operating system resource
  250. ** is low.
  251. */
  252. NSPR_API(PRMonitor*) PR_NewNamedMonitor(const char* name);
  253. /*
  254. ** Test and then lock the lock if it's not already locked by some other
  255. ** thread. Return PR_FALSE if some other thread owned the lock at the
  256. ** time of the call.
  257. */
  258. NSPR_API(PRBool) PR_TestAndLock(PRLock *lock);
  259. /*
  260. ** Test and then enter the mutex associated with the monitor if it's not
  261. ** already entered by some other thread. Return PR_FALSE if some other
  262. ** thread owned the mutex at the time of the call.
  263. */
  264. NSPR_API(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon);
  265. /*
  266. ** Return the number of times that the current thread has entered the
  267. ** mutex. Returns zero if the current thread has not entered the mutex.
  268. */
  269. NSPR_API(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon);
  270. /*
  271. ** Just like PR_CEnterMonitor except that if the monitor is owned by
  272. ** another thread NULL is returned.
  273. */
  274. NSPR_API(PRMonitor*) PR_CTestAndEnterMonitor(void *address);
  275. /*---------------------------------------------------------------------------
  276. ** PLATFORM-SPECIFIC THREAD SYNCHRONIZATION FUNCTIONS
  277. ---------------------------------------------------------------------------*/
  278. #if defined(XP_MAC)
  279. NSPR_API(void) PR_Mac_WaitForAsyncNotify(PRIntervalTime timeout);
  280. NSPR_API(void) PR_Mac_PostAsyncNotify(PRThread *thread);
  281. #endif /* XP_MAC */
  282. /*---------------------------------------------------------------------------
  283. ** PLATFORM-SPECIFIC INITIALIZATION FUNCTIONS
  284. ---------------------------------------------------------------------------*/
  285. #if defined(IRIX)
  286. /*
  287. ** Irix specific initialization funtion to be called before PR_Init
  288. ** is called by the application. Sets the CONF_INITUSERS and CONF_INITSIZE
  289. ** attributes of the shared arena set up by nspr.
  290. **
  291. ** The environment variables _NSPR_IRIX_INITUSERS and _NSPR_IRIX_INITSIZE
  292. ** can also be used to set these arena attributes. If _NSPR_IRIX_INITUSERS
  293. ** is set, but not _NSPR_IRIX_INITSIZE, the value of the CONF_INITSIZE
  294. ** attribute of the nspr arena is scaled as a function of the
  295. ** _NSPR_IRIX_INITUSERS value.
  296. **
  297. ** If the _PR_Irix_Set_Arena_Params() is called in addition to setting the
  298. ** environment variables, the values of the environment variables are used.
  299. **
  300. */
  301. NSPR_API(void) _PR_Irix_Set_Arena_Params(PRInt32 initusers, PRInt32 initsize);
  302. #endif /* IRIX */
  303. #if defined(XP_OS2)
  304. /*
  305. ** These functions need to be called at the start and end of a thread.
  306. ** An EXCEPTIONREGISTRATIONRECORD must be declared on the stack and its
  307. ** address passed to the two functions.
  308. */
  309. NSPR_API(void) PR_OS2_SetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e);
  310. NSPR_API(void) PR_OS2_UnsetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e);
  311. #endif /* XP_OS2 */
  312. /* I think PR_GetMonitorEntryCount is useless. All you really want is this... */
  313. #define PR_InMonitor(m) (PR_GetMonitorEntryCount(m) > 0)
  314. /*---------------------------------------------------------------------------
  315. ** Special X-Lock hack for client
  316. ---------------------------------------------------------------------------*/
  317. #ifdef XP_UNIX
  318. extern void PR_XLock(void);
  319. extern void PR_XUnlock(void);
  320. extern PRBool PR_XIsLocked(void);
  321. #endif /* XP_UNIX */
  322. PR_END_EXTERN_C
  323. #endif /* pprthred_h___ */