andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef pprthred_h___ andre@0: #define pprthred_h___ andre@0: andre@0: /* andre@0: ** API for PR private functions. These calls are to be used by internal andre@0: ** developers only. andre@0: */ andre@0: #include "nspr.h" andre@0: andre@0: #if defined(XP_OS2) andre@0: #define INCL_DOS andre@0: #define INCL_DOSERRORS andre@0: #define INCL_WIN andre@0: #include andre@0: #endif andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /*--------------------------------------------------------------------------- andre@0: ** THREAD PRIVATE FUNCTIONS andre@0: ---------------------------------------------------------------------------*/ andre@0: andre@0: /* andre@0: ** Associate a thread object with an existing native thread. andre@0: ** "type" is the type of thread object to attach andre@0: ** "priority" is the priority to assign to the thread andre@0: ** "stack" defines the shape of the threads stack andre@0: ** andre@0: ** This can return NULL if some kind of error occurs, or if memory is andre@0: ** tight. This call invokes "start(obj,arg)" and returns when the andre@0: ** function returns. The thread object is automatically destroyed. andre@0: ** andre@0: ** This call is not normally needed unless you create your own native andre@0: ** thread. PR_Init does this automatically for the primordial thread. andre@0: */ andre@0: NSPR_API(PRThread*) PR_AttachThread(PRThreadType type, andre@0: PRThreadPriority priority, andre@0: PRThreadStack *stack); andre@0: andre@0: /* andre@0: ** Detach the nspr thread from the currently executing native thread. andre@0: ** The thread object will be destroyed and all related data attached andre@0: ** to it. The exit procs will be invoked. andre@0: ** andre@0: ** This call is not normally needed unless you create your own native andre@0: ** thread. PR_Exit will automatially detach the nspr thread object andre@0: ** created by PR_Init for the primordial thread. andre@0: ** andre@0: ** This call returns after the nspr thread object is destroyed. andre@0: */ andre@0: NSPR_API(void) PR_DetachThread(void); andre@0: andre@0: /* andre@0: ** Get the id of the named thread. Each thread is assigned a unique id andre@0: ** when it is created or attached. andre@0: */ andre@0: NSPR_API(PRUint32) PR_GetThreadID(PRThread *thread); andre@0: andre@0: /* andre@0: ** Set the procedure that is called when a thread is dumped. The procedure andre@0: ** will be applied to the argument, arg, when called. Setting the procedure andre@0: ** to NULL effectively removes it. andre@0: */ andre@0: typedef void (*PRThreadDumpProc)(PRFileDesc *fd, PRThread *t, void *arg); andre@0: NSPR_API(void) PR_SetThreadDumpProc( andre@0: PRThread* thread, PRThreadDumpProc dump, void *arg); andre@0: andre@0: /* andre@0: ** Get this thread's affinity mask. The affinity mask is a 32 bit quantity andre@0: ** marking a bit for each processor this process is allowed to run on. andre@0: ** The processor mask is returned in the mask argument. andre@0: ** The least-significant-bit represents processor 0. andre@0: ** andre@0: ** Returns 0 on success, -1 on failure. andre@0: */ andre@0: NSPR_API(PRInt32) PR_GetThreadAffinityMask(PRThread *thread, PRUint32 *mask); andre@0: andre@0: /* andre@0: ** Set this thread's affinity mask. andre@0: ** andre@0: ** Returns 0 on success, -1 on failure. andre@0: */ andre@0: NSPR_API(PRInt32) PR_SetThreadAffinityMask(PRThread *thread, PRUint32 mask ); andre@0: andre@0: /* andre@0: ** Set the default CPU Affinity mask. andre@0: ** andre@0: */ andre@0: NSPR_API(PRInt32) PR_SetCPUAffinityMask(PRUint32 mask); andre@0: andre@0: /* andre@0: ** Show status of all threads to standard error output. andre@0: */ andre@0: NSPR_API(void) PR_ShowStatus(void); andre@0: andre@0: /* andre@0: ** Set thread recycle mode to on (1) or off (0) andre@0: */ andre@0: NSPR_API(void) PR_SetThreadRecycleMode(PRUint32 flag); andre@0: andre@0: andre@0: /*--------------------------------------------------------------------------- andre@0: ** THREAD PRIVATE FUNCTIONS FOR GARBAGE COLLECTIBLE THREADS andre@0: ---------------------------------------------------------------------------*/ andre@0: andre@0: /* andre@0: ** Only Garbage collectible threads participate in resume all, suspend all and andre@0: ** enumeration operations. They are also different during creation when andre@0: ** platform specific action may be needed (For example, all Solaris GC able andre@0: ** threads are bound threads). andre@0: */ andre@0: andre@0: /* andre@0: ** Same as PR_CreateThread except that the thread is marked as garbage andre@0: ** collectible. andre@0: */ andre@0: NSPR_API(PRThread*) PR_CreateThreadGCAble(PRThreadType type, andre@0: void (*start)(void *arg), andre@0: void *arg, andre@0: PRThreadPriority priority, andre@0: PRThreadScope scope, andre@0: PRThreadState state, andre@0: PRUint32 stackSize); andre@0: andre@0: /* andre@0: ** Same as PR_AttachThread except that the thread being attached is marked as andre@0: ** garbage collectible. andre@0: */ andre@0: NSPR_API(PRThread*) PR_AttachThreadGCAble(PRThreadType type, andre@0: PRThreadPriority priority, andre@0: PRThreadStack *stack); andre@0: andre@0: /* andre@0: ** Mark the thread as garbage collectible. andre@0: */ andre@0: NSPR_API(void) PR_SetThreadGCAble(void); andre@0: andre@0: /* andre@0: ** Unmark the thread as garbage collectible. andre@0: */ andre@0: NSPR_API(void) PR_ClearThreadGCAble(void); andre@0: andre@0: /* andre@0: ** This routine prevents all other GC able threads from running. This call is needed by andre@0: ** the garbage collector. andre@0: */ andre@0: NSPR_API(void) PR_SuspendAll(void); andre@0: andre@0: /* andre@0: ** This routine unblocks all other GC able threads that were suspended from running by andre@0: ** PR_SuspendAll(). This call is needed by the garbage collector. andre@0: */ andre@0: NSPR_API(void) PR_ResumeAll(void); andre@0: andre@0: /* andre@0: ** Return the thread stack pointer of the given thread. andre@0: ** Needed by the garbage collector. andre@0: */ andre@0: NSPR_API(void *) PR_GetSP(PRThread *thread); andre@0: andre@0: /* andre@0: ** Save the registers that the GC would find interesting into the thread andre@0: ** "t". isCurrent will be non-zero if the thread state that is being andre@0: ** saved is the currently executing thread. Return the address of the andre@0: ** first register to be scanned as well as the number of registers to andre@0: ** scan in "np". andre@0: ** andre@0: ** If "isCurrent" is non-zero then it is allowed for the thread context andre@0: ** area to be used as scratch storage to hold just the registers andre@0: ** necessary for scanning. andre@0: ** andre@0: ** This function simply calls the internal function _MD_HomeGCRegisters(). andre@0: */ andre@0: NSPR_API(PRWord *) PR_GetGCRegisters(PRThread *t, int isCurrent, int *np); andre@0: andre@0: /* andre@0: ** (Get|Set)ExecutionEnvironent andre@0: ** andre@0: ** Used by Java to associate it's execution environment so garbage collector andre@0: ** can find it. If return is NULL, then it's probably not a collectable thread. andre@0: ** andre@0: ** There's no locking required around these calls. andre@0: */ andre@0: NSPR_API(void*) GetExecutionEnvironment(PRThread *thread); andre@0: NSPR_API(void) SetExecutionEnvironment(PRThread* thread, void *environment); andre@0: andre@0: /* andre@0: ** Enumeration function that applies "func(thread,i,arg)" to each active andre@0: ** thread in the process. The enumerator returns PR_SUCCESS if the enumeration andre@0: ** should continue, any other value is considered failure, and enumeration andre@0: ** stops, returning the failure value from PR_EnumerateThreads. andre@0: ** Needed by the garbage collector. andre@0: */ andre@0: typedef PRStatus (PR_CALLBACK *PREnumerator)(PRThread *t, int i, void *arg); andre@0: NSPR_API(PRStatus) PR_EnumerateThreads(PREnumerator func, void *arg); andre@0: andre@0: /* andre@0: ** Signature of a thread stack scanning function. It is applied to every andre@0: ** contiguous group of potential pointers within a thread. Count denotes the andre@0: ** number of pointers. andre@0: */ andre@0: typedef PRStatus andre@0: (PR_CALLBACK *PRScanStackFun)(PRThread* t, andre@0: void** baseAddr, PRUword count, void* closure); andre@0: andre@0: /* andre@0: ** Applies scanFun to all contiguous groups of potential pointers andre@0: ** within a thread. This includes the stack, registers, and thread-local andre@0: ** data. If scanFun returns a status value other than PR_SUCCESS the scan andre@0: ** is aborted, and the status value is returned. andre@0: */ andre@0: NSPR_API(PRStatus) andre@0: PR_ThreadScanStackPointers(PRThread* t, andre@0: PRScanStackFun scanFun, void* scanClosure); andre@0: andre@0: /* andre@0: ** Calls PR_ThreadScanStackPointers for every thread. andre@0: */ andre@0: NSPR_API(PRStatus) andre@0: PR_ScanStackPointers(PRScanStackFun scanFun, void* scanClosure); andre@0: andre@0: /* andre@0: ** Returns a conservative estimate on the amount of stack space left andre@0: ** on a thread in bytes, sufficient for making decisions about whether andre@0: ** to continue recursing or not. andre@0: */ andre@0: NSPR_API(PRUword) andre@0: PR_GetStackSpaceLeft(PRThread* t); andre@0: andre@0: /*--------------------------------------------------------------------------- andre@0: ** THREAD CPU PRIVATE FUNCTIONS andre@0: ---------------------------------------------------------------------------*/ andre@0: andre@0: /* andre@0: ** Get a pointer to the primordial CPU. andre@0: */ andre@0: NSPR_API(struct _PRCPU *) _PR_GetPrimordialCPU(void); andre@0: andre@0: /*--------------------------------------------------------------------------- andre@0: ** THREAD SYNCHRONIZATION PRIVATE FUNCTIONS andre@0: ---------------------------------------------------------------------------*/ andre@0: andre@0: /* andre@0: ** Create a new named monitor (named for debugging purposes). andre@0: ** Monitors are re-entrant locks with a built-in condition variable. andre@0: ** andre@0: ** This may fail if memory is tight or if some operating system resource andre@0: ** is low. andre@0: */ andre@0: NSPR_API(PRMonitor*) PR_NewNamedMonitor(const char* name); andre@0: andre@0: /* andre@0: ** Test and then lock the lock if it's not already locked by some other andre@0: ** thread. Return PR_FALSE if some other thread owned the lock at the andre@0: ** time of the call. andre@0: */ andre@0: NSPR_API(PRBool) PR_TestAndLock(PRLock *lock); andre@0: andre@0: /* andre@0: ** Test and then enter the mutex associated with the monitor if it's not andre@0: ** already entered by some other thread. Return PR_FALSE if some other andre@0: ** thread owned the mutex at the time of the call. andre@0: */ andre@0: NSPR_API(PRBool) PR_TestAndEnterMonitor(PRMonitor *mon); andre@0: andre@0: /* andre@0: ** Return the number of times that the current thread has entered the andre@0: ** mutex. Returns zero if the current thread has not entered the mutex. andre@0: */ andre@0: NSPR_API(PRIntn) PR_GetMonitorEntryCount(PRMonitor *mon); andre@0: andre@0: /* andre@0: ** Just like PR_CEnterMonitor except that if the monitor is owned by andre@0: ** another thread NULL is returned. andre@0: */ andre@0: NSPR_API(PRMonitor*) PR_CTestAndEnterMonitor(void *address); andre@0: andre@0: /*--------------------------------------------------------------------------- andre@0: ** PLATFORM-SPECIFIC INITIALIZATION FUNCTIONS andre@0: ---------------------------------------------------------------------------*/ andre@0: #if defined(IRIX) andre@0: /* andre@0: ** Irix specific initialization funtion to be called before PR_Init andre@0: ** is called by the application. Sets the CONF_INITUSERS and CONF_INITSIZE andre@0: ** attributes of the shared arena set up by nspr. andre@0: ** andre@0: ** The environment variables _NSPR_IRIX_INITUSERS and _NSPR_IRIX_INITSIZE andre@0: ** can also be used to set these arena attributes. If _NSPR_IRIX_INITUSERS andre@0: ** is set, but not _NSPR_IRIX_INITSIZE, the value of the CONF_INITSIZE andre@0: ** attribute of the nspr arena is scaled as a function of the andre@0: ** _NSPR_IRIX_INITUSERS value. andre@0: ** andre@0: ** If the _PR_Irix_Set_Arena_Params() is called in addition to setting the andre@0: ** environment variables, the values of the environment variables are used. andre@0: ** andre@0: */ andre@0: NSPR_API(void) _PR_Irix_Set_Arena_Params(PRInt32 initusers, PRInt32 initsize); andre@0: andre@0: #endif /* IRIX */ andre@0: andre@0: #if defined(XP_OS2) andre@0: /* andre@0: ** These functions need to be called at the start and end of a thread. andre@0: ** An EXCEPTIONREGISTRATIONRECORD must be declared on the stack and its andre@0: ** address passed to the two functions. andre@0: */ andre@0: NSPR_API(void) PR_OS2_SetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e); andre@0: NSPR_API(void) PR_OS2_UnsetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* e); andre@0: #endif /* XP_OS2 */ andre@0: andre@0: /* I think PR_GetMonitorEntryCount is useless. All you really want is this... */ andre@0: #define PR_InMonitor(m) (PR_GetMonitorEntryCount(m) > 0) andre@0: andre@0: /*--------------------------------------------------------------------------- andre@0: ** Special X-Lock hack for client andre@0: ---------------------------------------------------------------------------*/ andre@0: andre@0: #ifdef XP_UNIX andre@0: extern void PR_XLock(void); andre@0: extern void PR_XUnlock(void); andre@0: extern PRBool PR_XIsLocked(void); andre@0: #endif /* XP_UNIX */ andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* pprthred_h___ */