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