prthread.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 prthread_h___
  38. #define prthread_h___
  39. /*
  40. ** API for NSPR threads. On some architectures (MAC and WIN16
  41. ** notably) pre-emptibility is not guaranteed. Hard priority scheduling
  42. ** is not guaranteed, so programming using priority based synchronization
  43. ** is a no-no.
  44. **
  45. ** NSPR threads are scheduled based loosely on their client set priority.
  46. ** In general, a thread of a higher priority has a statistically better
  47. ** chance of running relative to threads of lower priority. However,
  48. ** NSPR uses multiple strategies to provide execution vehicles for thread
  49. ** abstraction of various host platforms. As it turns out, there is little
  50. ** NSPR can do to affect the scheduling attributes of "GLOBAL" threads.
  51. ** However, a semblance of GLOBAL threads is used to implement "LOCAL"
  52. ** threads. An arbitrary number of such LOCAL threads can be assigned to
  53. ** a single GLOBAL thread.
  54. **
  55. ** For scheduling, NSPR will attempt to run the highest priority LOCAL
  56. ** thread associated with a given GLOBAL thread. It is further assumed
  57. ** that the host OS will apply some form of "fair" scheduling on the
  58. ** GLOBAL threads.
  59. **
  60. ** Threads have a "system flag" which when set indicates the thread
  61. ** doesn't count for determining when the process should exit (the
  62. ** process exits when the last user thread exits).
  63. **
  64. ** Threads also have a "scope flag" which controls whether the threads
  65. ** are scheduled in the local scope or scheduled by the OS globally. This
  66. ** indicates whether a thread is permanently bound to a native OS thread.
  67. ** An unbound thread competes for scheduling resources in the same process.
  68. **
  69. ** Another flag is "state flag" which control whether the thread is joinable.
  70. ** It allows other threads to wait for the created thread to reach completion.
  71. **
  72. ** Threads can have "per-thread-data" attached to them. Each thread has a
  73. ** per-thread error number and error string which are updated when NSPR
  74. ** operations fail.
  75. */
  76. #include "prtypes.h"
  77. #include "prinrval.h"
  78. PR_BEGIN_EXTERN_C
  79. typedef struct PRThread PRThread;
  80. typedef struct PRThreadStack PRThreadStack;
  81. typedef enum PRThreadType {
  82. PR_USER_THREAD,
  83. PR_SYSTEM_THREAD
  84. } PRThreadType;
  85. typedef enum PRThreadScope {
  86. PR_LOCAL_THREAD,
  87. PR_GLOBAL_THREAD,
  88. PR_GLOBAL_BOUND_THREAD
  89. } PRThreadScope;
  90. typedef enum PRThreadState {
  91. PR_JOINABLE_THREAD,
  92. PR_UNJOINABLE_THREAD
  93. } PRThreadState;
  94. typedef enum PRThreadPriority
  95. {
  96. PR_PRIORITY_FIRST = 0, /* just a placeholder */
  97. PR_PRIORITY_LOW = 0, /* the lowest possible priority */
  98. PR_PRIORITY_NORMAL = 1, /* most common expected priority */
  99. PR_PRIORITY_HIGH = 2, /* slightly more aggressive scheduling */
  100. PR_PRIORITY_URGENT = 3, /* it does little good to have more than one */
  101. PR_PRIORITY_LAST = 3 /* this is just a placeholder */
  102. } PRThreadPriority;
  103. /*
  104. ** Create a new thread:
  105. ** "type" is the type of thread to create
  106. ** "start(arg)" will be invoked as the threads "main"
  107. ** "priority" will be created thread's priority
  108. ** "scope" will specify whether the thread is local or global
  109. ** "state" will specify whether the thread is joinable or not
  110. ** "stackSize" the size of the stack, in bytes. The value can be zero
  111. ** and then a machine specific stack size will be chosen.
  112. **
  113. ** This can return NULL if some kind of error occurs, such as if memory is
  114. ** tight.
  115. **
  116. ** If you want the thread to start up waiting for the creator to do
  117. ** something, enter a lock before creating the thread and then have the
  118. ** threads start routine enter and exit the same lock. When you are ready
  119. ** for the thread to run, exit the lock.
  120. **
  121. ** If you want to detect the completion of the created thread, the thread
  122. ** should be created joinable. Then, use PR_JoinThread to synchrnoize the
  123. ** termination of another thread.
  124. **
  125. ** When the start function returns the thread exits. If it is the last
  126. ** PR_USER_THREAD to exit then the process exits.
  127. */
  128. NSPR_API(PRThread*) PR_CreateThread(PRThreadType type,
  129. void (PR_CALLBACK *start)(void *arg),
  130. void *arg,
  131. PRThreadPriority priority,
  132. PRThreadScope scope,
  133. PRThreadState state,
  134. PRUint32 stackSize);
  135. /*
  136. ** Wait for thread termination:
  137. ** "thread" is the target thread
  138. **
  139. ** This can return PR_FAILURE if no joinable thread could be found
  140. ** corresponding to the specified target thread.
  141. **
  142. ** The calling thread is blocked until the target thread completes.
  143. ** Several threads cannot wait for the same thread to complete; one thread
  144. ** will operate successfully and others will terminate with an error PR_FAILURE.
  145. ** The calling thread will not be blocked if the target thread has already
  146. ** terminated.
  147. */
  148. NSPR_API(PRStatus) PR_JoinThread(PRThread *thread);
  149. /*
  150. ** Return the current thread object for the currently running code.
  151. ** Never returns NULL.
  152. */
  153. NSPR_API(PRThread*) PR_GetCurrentThread(void);
  154. #ifndef NO_NSPR_10_SUPPORT
  155. #define PR_CurrentThread() PR_GetCurrentThread() /* for nspr1.0 compat. */
  156. #endif /* NO_NSPR_10_SUPPORT */
  157. /*
  158. ** Get the priority of "thread".
  159. */
  160. NSPR_API(PRThreadPriority) PR_GetThreadPriority(const PRThread *thread);
  161. /*
  162. ** Change the priority of the "thread" to "priority".
  163. */
  164. NSPR_API(void) PR_SetThreadPriority(PRThread *thread, PRThreadPriority priority);
  165. /*
  166. ** This routine returns a new index for per-thread-private data table.
  167. ** The index is visible to all threads within a process. This index can
  168. ** be used with the PR_SetThreadPrivate() and PR_GetThreadPrivate() routines
  169. ** to save and retrieve data associated with the index for a thread.
  170. **
  171. ** Each index is associationed with a destructor function ('dtor'). The function
  172. ** may be specified as NULL when the index is created. If it is not NULL, the
  173. ** function will be called when:
  174. ** - the thread exits and the private data for the associated index
  175. ** is not NULL,
  176. ** - new thread private data is set and the current private data is
  177. ** not NULL.
  178. **
  179. ** The index independently maintains specific values for each binding thread.
  180. ** A thread can only get access to its own thread-specific-data.
  181. **
  182. ** Upon a new index return the value associated with the index for all threads
  183. ** is NULL, and upon thread creation the value associated with all indices for
  184. ** that thread is NULL.
  185. **
  186. ** Returns PR_FAILURE if the total number of indices will exceed the maximun
  187. ** allowed.
  188. */
  189. typedef void (PR_CALLBACK *PRThreadPrivateDTOR)(void *priv);
  190. NSPR_API(PRStatus) PR_NewThreadPrivateIndex(
  191. PRUintn *newIndex, PRThreadPrivateDTOR destructor);
  192. /*
  193. ** Define some per-thread-private data.
  194. ** "tpdIndex" is an index into the per-thread private data table
  195. ** "priv" is the per-thread-private data
  196. **
  197. ** If the per-thread private data table has a previously registered
  198. ** destructor function and a non-NULL per-thread-private data value,
  199. ** the destructor function is invoked.
  200. **
  201. ** This can return PR_FAILURE if the index is invalid.
  202. */
  203. NSPR_API(PRStatus) PR_SetThreadPrivate(PRUintn tpdIndex, void *priv);
  204. /*
  205. ** Recover the per-thread-private data for the current thread. "tpdIndex" is
  206. ** the index into the per-thread private data table.
  207. **
  208. ** The returned value may be NULL which is indistinguishable from an error
  209. ** condition.
  210. **
  211. ** A thread can only get access to its own thread-specific-data.
  212. */
  213. NSPR_API(void*) PR_GetThreadPrivate(PRUintn tpdIndex);
  214. /*
  215. ** This routine sets the interrupt request for a target thread. The interrupt
  216. ** request remains in the thread's state until it is delivered exactly once
  217. ** or explicitly canceled.
  218. **
  219. ** A thread that has been interrupted will fail all NSPR blocking operations
  220. ** that return a PRStatus (I/O, waiting on a condition, etc).
  221. **
  222. ** PR_Interrupt may itself fail if the target thread is invalid.
  223. */
  224. NSPR_API(PRStatus) PR_Interrupt(PRThread *thread);
  225. /*
  226. ** Clear the interrupt request for the calling thread. If no such request
  227. ** is pending, this operation is a noop.
  228. */
  229. NSPR_API(void) PR_ClearInterrupt(void);
  230. /*
  231. ** Block the interrupt for the calling thread.
  232. */
  233. NSPR_API(void) PR_BlockInterrupt(void);
  234. /*
  235. ** Unblock the interrupt for the calling thread.
  236. */
  237. NSPR_API(void) PR_UnblockInterrupt(void);
  238. /*
  239. ** Make the current thread sleep until "ticks" time amount of time
  240. ** has expired. If "ticks" is PR_INTERVAL_NO_WAIT then the call is
  241. ** equivalent to calling PR_Yield. Calling PR_Sleep with an argument
  242. ** equivalent to PR_INTERVAL_NO_TIMEOUT is an error and will result
  243. ** in a PR_FAILURE error return.
  244. */
  245. NSPR_API(PRStatus) PR_Sleep(PRIntervalTime ticks);
  246. /*
  247. ** Get the scoping of this thread.
  248. */
  249. NSPR_API(PRThreadScope) PR_GetThreadScope(const PRThread *thread);
  250. /*
  251. ** Get the type of this thread.
  252. */
  253. NSPR_API(PRThreadType) PR_GetThreadType(const PRThread *thread);
  254. /*
  255. ** Get the join state of this thread.
  256. */
  257. NSPR_API(PRThreadState) PR_GetThreadState(const PRThread *thread);
  258. PR_END_EXTERN_C
  259. #endif /* prthread_h___ */