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