xref: /aoo41x/main/sal/inc/osl/thread.h (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _OSL_THREAD_H_
29*cdf0e10cSrcweir #define _OSL_THREAD_H_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <osl/time.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifndef _RTL_TEXTENC_H_
34*cdf0e10cSrcweir #	include <rtl/textenc.h>
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #ifdef __cplusplus
38*cdf0e10cSrcweir extern "C" {
39*cdf0e10cSrcweir #endif
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir /**
42*cdf0e10cSrcweir 	Opaque data type for threads. As with all other osl-handles
43*cdf0e10cSrcweir 	you can initialize and/or test it to/for 0.
44*cdf0e10cSrcweir */
45*cdf0e10cSrcweir typedef void* oslThread;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir /** the function-ptr. representing the threads worker-function.
48*cdf0e10cSrcweir */
49*cdf0e10cSrcweir typedef void (SAL_CALL *oslWorkerFunction)(void*);
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir /** levels of thread-priority
52*cdf0e10cSrcweir 	Note that oslThreadPriorityUnknown might be returned
53*cdf0e10cSrcweir 	by getPriorityOfThread() (e.g. when it is terminated),
54*cdf0e10cSrcweir 	but mustn't be used with setPriority()!
55*cdf0e10cSrcweir */
56*cdf0e10cSrcweir typedef enum
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 	osl_Thread_PriorityHighest,
59*cdf0e10cSrcweir 	osl_Thread_PriorityAboveNormal,
60*cdf0e10cSrcweir 	osl_Thread_PriorityNormal,
61*cdf0e10cSrcweir 	osl_Thread_PriorityBelowNormal,
62*cdf0e10cSrcweir 	osl_Thread_PriorityLowest,
63*cdf0e10cSrcweir 	osl_Thread_PriorityUnknown,			/* don't use to set */
64*cdf0e10cSrcweir 	osl_Thread_Priority_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
65*cdf0e10cSrcweir } oslThreadPriority;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir typedef sal_uInt32 oslThreadIdentifier;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir typedef sal_uInt32 oslThreadKey;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir /** Create the thread, using the function-ptr pWorker as
73*cdf0e10cSrcweir 	its main (worker) function. This functions receives in
74*cdf0e10cSrcweir 	its void* parameter the value supplied by pThreadData.
75*cdf0e10cSrcweir 	Once the OS-structures are initialized,the thread starts
76*cdf0e10cSrcweir 	running.
77*cdf0e10cSrcweir 	@return 0 if creation failed, otherwise a handle to the thread
78*cdf0e10cSrcweir */
79*cdf0e10cSrcweir oslThread SAL_CALL osl_createThread(oslWorkerFunction pWorker, void* pThreadData);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir /** Create the thread, using the function-ptr pWorker as
82*cdf0e10cSrcweir 	its main (worker) function. This functions receives in
83*cdf0e10cSrcweir 	its void* parameter the value supplied by pThreadData.
84*cdf0e10cSrcweir 	The thread will be created, but it won't start running.
85*cdf0e10cSrcweir 	To wake-up the thread, use resume().
86*cdf0e10cSrcweir 	@return 0 if creation failed, otherwise a handle to the thread
87*cdf0e10cSrcweir */
88*cdf0e10cSrcweir oslThread SAL_CALL osl_createSuspendedThread(oslWorkerFunction pWorker, void* pThreadData);
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir /** Get the identifier for the specified thread or if parameter
91*cdf0e10cSrcweir     Thread is NULL of the current active thread.
92*cdf0e10cSrcweir 	@return identifier of the thread
93*cdf0e10cSrcweir */
94*cdf0e10cSrcweir oslThreadIdentifier SAL_CALL osl_getThreadIdentifier(oslThread Thread);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir /** Release the thread handle.
97*cdf0e10cSrcweir 	If Thread is NULL, the function won't do anything.
98*cdf0e10cSrcweir 	Note that we do not interfere with the actual running of
99*cdf0e10cSrcweir 	the thread, we just free up the memory needed by the handle.
100*cdf0e10cSrcweir */
101*cdf0e10cSrcweir void SAL_CALL osl_destroyThread(oslThread Thread);
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir /** Wake-up a thread that was suspended with suspend() or
104*cdf0e10cSrcweir 	createSuspended(). The oslThread must be valid!
105*cdf0e10cSrcweir */
106*cdf0e10cSrcweir void SAL_CALL osl_resumeThread(oslThread Thread);
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir /** Suspend the execution of the thread. If you want the thread
109*cdf0e10cSrcweir 	to continue, call resume(). The oslThread must be valid!
110*cdf0e10cSrcweir */
111*cdf0e10cSrcweir void SAL_CALL osl_suspendThread(oslThread Thread);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir /** Changes the threads priority.
114*cdf0e10cSrcweir 	The oslThread must be valid!
115*cdf0e10cSrcweir */
116*cdf0e10cSrcweir void SAL_CALL osl_setThreadPriority(oslThread Thread, oslThreadPriority Priority);
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir /** Retrieves the threads priority.
119*cdf0e10cSrcweir 	Returns oslThreadPriorityUnknown for invalid Thread-argument or
120*cdf0e10cSrcweir 	terminated thread. (I.e.: The oslThread might be invalid.)
121*cdf0e10cSrcweir */
122*cdf0e10cSrcweir oslThreadPriority SAL_CALL osl_getThreadPriority(const oslThread Thread);
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir /** Returns True if the thread was created and has not terminated yet.
125*cdf0e10cSrcweir 	Note that according to this definition a "running" thread might be
126*cdf0e10cSrcweir 	suspended! Also returns False is Thread is NULL.
127*cdf0e10cSrcweir */
128*cdf0e10cSrcweir sal_Bool SAL_CALL osl_isThreadRunning(const oslThread Thread);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir /** Blocks the calling thread until Thread has terminated.
131*cdf0e10cSrcweir 	Returns immediately if Thread is NULL.
132*cdf0e10cSrcweir */
133*cdf0e10cSrcweir void SAL_CALL osl_joinWithThread(oslThread Thread);
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir /** Blocks the calling thread at least for the given number
136*cdf0e10cSrcweir     of time.
137*cdf0e10cSrcweir */
138*cdf0e10cSrcweir void SAL_CALL osl_waitThread(const TimeValue* pDelay);
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir /** The requested thread will get terminate the next time
141*cdf0e10cSrcweir 	scheduleThread() is called.
142*cdf0e10cSrcweir */
143*cdf0e10cSrcweir void SAL_CALL osl_terminateThread(oslThread Thread);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir /** Offers the rest of the threads time-slice to the OS.
146*cdf0e10cSrcweir 	scheduleThread() should be called in the working loop
147*cdf0e10cSrcweir 	of the thread, so any other thread could also get the
148*cdf0e10cSrcweir 	processor. Returns False if the thread should terminate, so
149*cdf0e10cSrcweir 	the thread could free any allocated resources.
150*cdf0e10cSrcweir */
151*cdf0e10cSrcweir sal_Bool SAL_CALL osl_scheduleThread(oslThread Thread);
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir /** Offers the rest of the threads time-slice to the OS.
154*cdf0e10cSrcweir 	Under POSIX you _need_ to yield(), otherwise, since the
155*cdf0e10cSrcweir 	threads are not preempted during execution, NO other thread
156*cdf0e10cSrcweir 	(even with higher priority) gets the processor. Control is
157*cdf0e10cSrcweir 	only given to another thread if the current thread blocks
158*cdf0e10cSrcweir 	or uses yield().
159*cdf0e10cSrcweir */
160*cdf0e10cSrcweir void SAL_CALL osl_yieldThread(void);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir /** Attempts to set the name of the current thread.
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     The name of a thread is usually evaluated for debugging purposes.  Not all
165*cdf0e10cSrcweir     platforms support this.  On Linux, a set thread name can be observed with
166*cdf0e10cSrcweir     "ps -L".  On Windows with the Microsoft compiler, a thread name set while a
167*cdf0e10cSrcweir     debugger is attached can be observed within the debugger.
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     @param name  the name of the thread; must not be null; on Linux, only the
170*cdf0e10cSrcweir     first 16 characters are used
171*cdf0e10cSrcweir */
172*cdf0e10cSrcweir void SAL_CALL osl_setThreadName(char const * name);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir /* Callback when data stored in a thread key is no longer needed */
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir typedef void (SAL_CALL *oslThreadKeyCallbackFunction)(void *);
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir /** Create a key to an associated thread local storage pointer. */
179*cdf0e10cSrcweir oslThreadKey SAL_CALL osl_createThreadKey(oslThreadKeyCallbackFunction pCallback);
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir /** Destroy a key to an associated thread local storage pointer. */
182*cdf0e10cSrcweir void SAL_CALL osl_destroyThreadKey(oslThreadKey Key);
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir /** Get to key associated thread specific data. */
185*cdf0e10cSrcweir void* SAL_CALL osl_getThreadKeyData(oslThreadKey Key);
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir /** Set to key associated thread specific data. */
188*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setThreadKeyData(oslThreadKey Key, void *pData);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir /** Get the current thread local text encoding. */
191*cdf0e10cSrcweir rtl_TextEncoding SAL_CALL osl_getThreadTextEncoding(void);
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir /** Set the thread local text encoding.
194*cdf0e10cSrcweir 	@return the old text encoding.
195*cdf0e10cSrcweir */
196*cdf0e10cSrcweir rtl_TextEncoding SAL_CALL osl_setThreadTextEncoding(rtl_TextEncoding Encoding);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir #ifdef __cplusplus
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir #endif
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir #endif	/* _OSL_THREAD_H_ */
203*cdf0e10cSrcweir 
204