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 prthread_h___ andre@0: #define prthread_h___ andre@0: andre@0: /* andre@0: ** API for NSPR threads. On some architectures (Mac OS Classic andre@0: ** notably) pre-emptibility is not guaranteed. Hard priority scheduling andre@0: ** is not guaranteed, so programming using priority based synchronization andre@0: ** is a no-no. andre@0: ** andre@0: ** NSPR threads are scheduled based loosely on their client set priority. andre@0: ** In general, a thread of a higher priority has a statistically better andre@0: ** chance of running relative to threads of lower priority. However, andre@0: ** NSPR uses multiple strategies to provide execution vehicles for thread andre@0: ** abstraction of various host platforms. As it turns out, there is little andre@0: ** NSPR can do to affect the scheduling attributes of "GLOBAL" threads. andre@0: ** However, a semblance of GLOBAL threads is used to implement "LOCAL" andre@0: ** threads. An arbitrary number of such LOCAL threads can be assigned to andre@0: ** a single GLOBAL thread. andre@0: ** andre@0: ** For scheduling, NSPR will attempt to run the highest priority LOCAL andre@0: ** thread associated with a given GLOBAL thread. It is further assumed andre@0: ** that the host OS will apply some form of "fair" scheduling on the andre@0: ** GLOBAL threads. andre@0: ** andre@0: ** Threads have a "system flag" which when set indicates the thread andre@0: ** doesn't count for determining when the process should exit (the andre@0: ** process exits when the last user thread exits). andre@0: ** andre@0: ** Threads also have a "scope flag" which controls whether the threads andre@0: ** are scheduled in the local scope or scheduled by the OS globally. This andre@0: ** indicates whether a thread is permanently bound to a native OS thread. andre@0: ** An unbound thread competes for scheduling resources in the same process. andre@0: ** andre@0: ** Another flag is "state flag" which control whether the thread is joinable. andre@0: ** It allows other threads to wait for the created thread to reach completion. andre@0: ** andre@0: ** Threads can have "per-thread-data" attached to them. Each thread has a andre@0: ** per-thread error number and error string which are updated when NSPR andre@0: ** operations fail. andre@0: */ andre@0: #include "prtypes.h" andre@0: #include "prinrval.h" andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: typedef struct PRThread PRThread; andre@0: typedef struct PRThreadStack PRThreadStack; andre@0: andre@0: typedef enum PRThreadType { andre@0: PR_USER_THREAD, andre@0: PR_SYSTEM_THREAD andre@0: } PRThreadType; andre@0: andre@0: typedef enum PRThreadScope { andre@0: PR_LOCAL_THREAD, andre@0: PR_GLOBAL_THREAD, andre@0: PR_GLOBAL_BOUND_THREAD andre@0: } PRThreadScope; andre@0: andre@0: typedef enum PRThreadState { andre@0: PR_JOINABLE_THREAD, andre@0: PR_UNJOINABLE_THREAD andre@0: } PRThreadState; andre@0: andre@0: typedef enum PRThreadPriority andre@0: { andre@0: PR_PRIORITY_FIRST = 0, /* just a placeholder */ andre@0: PR_PRIORITY_LOW = 0, /* the lowest possible priority */ andre@0: PR_PRIORITY_NORMAL = 1, /* most common expected priority */ andre@0: PR_PRIORITY_HIGH = 2, /* slightly more aggressive scheduling */ andre@0: PR_PRIORITY_URGENT = 3, /* it does little good to have more than one */ andre@0: PR_PRIORITY_LAST = 3 /* this is just a placeholder */ andre@0: } PRThreadPriority; andre@0: andre@0: /* andre@0: ** Create a new thread: andre@0: ** "type" is the type of thread to create andre@0: ** "start(arg)" will be invoked as the threads "main" andre@0: ** "priority" will be created thread's priority andre@0: ** "scope" will specify whether the thread is local or global andre@0: ** "state" will specify whether the thread is joinable or not andre@0: ** "stackSize" the size of the stack, in bytes. The value can be zero andre@0: ** and then a machine specific stack size will be chosen. andre@0: ** andre@0: ** This can return NULL if some kind of error occurs, such as if memory is andre@0: ** tight. andre@0: ** andre@0: ** If you want the thread to start up waiting for the creator to do andre@0: ** something, enter a lock before creating the thread and then have the andre@0: ** threads start routine enter and exit the same lock. When you are ready andre@0: ** for the thread to run, exit the lock. andre@0: ** andre@0: ** If you want to detect the completion of the created thread, the thread andre@0: ** should be created joinable. Then, use PR_JoinThread to synchrnoize the andre@0: ** termination of another thread. andre@0: ** andre@0: ** When the start function returns the thread exits. If it is the last andre@0: ** PR_USER_THREAD to exit then the process exits. andre@0: */ andre@0: NSPR_API(PRThread*) PR_CreateThread(PRThreadType type, andre@0: void (PR_CALLBACK *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: ** Wait for thread termination: andre@0: ** "thread" is the target thread andre@0: ** andre@0: ** This can return PR_FAILURE if no joinable thread could be found andre@0: ** corresponding to the specified target thread. andre@0: ** andre@0: ** The calling thread is blocked until the target thread completes. andre@0: ** Several threads cannot wait for the same thread to complete; one thread andre@0: ** will operate successfully and others will terminate with an error PR_FAILURE. andre@0: ** The calling thread will not be blocked if the target thread has already andre@0: ** terminated. andre@0: */ andre@0: NSPR_API(PRStatus) PR_JoinThread(PRThread *thread); andre@0: andre@0: /* andre@0: ** Return the current thread object for the currently running code. andre@0: ** Never returns NULL. andre@0: */ andre@0: NSPR_API(PRThread*) PR_GetCurrentThread(void); andre@0: #ifndef NO_NSPR_10_SUPPORT andre@0: #define PR_CurrentThread() PR_GetCurrentThread() /* for nspr1.0 compat. */ andre@0: #endif /* NO_NSPR_10_SUPPORT */ andre@0: andre@0: /* andre@0: ** Get the priority of "thread". andre@0: */ andre@0: NSPR_API(PRThreadPriority) PR_GetThreadPriority(const PRThread *thread); andre@0: andre@0: /* andre@0: ** Change the priority of the "thread" to "priority". andre@0: ** andre@0: ** PR_SetThreadPriority works in a best-effort manner. On some platforms a andre@0: ** special privilege, such as root access, is required to change thread andre@0: ** priorities, especially to raise thread priorities. If the caller doesn't andre@0: ** have enough privileges to change thread priorites, the function has no andre@0: ** effect except causing a future PR_GetThreadPriority call to return andre@0: ** |priority|. andre@0: */ andre@0: NSPR_API(void) PR_SetThreadPriority(PRThread *thread, PRThreadPriority priority); andre@0: andre@0: /* andre@0: ** Set the name of the current thread, which will be visible in a debugger andre@0: ** and accessible via a call to PR_GetThreadName(). andre@0: */ andre@0: NSPR_API(PRStatus) PR_SetCurrentThreadName(const char *name); andre@0: andre@0: /* andre@0: ** Return the name of "thread", if set. Otherwise return NULL. andre@0: */ andre@0: NSPR_API(const char *) PR_GetThreadName(const PRThread *thread); andre@0: andre@0: /* andre@0: ** This routine returns a new index for per-thread-private data table. andre@0: ** The index is visible to all threads within a process. This index can andre@0: ** be used with the PR_SetThreadPrivate() and PR_GetThreadPrivate() routines andre@0: ** to save and retrieve data associated with the index for a thread. andre@0: ** andre@0: ** Each index is associationed with a destructor function ('dtor'). The function andre@0: ** may be specified as NULL when the index is created. If it is not NULL, the andre@0: ** function will be called when: andre@0: ** - the thread exits and the private data for the associated index andre@0: ** is not NULL, andre@0: ** - new thread private data is set and the current private data is andre@0: ** not NULL. andre@0: ** andre@0: ** The index independently maintains specific values for each binding thread. andre@0: ** A thread can only get access to its own thread-specific-data. andre@0: ** andre@0: ** Upon a new index return the value associated with the index for all threads andre@0: ** is NULL, and upon thread creation the value associated with all indices for andre@0: ** that thread is NULL. andre@0: ** andre@0: ** Returns PR_FAILURE if the total number of indices will exceed the maximun andre@0: ** allowed. andre@0: */ andre@0: typedef void (PR_CALLBACK *PRThreadPrivateDTOR)(void *priv); andre@0: andre@0: NSPR_API(PRStatus) PR_NewThreadPrivateIndex( andre@0: PRUintn *newIndex, PRThreadPrivateDTOR destructor); andre@0: andre@0: /* andre@0: ** Define some per-thread-private data. andre@0: ** "tpdIndex" is an index into the per-thread private data table andre@0: ** "priv" is the per-thread-private data andre@0: ** andre@0: ** If the per-thread private data table has a previously registered andre@0: ** destructor function and a non-NULL per-thread-private data value, andre@0: ** the destructor function is invoked. andre@0: ** andre@0: ** This can return PR_FAILURE if the index is invalid. andre@0: */ andre@0: NSPR_API(PRStatus) PR_SetThreadPrivate(PRUintn tpdIndex, void *priv); andre@0: andre@0: /* andre@0: ** Recover the per-thread-private data for the current thread. "tpdIndex" is andre@0: ** the index into the per-thread private data table. andre@0: ** andre@0: ** The returned value may be NULL which is indistinguishable from an error andre@0: ** condition. andre@0: ** andre@0: ** A thread can only get access to its own thread-specific-data. andre@0: */ andre@0: NSPR_API(void*) PR_GetThreadPrivate(PRUintn tpdIndex); andre@0: andre@0: /* andre@0: ** This routine sets the interrupt request for a target thread. The interrupt andre@0: ** request remains in the thread's state until it is delivered exactly once andre@0: ** or explicitly canceled. andre@0: ** andre@0: ** A thread that has been interrupted will fail all NSPR blocking operations andre@0: ** that return a PRStatus (I/O, waiting on a condition, etc). andre@0: ** andre@0: ** PR_Interrupt may itself fail if the target thread is invalid. andre@0: */ andre@0: NSPR_API(PRStatus) PR_Interrupt(PRThread *thread); andre@0: andre@0: /* andre@0: ** Clear the interrupt request for the calling thread. If no such request andre@0: ** is pending, this operation is a noop. andre@0: */ andre@0: NSPR_API(void) PR_ClearInterrupt(void); andre@0: andre@0: /* andre@0: ** Block the interrupt for the calling thread. andre@0: */ andre@0: NSPR_API(void) PR_BlockInterrupt(void); andre@0: andre@0: /* andre@0: ** Unblock the interrupt for the calling thread. andre@0: */ andre@0: NSPR_API(void) PR_UnblockInterrupt(void); andre@0: andre@0: /* andre@0: ** Make the current thread sleep until "ticks" time amount of time andre@0: ** has expired. If "ticks" is PR_INTERVAL_NO_WAIT then the call is andre@0: ** equivalent to calling PR_Yield. Calling PR_Sleep with an argument andre@0: ** equivalent to PR_INTERVAL_NO_TIMEOUT is an error and will result andre@0: ** in a PR_FAILURE error return. andre@0: */ andre@0: NSPR_API(PRStatus) PR_Sleep(PRIntervalTime ticks); andre@0: andre@0: /* andre@0: ** Get the scoping of this thread. andre@0: */ andre@0: NSPR_API(PRThreadScope) PR_GetThreadScope(const PRThread *thread); andre@0: andre@0: /* andre@0: ** Get the type of this thread. andre@0: */ andre@0: NSPR_API(PRThreadType) PR_GetThreadType(const PRThread *thread); andre@0: andre@0: /* andre@0: ** Get the join state of this thread. andre@0: */ andre@0: NSPR_API(PRThreadState) PR_GetThreadState(const PRThread *thread); andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* prthread_h___ */