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