Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 pprthred_h___ |
michael@0 | 7 | #define pprthred_h___ |
michael@0 | 8 | |
michael@0 | 9 | /* |
michael@0 | 10 | ** API for PR private functions. These calls are to be used by internal |
michael@0 | 11 | ** developers only. |
michael@0 | 12 | */ |
michael@0 | 13 | #include "nspr.h" |
michael@0 | 14 | |
michael@0 | 15 | #if defined(XP_OS2) |
michael@0 | 16 | #define INCL_DOS |
michael@0 | 17 | #define INCL_DOSERRORS |
michael@0 | 18 | #define INCL_WIN |
michael@0 | 19 | #include <os2.h> |
michael@0 | 20 | #endif |
michael@0 | 21 | |
michael@0 | 22 | PR_BEGIN_EXTERN_C |
michael@0 | 23 | |
michael@0 | 24 | /*--------------------------------------------------------------------------- |
michael@0 | 25 | ** THREAD PRIVATE FUNCTIONS |
michael@0 | 26 | ---------------------------------------------------------------------------*/ |
michael@0 | 27 | |
michael@0 | 28 | /* |
michael@0 | 29 | ** Associate a thread object with an existing native thread. |
michael@0 | 30 | ** "type" is the type of thread object to attach |
michael@0 | 31 | ** "priority" is the priority to assign to the thread |
michael@0 | 32 | ** "stack" defines the shape of the threads stack |
michael@0 | 33 | ** |
michael@0 | 34 | ** This can return NULL if some kind of error occurs, or if memory is |
michael@0 | 35 | ** tight. This call invokes "start(obj,arg)" and returns when the |
michael@0 | 36 | ** function returns. The thread object is automatically destroyed. |
michael@0 | 37 | ** |
michael@0 | 38 | ** This call is not normally needed unless you create your own native |
michael@0 | 39 | ** thread. PR_Init does this automatically for the primordial thread. |
michael@0 | 40 | */ |
michael@0 | 41 | NSPR_API(PRThread*) PR_AttachThread(PRThreadType type, |
michael@0 | 42 | PRThreadPriority priority, |
michael@0 | 43 | PRThreadStack *stack); |
michael@0 | 44 | |
michael@0 | 45 | /* |
michael@0 | 46 | ** Detach the nspr thread from the currently executing native thread. |
michael@0 | 47 | ** The thread object will be destroyed and all related data attached |
michael@0 | 48 | ** to it. The exit procs will be invoked. |
michael@0 | 49 | ** |
michael@0 | 50 | ** This call is not normally needed unless you create your own native |
michael@0 | 51 | ** thread. PR_Exit will automatially detach the nspr thread object |
michael@0 | 52 | ** created by PR_Init for the primordial thread. |
michael@0 | 53 | ** |
michael@0 | 54 | ** This call returns after the nspr thread object is destroyed. |
michael@0 | 55 | */ |
michael@0 | 56 | NSPR_API(void) PR_DetachThread(void); |
michael@0 | 57 | |
michael@0 | 58 | /* |
michael@0 | 59 | ** Get the id of the named thread. Each thread is assigned a unique id |
michael@0 | 60 | ** when it is created or attached. |
michael@0 | 61 | */ |
michael@0 | 62 | NSPR_API(PRUint32) PR_GetThreadID(PRThread *thread); |
michael@0 | 63 | |
michael@0 | 64 | /* |
michael@0 | 65 | ** Set the procedure that is called when a thread is dumped. The procedure |
michael@0 | 66 | ** will be applied to the argument, arg, when called. Setting the procedure |
michael@0 | 67 | ** to NULL effectively removes it. |
michael@0 | 68 | */ |
michael@0 | 69 | typedef void (*PRThreadDumpProc)(PRFileDesc *fd, PRThread *t, void *arg); |
michael@0 | 70 | NSPR_API(void) PR_SetThreadDumpProc( |
michael@0 | 71 | PRThread* thread, PRThreadDumpProc dump, void *arg); |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | ** Get this thread's affinity mask. The affinity mask is a 32 bit quantity |
michael@0 | 75 | ** marking a bit for each processor this process is allowed to run on. |
michael@0 | 76 | ** The processor mask is returned in the mask argument. |
michael@0 | 77 | ** The least-significant-bit represents processor 0. |
michael@0 | 78 | ** |
michael@0 | 79 | ** Returns 0 on success, -1 on failure. |
michael@0 | 80 | */ |
michael@0 | 81 | NSPR_API(PRInt32) PR_GetThreadAffinityMask(PRThread *thread, PRUint32 *mask); |
michael@0 | 82 | |
michael@0 | 83 | /* |
michael@0 | 84 | ** Set this thread's affinity mask. |
michael@0 | 85 | ** |
michael@0 | 86 | ** Returns 0 on success, -1 on failure. |
michael@0 | 87 | */ |
michael@0 | 88 | NSPR_API(PRInt32) PR_SetThreadAffinityMask(PRThread *thread, PRUint32 mask ); |
michael@0 | 89 | |
michael@0 | 90 | /* |
michael@0 | 91 | ** Set the default CPU Affinity mask. |
michael@0 | 92 | ** |
michael@0 | 93 | */ |
michael@0 | 94 | NSPR_API(PRInt32) PR_SetCPUAffinityMask(PRUint32 mask); |
michael@0 | 95 | |
michael@0 | 96 | /* |
michael@0 | 97 | ** Show status of all threads to standard error output. |
michael@0 | 98 | */ |
michael@0 | 99 | NSPR_API(void) PR_ShowStatus(void); |
michael@0 | 100 | |
michael@0 | 101 | /* |
michael@0 | 102 | ** Set thread recycle mode to on (1) or off (0) |
michael@0 | 103 | */ |
michael@0 | 104 | NSPR_API(void) PR_SetThreadRecycleMode(PRUint32 flag); |
michael@0 | 105 | |
michael@0 | 106 | |
michael@0 | 107 | /*--------------------------------------------------------------------------- |
michael@0 | 108 | ** THREAD PRIVATE FUNCTIONS FOR GARBAGE COLLECTIBLE THREADS |
michael@0 | 109 | ---------------------------------------------------------------------------*/ |
michael@0 | 110 | |
michael@0 | 111 | /* |
michael@0 | 112 | ** Only Garbage collectible threads participate in resume all, suspend all and |
michael@0 | 113 | ** enumeration operations. They are also different during creation when |
michael@0 | 114 | ** platform specific action may be needed (For example, all Solaris GC able |
michael@0 | 115 | ** threads are bound threads). |
michael@0 | 116 | */ |
michael@0 | 117 | |
michael@0 | 118 | /* |
michael@0 | 119 | ** Same as PR_CreateThread except that the thread is marked as garbage |
michael@0 | 120 | ** collectible. |
michael@0 | 121 | */ |
michael@0 | 122 | NSPR_API(PRThread*) PR_CreateThreadGCAble(PRThreadType type, |
michael@0 | 123 | void (*start)(void *arg), |
michael@0 | 124 | void *arg, |
michael@0 | 125 | PRThreadPriority priority, |
michael@0 | 126 | PRThreadScope scope, |
michael@0 | 127 | PRThreadState state, |
michael@0 | 128 | PRUint32 stackSize); |
michael@0 | 129 | |
michael@0 | 130 | /* |
michael@0 | 131 | ** Same as PR_AttachThread except that the thread being attached is marked as |
michael@0 | 132 | ** garbage collectible. |
michael@0 | 133 | */ |
michael@0 | 134 | NSPR_API(PRThread*) PR_AttachThreadGCAble(PRThreadType type, |
michael@0 | 135 | PRThreadPriority priority, |
michael@0 | 136 | PRThreadStack *stack); |
michael@0 | 137 | |
michael@0 | 138 | /* |
michael@0 | 139 | ** Mark the thread as garbage collectible. |
michael@0 | 140 | */ |
michael@0 | 141 | NSPR_API(void) PR_SetThreadGCAble(void); |
michael@0 | 142 | |
michael@0 | 143 | /* |
michael@0 | 144 | ** Unmark the thread as garbage collectible. |
michael@0 | 145 | */ |
michael@0 | 146 | NSPR_API(void) PR_ClearThreadGCAble(void); |
michael@0 | 147 | |
michael@0 | 148 | /* |
michael@0 | 149 | ** This routine prevents all other GC able threads from running. This call is needed by |
michael@0 | 150 | ** the garbage collector. |
michael@0 | 151 | */ |
michael@0 | 152 | NSPR_API(void) PR_SuspendAll(void); |
michael@0 | 153 | |
michael@0 | 154 | /* |
michael@0 | 155 | ** This routine unblocks all other GC able threads that were suspended from running by |
michael@0 | 156 | ** PR_SuspendAll(). This call is needed by the garbage collector. |
michael@0 | 157 | */ |
michael@0 | 158 | NSPR_API(void) PR_ResumeAll(void); |
michael@0 | 159 | |
michael@0 | 160 | /* |
michael@0 | 161 | ** Return the thread stack pointer of the given thread. |
michael@0 | 162 | ** Needed by the garbage collector. |
michael@0 | 163 | */ |
michael@0 | 164 | NSPR_API(void *) PR_GetSP(PRThread *thread); |
michael@0 | 165 | |
michael@0 | 166 | /* |
michael@0 | 167 | ** Save the registers that the GC would find interesting into the thread |
michael@0 | 168 | ** "t". isCurrent will be non-zero if the thread state that is being |
michael@0 | 169 | ** saved is the currently executing thread. Return the address of the |
michael@0 | 170 | ** first register to be scanned as well as the number of registers to |
michael@0 | 171 | ** scan in "np". |
michael@0 | 172 | ** |
michael@0 | 173 | ** If "isCurrent" is non-zero then it is allowed for the thread context |
michael@0 | 174 | ** area to be used as scratch storage to hold just the registers |
michael@0 | 175 | ** necessary for scanning. |
michael@0 | 176 | ** |
michael@0 | 177 | ** This function simply calls the internal function _MD_HomeGCRegisters(). |
michael@0 | 178 | */ |
michael@0 | 179 | NSPR_API(PRWord *) PR_GetGCRegisters(PRThread *t, int isCurrent, int *np); |
michael@0 | 180 | |
michael@0 | 181 | /* |
michael@0 | 182 | ** (Get|Set)ExecutionEnvironent |
michael@0 | 183 | ** |
michael@0 | 184 | ** Used by Java to associate it's execution environment so garbage collector |
michael@0 | 185 | ** can find it. If return is NULL, then it's probably not a collectable thread. |
michael@0 | 186 | ** |
michael@0 | 187 | ** There's no locking required around these calls. |
michael@0 | 188 | */ |
michael@0 | 189 | NSPR_API(void*) GetExecutionEnvironment(PRThread *thread); |
michael@0 | 190 | NSPR_API(void) SetExecutionEnvironment(PRThread* thread, void *environment); |
michael@0 | 191 | |
michael@0 | 192 | /* |
michael@0 | 193 | ** Enumeration function that applies "func(thread,i,arg)" to each active |
michael@0 | 194 | ** thread in the process. The enumerator returns PR_SUCCESS if the enumeration |
michael@0 | 195 | ** should continue, any other value is considered failure, and enumeration |
michael@0 | 196 | ** stops, returning the failure value from PR_EnumerateThreads. |
michael@0 | 197 | ** Needed by the garbage collector. |
michael@0 | 198 | */ |
michael@0 | 199 | typedef PRStatus (PR_CALLBACK *PREnumerator)(PRThread *t, int i, void *arg); |
michael@0 | 200 | NSPR_API(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg); |
michael@0 | 201 | |
michael@0 | 202 | /* |
michael@0 | 203 | ** Signature of a thread stack scanning function. It is applied to every |
michael@0 | 204 | ** contiguous group of potential pointers within a thread. Count denotes the |
michael@0 | 205 | ** number of pointers. |
michael@0 | 206 | */ |
michael@0 | 207 | typedef PRStatus |
michael@0 | 208 | (PR_CALLBACK *PRScanStackFun)(PRThread* t, |
michael@0 | 209 | void** baseAddr, PRUword count, void* closure); |
michael@0 | 210 | |
michael@0 | 211 | /* |
michael@0 | 212 | ** Applies scanFun to all contiguous groups of potential pointers |
michael@0 | 213 | ** within a thread. This includes the stack, registers, and thread-local |
michael@0 | 214 | ** data. If scanFun returns a status value other than PR_SUCCESS the scan |
michael@0 | 215 | ** is aborted, and the status value is returned. |
michael@0 | 216 | */ |
michael@0 | 217 | NSPR_API(PRStatus) |
michael@0 | 218 | PR_ThreadScanStackPointers(PRThread* t, |
michael@0 | 219 | PRScanStackFun scanFun, void* scanClosure); |
michael@0 | 220 | |
michael@0 | 221 | /* |
michael@0 | 222 | ** Calls PR_ThreadScanStackPointers for every thread. |
michael@0 | 223 | */ |
michael@0 | 224 | NSPR_API(PRStatus) |
michael@0 | 225 | PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure); |
michael@0 | 226 | |
michael@0 | 227 | /* |
michael@0 | 228 | ** Returns a conservative estimate on the amount of stack space left |
michael@0 | 229 | ** on a thread in bytes, sufficient for making decisions about whether |
michael@0 | 230 | ** to continue recursing or not. |
michael@0 | 231 | */ |
michael@0 | 232 | NSPR_API(PRUword) |
michael@0 | 233 | PR_GetStackSpaceLeft(PRThread* t); |
michael@0 | 234 | |
michael@0 | 235 | /*--------------------------------------------------------------------------- |
michael@0 | 236 | ** THREAD CPU PRIVATE FUNCTIONS |
michael@0 | 237 | ---------------------------------------------------------------------------*/ |
michael@0 | 238 | |
michael@0 | 239 | /* |
michael@0 | 240 | ** Get a pointer to the primordial CPU. |
michael@0 | 241 | */ |
michael@0 | 242 | NSPR_API(struct _PRCPU *) _PR_GetPrimordialCPU(void); |
michael@0 | 243 | |
michael@0 | 244 | /*--------------------------------------------------------------------------- |
michael@0 | 245 | ** THREAD SYNCHRONIZATION PRIVATE FUNCTIONS |
michael@0 | 246 | ---------------------------------------------------------------------------*/ |
michael@0 | 247 | |
michael@0 | 248 | /* |
michael@0 | 249 | ** Create a new named monitor (named for debugging purposes). |
michael@0 | 250 | ** Monitors are re-entrant locks with a built-in condition variable. |
michael@0 | 251 | ** |
michael@0 | 252 | ** This may fail if memory is tight or if some operating system resource |
michael@0 | 253 | ** is low. |
michael@0 | 254 | */ |
michael@0 | 255 | NSPR_API(PRMonitor*) PR_NewNamedMonitor(const char* name); |
michael@0 | 256 | |
michael@0 | 257 | /* |
michael@0 | 258 | ** Test and then lock the lock if it's not already locked by some other |
michael@0 | 259 | ** thread. Return PR_FALSE if some other thread owned the lock at the |
michael@0 | 260 | ** time of the call. |
michael@0 | 261 | */ |
michael@0 | 262 | NSPR_API(PRBool) PR_TestAndLock(PRLock *lock); |
michael@0 | 263 | |
michael@0 | 264 | /* |
michael@0 | 265 | ** Test and then enter the mutex associated with the monitor if it's not |
michael@0 | 266 | ** already entered by some other thread. Return PR_FALSE if some other |
michael@0 | 267 | ** thread owned the mutex at the time of the call. |
michael@0 | 268 | */ |
michael@0 | 269 | NSPR_API(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon); |
michael@0 | 270 | |
michael@0 | 271 | /* |
michael@0 | 272 | ** Return the number of times that the current thread has entered the |
michael@0 | 273 | ** mutex. Returns zero if the current thread has not entered the mutex. |
michael@0 | 274 | */ |
michael@0 | 275 | NSPR_API(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon); |
michael@0 | 276 | |
michael@0 | 277 | /* |
michael@0 | 278 | ** Just like PR_CEnterMonitor except that if the monitor is owned by |
michael@0 | 279 | ** another thread NULL is returned. |
michael@0 | 280 | */ |
michael@0 | 281 | NSPR_API(PRMonitor*) PR_CTestAndEnterMonitor(void *address); |
michael@0 | 282 | |
michael@0 | 283 | /*--------------------------------------------------------------------------- |
michael@0 | 284 | ** PLATFORM-SPECIFIC INITIALIZATION FUNCTIONS |
michael@0 | 285 | ---------------------------------------------------------------------------*/ |
michael@0 | 286 | #if defined(IRIX) |
michael@0 | 287 | /* |
michael@0 | 288 | ** Irix specific initialization funtion to be called before PR_Init |
michael@0 | 289 | ** is called by the application. Sets the CONF_INITUSERS and CONF_INITSIZE |
michael@0 | 290 | ** attributes of the shared arena set up by nspr. |
michael@0 | 291 | ** |
michael@0 | 292 | ** The environment variables _NSPR_IRIX_INITUSERS and _NSPR_IRIX_INITSIZE |
michael@0 | 293 | ** can also be used to set these arena attributes. If _NSPR_IRIX_INITUSERS |
michael@0 | 294 | ** is set, but not _NSPR_IRIX_INITSIZE, the value of the CONF_INITSIZE |
michael@0 | 295 | ** attribute of the nspr arena is scaled as a function of the |
michael@0 | 296 | ** _NSPR_IRIX_INITUSERS value. |
michael@0 | 297 | ** |
michael@0 | 298 | ** If the _PR_Irix_Set_Arena_Params() is called in addition to setting the |
michael@0 | 299 | ** environment variables, the values of the environment variables are used. |
michael@0 | 300 | ** |
michael@0 | 301 | */ |
michael@0 | 302 | NSPR_API(void) _PR_Irix_Set_Arena_Params(PRInt32 initusers, PRInt32 initsize); |
michael@0 | 303 | |
michael@0 | 304 | #endif /* IRIX */ |
michael@0 | 305 | |
michael@0 | 306 | #if defined(XP_OS2) |
michael@0 | 307 | /* |
michael@0 | 308 | ** These functions need to be called at the start and end of a thread. |
michael@0 | 309 | ** An EXCEPTIONREGISTRATIONRECORD must be declared on the stack and its |
michael@0 | 310 | ** address passed to the two functions. |
michael@0 | 311 | */ |
michael@0 | 312 | NSPR_API(void) PR_OS2_SetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e); |
michael@0 | 313 | NSPR_API(void) PR_OS2_UnsetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e); |
michael@0 | 314 | #endif /* XP_OS2 */ |
michael@0 | 315 | |
michael@0 | 316 | /* I think PR_GetMonitorEntryCount is useless. All you really want is this... */ |
michael@0 | 317 | #define PR_InMonitor(m) (PR_GetMonitorEntryCount(m) > 0) |
michael@0 | 318 | |
michael@0 | 319 | /*--------------------------------------------------------------------------- |
michael@0 | 320 | ** Special X-Lock hack for client |
michael@0 | 321 | ---------------------------------------------------------------------------*/ |
michael@0 | 322 | |
michael@0 | 323 | #ifdef XP_UNIX |
michael@0 | 324 | extern void PR_XLock(void); |
michael@0 | 325 | extern void PR_XUnlock(void); |
michael@0 | 326 | extern PRBool PR_XIsLocked(void); |
michael@0 | 327 | #endif /* XP_UNIX */ |
michael@0 | 328 | |
michael@0 | 329 | PR_END_EXTERN_C |
michael@0 | 330 | |
michael@0 | 331 | #endif /* pprthred_h___ */ |