1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
42 ** API for NSPR threads. On some architectures (MAC and WIN16
43 ** notably) pre-emptibility is not guaranteed. Hard priority scheduling
44 ** is not guaranteed, so programming using priority based synchronization
47 ** NSPR threads are scheduled based loosely on their client set priority.
48 ** In general, a thread of a higher priority has a statistically better
49 ** chance of running relative to threads of lower priority. However,
50 ** NSPR uses multiple strategies to provide execution vehicles for thread
51 ** abstraction of various host platforms. As it turns out, there is little
52 ** NSPR can do to affect the scheduling attributes of "GLOBAL" threads.
53 ** However, a semblance of GLOBAL threads is used to implement "LOCAL"
54 ** threads. An arbitrary number of such LOCAL threads can be assigned to
55 ** a single GLOBAL thread.
57 ** For scheduling, NSPR will attempt to run the highest priority LOCAL
58 ** thread associated with a given GLOBAL thread. It is further assumed
59 ** that the host OS will apply some form of "fair" scheduling on the
62 ** Threads have a "system flag" which when set indicates the thread
63 ** doesn't count for determining when the process should exit (the
64 ** process exits when the last user thread exits).
66 ** Threads also have a "scope flag" which controls whether the threads
67 ** are scheduled in the local scope or scheduled by the OS globally. This
68 ** indicates whether a thread is permanently bound to a native OS thread.
69 ** An unbound thread competes for scheduling resources in the same process.
71 ** Another flag is "state flag" which control whether the thread is joinable.
72 ** It allows other threads to wait for the created thread to reach completion.
74 ** Threads can have "per-thread-data" attached to them. Each thread has a
75 ** per-thread error number and error string which are updated when NSPR
83 typedef struct PRThread PRThread
;
84 typedef struct PRThreadStack PRThreadStack
;
86 typedef enum PRThreadType
{
91 typedef enum PRThreadScope
{
94 PR_GLOBAL_BOUND_THREAD
97 typedef enum PRThreadState
{
102 typedef enum PRThreadPriority
104 PR_PRIORITY_FIRST
= 0, /* just a placeholder */
105 PR_PRIORITY_LOW
= 0, /* the lowest possible priority */
106 PR_PRIORITY_NORMAL
= 1, /* most common expected priority */
107 PR_PRIORITY_HIGH
= 2, /* slightly more aggressive scheduling */
108 PR_PRIORITY_URGENT
= 3, /* it does little good to have more than one */
109 PR_PRIORITY_LAST
= 3 /* this is just a placeholder */
113 ** Create a new thread:
114 ** "type" is the type of thread to create
115 ** "start(arg)" will be invoked as the threads "main"
116 ** "priority" will be created thread's priority
117 ** "scope" will specify whether the thread is local or global
118 ** "state" will specify whether the thread is joinable or not
119 ** "stackSize" the size of the stack, in bytes. The value can be zero
120 ** and then a machine specific stack size will be chosen.
122 ** This can return NULL if some kind of error occurs, such as if memory is
125 ** If you want the thread to start up waiting for the creator to do
126 ** something, enter a lock before creating the thread and then have the
127 ** threads start routine enter and exit the same lock. When you are ready
128 ** for the thread to run, exit the lock.
130 ** If you want to detect the completion of the created thread, the thread
131 ** should be created joinable. Then, use PR_JoinThread to synchrnoize the
132 ** termination of another thread.
134 ** When the start function returns the thread exits. If it is the last
135 ** PR_USER_THREAD to exit then the process exits.
137 NSPR_API(PRThread
*) PR_CreateThread(PRThreadType type
,
138 void (PR_CALLBACK
*start
)(void *arg
),
140 PRThreadPriority priority
,
146 ** Wait for thread termination:
147 ** "thread" is the target thread
149 ** This can return PR_FAILURE if no joinable thread could be found
150 ** corresponding to the specified target thread.
152 ** The calling thread is blocked until the target thread completes.
153 ** Several threads cannot wait for the same thread to complete; one thread
154 ** will operate successfully and others will terminate with an error PR_FAILURE.
155 ** The calling thread will not be blocked if the target thread has already
158 NSPR_API(PRStatus
) PR_JoinThread(PRThread
*thread
);
161 ** Return the current thread object for the currently running code.
162 ** Never returns NULL.
164 NSPR_API(PRThread
*) PR_GetCurrentThread(void);
165 #ifndef NO_NSPR_10_SUPPORT
166 #define PR_CurrentThread() PR_GetCurrentThread() /* for nspr1.0 compat. */
167 #endif /* NO_NSPR_10_SUPPORT */
170 ** Get the priority of "thread".
172 NSPR_API(PRThreadPriority
) PR_GetThreadPriority(const PRThread
*thread
);
175 ** Change the priority of the "thread" to "priority".
177 NSPR_API(void) PR_SetThreadPriority(PRThread
*thread
, PRThreadPriority priority
);
180 ** This routine returns a new index for per-thread-private data table.
181 ** The index is visible to all threads within a process. This index can
182 ** be used with the PR_SetThreadPrivate() and PR_GetThreadPrivate() routines
183 ** to save and retrieve data associated with the index for a thread.
185 ** Each index is associationed with a destructor function ('dtor'). The function
186 ** may be specified as NULL when the index is created. If it is not NULL, the
187 ** function will be called when:
188 ** - the thread exits and the private data for the associated index
190 ** - new thread private data is set and the current private data is
193 ** The index independently maintains specific values for each binding thread.
194 ** A thread can only get access to its own thread-specific-data.
196 ** Upon a new index return the value associated with the index for all threads
197 ** is NULL, and upon thread creation the value associated with all indices for
198 ** that thread is NULL.
200 ** Returns PR_FAILURE if the total number of indices will exceed the maximun
203 typedef void (PR_CALLBACK
*PRThreadPrivateDTOR
)(void *priv
);
205 NSPR_API(PRStatus
) PR_NewThreadPrivateIndex(
206 PRUintn
*newIndex
, PRThreadPrivateDTOR destructor
);
209 ** Define some per-thread-private data.
210 ** "tpdIndex" is an index into the per-thread private data table
211 ** "priv" is the per-thread-private data
213 ** If the per-thread private data table has a previously registered
214 ** destructor function and a non-NULL per-thread-private data value,
215 ** the destructor function is invoked.
217 ** This can return PR_FAILURE if the index is invalid.
219 NSPR_API(PRStatus
) PR_SetThreadPrivate(PRUintn tpdIndex
, void *priv
);
222 ** Recover the per-thread-private data for the current thread. "tpdIndex" is
223 ** the index into the per-thread private data table.
225 ** The returned value may be NULL which is indistinguishable from an error
228 ** A thread can only get access to its own thread-specific-data.
230 NSPR_API(void*) PR_GetThreadPrivate(PRUintn tpdIndex
);
233 ** This routine sets the interrupt request for a target thread. The interrupt
234 ** request remains in the thread's state until it is delivered exactly once
235 ** or explicitly canceled.
237 ** A thread that has been interrupted will fail all NSPR blocking operations
238 ** that return a PRStatus (I/O, waiting on a condition, etc).
240 ** PR_Interrupt may itself fail if the target thread is invalid.
242 NSPR_API(PRStatus
) PR_Interrupt(PRThread
*thread
);
245 ** Clear the interrupt request for the calling thread. If no such request
246 ** is pending, this operation is a noop.
248 NSPR_API(void) PR_ClearInterrupt(void);
251 ** Block the interrupt for the calling thread.
253 NSPR_API(void) PR_BlockInterrupt(void);
256 ** Unblock the interrupt for the calling thread.
258 NSPR_API(void) PR_UnblockInterrupt(void);
261 ** Make the current thread sleep until "ticks" time amount of time
262 ** has expired. If "ticks" is PR_INTERVAL_NO_WAIT then the call is
263 ** equivalent to calling PR_Yield. Calling PR_Sleep with an argument
264 ** equivalent to PR_INTERVAL_NO_TIMEOUT is an error and will result
265 ** in a PR_FAILURE error return.
267 NSPR_API(PRStatus
) PR_Sleep(PRIntervalTime ticks
);
270 ** Get the scoping of this thread.
272 NSPR_API(PRThreadScope
) PR_GetThreadScope(const PRThread
*thread
);
275 ** Get the type of this thread.
277 NSPR_API(PRThreadType
) PR_GetThreadType(const PRThread
*thread
);
280 ** Get the join state of this thread.
282 NSPR_API(PRThreadState
) PR_GetThreadState(const PRThread
*thread
);
286 #endif /* prthread_h___ */