1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 
   3  * The contents of this file are subject to the Mozilla Public 
   4  * License Version 1.1 (the "License"); you may not use this file 
   5  * except in compliance with the License. You may obtain a copy of 
   6  * the License at http://www.mozilla.org/MPL/ 
   8  * Software distributed under the License is distributed on an "AS 
   9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 
  10  * implied. See the License for the specific language governing 
  11  * rights and limitations under the License. 
  13  * The Original Code is the Netscape Portable Runtime (NSPR). 
  15  * The Initial Developer of the Original Code is Netscape 
  16  * Communications Corporation.  Portions created by Netscape are  
  17  * Copyright (C) 1998-2000 Netscape Communications Corporation.  All 
  22  * Alternatively, the contents of this file may be used under the 
  23  * terms of the GNU General Public License Version 2 or later (the 
  24  * "GPL"), in which case the provisions of the GPL are applicable  
  25  * instead of those above.  If you wish to allow use of your  
  26  * version of this file only under the terms of the GPL and not to 
  27  * allow others to use your version of this file under the MPL, 
  28  * indicate your decision by deleting the provisions above and 
  29  * replace them with the notice and other provisions required by 
  30  * the GPL.  If you do not delete the provisions above, a recipient 
  31  * may use your version of this file under either the MPL or the 
  39 ** API for NSPR threads. On some architectures (MAC and WIN16 
  40 ** notably) pre-emptibility is not guaranteed. Hard priority scheduling 
  41 ** is not guaranteed, so programming using priority based synchronization 
  44 ** NSPR threads are scheduled based loosly on their client set priority. 
  45 ** In general, a thread of a higher priority has a statistically better 
  46 ** chance of running relative to threads of lower priority. However, 
  47 ** NSPR uses multiple strategies to provide execution vehicles for thread 
  48 ** abstraction of various host platforms. As it turns out, there is little 
  49 ** NSPR can do to affect the scheduling attributes of "GLOBAL" threads. 
  50 ** However, a semblance of GLOBAL threads is used to implement "LOCAL" 
  51 ** threads. An arbitrary number of such LOCAL threads can be assigned to 
  52 ** a single GLOBAL thread. 
  54 ** For scheduling, NSPR will attempt to run the highest priority LOCAL 
  55 ** thread associated with a given GLOBAL thread. It is further assumed 
  56 ** that the host OS will apply some form of "fair" scheduling on the 
  59 ** Threads have a "system flag" which when set indicates the thread 
  60 ** doesn't count for determining when the process should exit (the 
  61 ** process exits when the last user thread exits). 
  63 ** Threads also have a "scope flag" which controls whether the threads 
  64 ** are scheduled in the local scope or scheduled by the OS globally. This  
  65 ** indicates whether a thread is permanently bound to a native OS thread.  
  66 ** An unbound thread competes for scheduling resources in the same process. 
  68 ** Another flag is "state flag" which control whether the thread is joinable. 
  69 ** It allows other threads to wait for the created thread to reach completion. 
  71 ** Threads can have "per-thread-data" attached to them. Each thread has a 
  72 ** per-thread error number and error string which are updated when NSPR 
  80 typedef struct PRThread PRThread
; 
  81 typedef struct PRThreadStack PRThreadStack
