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