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