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