; 
  83 typedef enum PRThreadType 
{ 
  88 typedef enum PRThreadScope 
{ 
  91     PR_GLOBAL_BOUND_THREAD
 
  94 typedef enum PRThreadState 
{ 
  99 typedef enum PRThreadPriority
 
 101     PR_PRIORITY_FIRST 
= 0,      /* just a placeholder */ 
 102     PR_PRIORITY_LOW 
= 0,        /* the lowest possible priority */ 
 103     PR_PRIORITY_NORMAL 
= 1,     /* most common expected priority */ 
 104     PR_PRIORITY_HIGH 
= 2,       /* slightly more aggressive scheduling */ 
 105     PR_PRIORITY_URGENT 
= 3,     /* it does little good to have more than one */ 
 106     PR_PRIORITY_LAST 
= 3        /* this is just a placeholder */ 
 110 ** Create a new thread: 
 111 **     "type" is the type of thread to create 
 112 **     "start(arg)" will be invoked as the threads "main" 
 113 **     "priority" will be created thread's priority 
 114 **     "scope" will specify whether the thread is local or global 
 115 **     "state" will specify whether the thread is joinable or not 
 116 **     "stackSize" the size of the stack, in bytes. The value can be zero 
 117 **        and then a machine specific stack size will be chosen. 
 119 ** This can return NULL if some kind of error occurs, such as if memory is 
 122 ** If you want the thread to start up waiting for the creator to do 
 123 ** something, enter a lock before creating the thread and then have the 
 124 ** threads start routine enter and exit the same lock. When you are ready 
 125 ** for the thread to run, exit the lock. 
 127 ** If you want to detect the completion of the created thread, the thread 
 128 ** should be created joinable.  Then, use PR_JoinThread to synchrnoize the 
 129 ** termination of another thread. 
 131 ** When the start function returns the thread exits. If it is the last 
 132 ** PR_USER_THREAD to exit then the process exits. 
 134 NSPR_API(PRThread
*) PR_CreateThread(PRThreadType type
, 
 135                      void (PR_CALLBACK 
*start
)(void *arg
), 
 137                      PRThreadPriority priority
, 
 143 ** Wait for thread termination: 
 144 **     "thread" is the target thread  
 146 ** This can return PR_FAILURE if no joinable thread could be found  
 147 ** corresponding to the specified target thread. 
 149 ** The calling thread is blocked until the target thread completes. 
 150 ** Several threads cannot wait for the same thread to complete; one thread 
 151 ** will operate successfully and others will terminate with an error PR_FAILURE. 
 152 ** The calling thread will not be blocked if the target thread has already 
 155 NSPR_API(PRStatus
) PR_JoinThread(PRThread 
*thread
); 
 158 ** Return the current thread object for the currently running code. 
 159 ** Never returns NULL. 
 161 NSPR_API(PRThread
*) PR_GetCurrentThread(void); 
 162 #ifndef NO_NSPR_10_SUPPORT 
 163 #define PR_CurrentThread() PR_GetCurrentThread() /* for nspr1.0 compat. */ 
 164 #endif /* NO_NSPR_10_SUPPORT */ 
 167 ** Get the priority of "thread". 
 169 NSPR_API(PRThreadPriority
) PR_GetThreadPriority(const PRThread 
*thread
); 
 172 ** Change the priority of the "thread" to "priority". 
 174 NSPR_API(void) PR_SetThreadPriority(PRThread 
*thread
, PRThreadPriority priority
); 
 177 ** This routine returns a new index for per-thread-private data table.  
 178 ** The index is visible to all threads within a process. This index can  
 179 ** be used with the PR_SetThreadPrivate() and PR_GetThreadPrivate() routines  
 180 ** to save and retrieve data associated with the index for a thread. 
 182 ** Each index is associationed with a destructor function ('dtor'). The function 
 183 ** may be specified as NULL when the index is created. If it is not NULL, the 
 184 ** function will be called when: 
 185 **      - the thread exits and the private data for the associated index 
 187 **      - new thread private data is set and the current private data is 
 190 ** The index independently maintains specific values for each binding thread.  
 191 ** A thread can only get access to its own thread-specific-data. 
 193 ** Upon a new index return the value associated with the index for all threads 
 194 ** is NULL, and upon thread creation the value associated with all indices for  
 195 ** that thread is NULL.  
 197 ** Returns PR_FAILURE if the total number of indices will exceed the maximun  
 200 typedef void (PR_CALLBACK 
*PRThreadPrivateDTOR
)(void *priv
); 
 202 NSPR_API(PRStatus
) PR_NewThreadPrivateIndex( 
 203     PRUintn 
*newIndex
, PRThreadPrivateDTOR destructor
); 
 206 ** Define some per-thread-private data. 
 207 **     "tpdIndex" is an index into the per-thread private data table 
 208 **     "priv" is the per-thread-private data  
 210 ** If the per-thread private data table has a previously registered 
 211 ** destructor function and a non-NULL per-thread-private data value, 
 212 ** the destructor function is invoked. 
 214 ** This can return PR_FAILURE if the index is invalid. 
 216 NSPR_API(PRStatus
) PR_SetThreadPrivate(PRUintn tpdIndex
, void *priv
); 
 219 ** Recover the per-thread-private data for the current thread. "tpdIndex" is 
 220 ** the index into the per-thread private data table.  
 222 ** The returned value may be NULL which is indistinguishable from an error  
 225 ** A thread can only get access to its own thread-specific-data. 
 227 NSPR_API(void*) PR_GetThreadPrivate(PRUintn tpdIndex
); 
 230 ** This routine sets the interrupt request for a target thread. The interrupt 
 231 ** request remains in the thread's state until it is delivered exactly once 
 232 ** or explicitly canceled. 
 234 ** A thread that has been interrupted will fail all NSPR blocking operations 
 235 ** that return a PRStatus (I/O, waiting on a condition, etc). 
 237 ** PR_Interrupt may itself fail if the target thread is invalid. 
 239 NSPR_API(PRStatus
) PR_Interrupt(PRThread 
*thread
); 
 242 ** Clear the interrupt request for the calling thread. If no such request 
 243 ** is pending, this operation is a noop. 
 245 NSPR_API(void) PR_ClearInterrupt(void); 
 248 ** Block the interrupt for the calling thread. 
 250 NSPR_API(void) PR_BlockInterrupt(void); 
 253 ** Unblock the interrupt for the calling thread. 
 255 NSPR_API(void) PR_UnblockInterrupt(void); 
 258 ** Make the current thread sleep until "ticks" time amount of time 
 259 ** has expired. If "ticks" is PR_INTERVAL_NO_WAIT then the call is 
 260 ** equivalent to calling PR_Yield. Calling PR_Sleep with an argument 
 261 ** equivalent to PR_INTERVAL_NO_TIMEOUT is an error and will result 
 262 ** in a PR_FAILURE error return. 
 264 NSPR_API(PRStatus
) PR_Sleep(PRIntervalTime ticks
); 
 267 ** Get the scoping of this thread. 
 269 NSPR_API(PRThreadScope
) PR_GetThreadScope(const PRThread 
*thread
); 
 272 ** Get the type of this thread. 
 274 NSPR_API(PRThreadType
) PR_GetThreadType(const PRThread 
*thread
); 
 277 ** Get the join state of this thread. 
 279 NSPR_API(PRThreadState
) PR_GetThreadState(const PRThread 
*thread
); 
 283 #endif /* prthread_h___ */