1 | /** @file
|
---|
2 | * innotek Portable Runtime - Threads.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License as published by the Free Software Foundation,
|
---|
12 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
13 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
14 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * If you received this file as part of a commercial VirtualBox
|
---|
17 | * distribution, then only the terms of your commercial VirtualBox
|
---|
18 | * license agreement apply instead of the previous paragraph.
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef ___iprt_thread_h
|
---|
22 | #define ___iprt_thread_h
|
---|
23 |
|
---|
24 | #include <iprt/cdefs.h>
|
---|
25 | #include <iprt/types.h>
|
---|
26 |
|
---|
27 | #ifdef IN_GC
|
---|
28 | # error "There are no threading APIs available Guest Context!"
|
---|
29 | #endif
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 | __BEGIN_DECLS
|
---|
34 |
|
---|
35 | /** @defgroup grp_rt_thread RTThread - Thread Management
|
---|
36 | * @ingroup grp_rt
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Get the thread handle of the current thread.
|
---|
42 | *
|
---|
43 | * @returns Thread handle.
|
---|
44 | */
|
---|
45 | RTDECL(RTTHREAD) RTThreadSelf(void);
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Get the native thread handle of the current thread.
|
---|
49 | *
|
---|
50 | * @returns Native thread handle.
|
---|
51 | */
|
---|
52 | RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void);
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Millisecond granular sleep function.
|
---|
56 | *
|
---|
57 | * @returns VINF_SUCCESS on success.
|
---|
58 | * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happend
|
---|
59 | * which interrupt the peaceful sleep.
|
---|
60 | * @param cMillies Number of milliseconds to sleep.
|
---|
61 | * 0 milliseconds means yielding the timeslice - deprecated!
|
---|
62 | * @remark See RTThreadNanoSleep() for sleeping for smaller periods of time.
|
---|
63 | */
|
---|
64 | RTDECL(int) RTThreadSleep(unsigned cMillies);
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Yields the CPU.
|
---|
68 | *
|
---|
69 | * @returns true if we yielded.
|
---|
70 | * @returns false if it's probable that we didn't yield.
|
---|
71 | */
|
---|
72 | RTDECL(bool) RTThreadYield(void);
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Thread function.
|
---|
78 | *
|
---|
79 | * @returns 0 on success.
|
---|
80 | * @param ThreadSelf Thread handle to this thread.
|
---|
81 | * @param pvUser User argument.
|
---|
82 | */
|
---|
83 | typedef DECLCALLBACK(int) FNRTTHREAD(RTTHREAD ThreadSelf, void *pvUser);
|
---|
84 | /** Pointer to a FNRTTHREAD(). */
|
---|
85 | typedef FNRTTHREAD *PFNRTTHREAD;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Thread types.
|
---|
89 | * Besides identifying the purpose of the thread, the thread type is
|
---|
90 | * used to select the scheduling properties.
|
---|
91 | *
|
---|
92 | * The types in are placed in a rough order of ascending priority.
|
---|
93 | */
|
---|
94 | typedef enum RTTHREADTYPE
|
---|
95 | {
|
---|
96 | /** Invalid type. */
|
---|
97 | RTTHREADTYPE_INVALID = 0,
|
---|
98 | /** Infrequent poller thread.
|
---|
99 | * This type of thread will sleep for the most of the time, and do
|
---|
100 | * infrequent polls on resources at 0.5 sec or higher intervals.
|
---|
101 | */
|
---|
102 | RTTHREADTYPE_INFREQUENT_POLLER,
|
---|
103 | /** Main heavy worker thread.
|
---|
104 | * Thread of this type is driving asynchronous tasks in the Main
|
---|
105 | * API which takes a long time and might involve a bit of CPU. Like
|
---|
106 | * for instance creating a fixed sized VDI.
|
---|
107 | */
|
---|
108 | RTTHREADTYPE_MAIN_HEAVY_WORKER,
|
---|
109 | /** The emulation thread type.
|
---|
110 | * While being a thread with very high workload it still is vital
|
---|
111 | * that it gets scheduled frequently. When possible all other thread
|
---|
112 | * types except DEFAULT and GUI should interrupt this one ASAP when
|
---|
113 | * they become ready.
|
---|
114 | */
|
---|
115 | RTTHREADTYPE_EMULATION,
|
---|
116 | /** The default thread type.
|
---|
117 | * Since it doesn't say much about the purpose of the thread
|
---|
118 | * nothing special is normally done to the scheduling. This type
|
---|
119 | * should be avoided.
|
---|
120 | * The main thread is registered with default type during RTR3Init()
|
---|
121 | * and that's what the default process priority is derived from.
|
---|
122 | */
|
---|
123 | RTTHREADTYPE_DEFAULT,
|
---|
124 | /** The GUI thread type
|
---|
125 | * The GUI normally have a low workload but is frequently scheduled
|
---|
126 | * to handle events. When possible the scheduler should not leave
|
---|
127 | * threads of this kind waiting for too long (~50ms).
|
---|
128 | */
|
---|
129 | RTTHREADTYPE_GUI,
|
---|
130 | /** Main worker thread.
|
---|
131 | * Thread of this type is driving asynchronous tasks in the Main API.
|
---|
132 | * In most cases this means little work an a lot of waiting.
|
---|
133 | */
|
---|
134 | RTTHREADTYPE_MAIN_WORKER,
|
---|
135 | /** VRDP I/O thread.
|
---|
136 | * These threads are I/O threads in the RDP server will hang around
|
---|
137 | * waiting for data, process it and pass it on.
|
---|
138 | */
|
---|
139 | RTTHREADTYPE_VRDP_IO,
|
---|
140 | /** The debugger type.
|
---|
141 | * Threads involved in servicing the debugger. It must remain
|
---|
142 | * responsive even when things are running wild in.
|
---|
143 | */
|
---|
144 | RTTHREADTYPE_DEBUGGER,
|
---|
145 | /** Message pump thread.
|
---|
146 | * Thread pumping messages from one thread/process to another
|
---|
147 | * thread/process. The workload is very small, most of the time
|
---|
148 | * it's blocked waiting for messages to be procduced or processed.
|
---|
149 | * This type of thread will be favored after I/O threads.
|
---|
150 | */
|
---|
151 | RTTHREADTYPE_MSG_PUMP,
|
---|
152 | /** The I/O thread type.
|
---|
153 | * Doing I/O means shuffling data, waiting for request to arrive and
|
---|
154 | * for them to complete. The thread should be favored when competing
|
---|
155 | * with any other threads except timer threads.
|
---|
156 | */
|
---|
157 | RTTHREADTYPE_IO,
|
---|
158 | /** The timer thread type.
|
---|
159 | * A timer thread is mostly waiting for the timer to tick
|
---|
160 | * and then perform a little bit of work. Accuracy is important here,
|
---|
161 | * so the thread should be favoured over all threads. If premention can
|
---|
162 | * be configured at thread level, it could be made very short.
|
---|
163 | */
|
---|
164 | RTTHREADTYPE_TIMER,
|
---|
165 | /** Only used for validation. */
|
---|
166 | RTTHREADTYPE_END
|
---|
167 | } RTTHREADTYPE;
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Thread creation flags.
|
---|
172 | */
|
---|
173 | typedef enum RTTHREADFLAGS
|
---|
174 | {
|
---|
175 | /**
|
---|
176 | * This flag is used to keep the thread structure around so it can
|
---|
177 | * be waited on after termination.
|
---|
178 | */
|
---|
179 | RTTHREADFLAGS_WAITABLE = BIT(0),
|
---|
180 | /** The bit number corresponding to the RTTHREADFLAGS_WAITABLE mask. */
|
---|
181 | RTTHREADFLAGS_WAITABLE_BIT = 0,
|
---|
182 |
|
---|
183 | /** Mask of valid flags, use for validation. */
|
---|
184 | RTTHREADFLAGS_MASK = BIT(0)
|
---|
185 | } RTTHREADFLAGS;
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Create a new thread.
|
---|
190 | *
|
---|
191 | * @returns iprt status code.
|
---|
192 | * @param pThread Where to store the thread handle to the new thread. (optional)
|
---|
193 | * @param pfnThread The thread function.
|
---|
194 | * @param pvUser User argument.
|
---|
195 | * @param cbStack The size of the stack for the new thread.
|
---|
196 | * Use 0 for the default stack size.
|
---|
197 | * @param enmType The thread type. Used for deciding scheduling attributes
|
---|
198 | * of the thread.
|
---|
199 | * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
|
---|
200 | * @param pszName Thread name.
|
---|
201 | *
|
---|
202 | * @remark When called in Ring-0, this API will create a new kernel thread and not a thread in
|
---|
203 | * the context of the calling process.
|
---|
204 | */
|
---|
205 | RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
|
---|
206 | RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Gets the native thread id of a IPRT thread.
|
---|
210 | *
|
---|
211 | * @returns The native thread id.
|
---|
212 | * @param Thread The IPRT thread.
|
---|
213 | */
|
---|
214 | RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Gets the IPRT thread of a native thread.
|
---|
218 | *
|
---|
219 | * @returns The IPRT thread handle
|
---|
220 | * @returns NIL_RTTHREAD if not a thread known to IPRT.
|
---|
221 | * @param NativeThread The native thread handle/id.
|
---|
222 | */
|
---|
223 | RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Changes the type of the specified thread.
|
---|
227 | *
|
---|
228 | * @returns iprt status code.
|
---|
229 | * @param Thread The thread which type should be changed.
|
---|
230 | * @param enmType The new thread type.
|
---|
231 | * @remark In Ring-0 it only works if Thread == RTThreadSelf().
|
---|
232 | */
|
---|
233 | RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Wait for the thread to terminate, resume on interruption.
|
---|
237 | *
|
---|
238 | * @returns iprt status code.
|
---|
239 | * Will not return VERR_INTERRUPTED.
|
---|
240 | * @param Thread The thread to wait for.
|
---|
241 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
242 | * an indefinite wait.
|
---|
243 | * @param prc Where to store the return code of the thread. Optional.
|
---|
244 | */
|
---|
245 | RTDECL(int) RTThreadWait(RTTHREAD Thread, unsigned cMillies, int *prc);
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Wait for the thread to terminate, return on interruption.
|
---|
249 | *
|
---|
250 | * @returns iprt status code.
|
---|
251 | * @param Thread The thread to wait for.
|
---|
252 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
253 | * an indefinite wait.
|
---|
254 | * @param prc Where to store the return code of the thread. Optional.
|
---|
255 | */
|
---|
256 | RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, unsigned cMillies, int *prc);
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Gets the name of the current thread thread.
|
---|
260 | *
|
---|
261 | * @returns Pointer to readonly name string.
|
---|
262 | * @returns NULL on failure.
|
---|
263 | */
|
---|
264 | RTDECL(const char *) RTThreadSelfName(void);
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Gets the name of a thread.
|
---|
268 | *
|
---|
269 | * @returns Pointer to readonly name string.
|
---|
270 | * @returns NULL on failure.
|
---|
271 | * @param Thread Thread handle of the thread to query the name of.
|
---|
272 | */
|
---|
273 | RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Gets the type of the specified thread.
|
---|
277 | *
|
---|
278 | * @returns The thread type.
|
---|
279 | * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
|
---|
280 | * @param Thread The thread in question.
|
---|
281 | */
|
---|
282 | RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Sets the name of a thread.
|
---|
286 | *
|
---|
287 | * @returns iprt status code.
|
---|
288 | * @param Thread Thread handle of the thread to query the name of.
|
---|
289 | * @param pszName The thread name.
|
---|
290 | */
|
---|
291 | RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Signal the user event.
|
---|
295 | *
|
---|
296 | * @returns iprt status code.
|
---|
297 | */
|
---|
298 | RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Wait for the user event.
|
---|
302 | *
|
---|
303 | * @returns iprt status code.
|
---|
304 | * @param Thread The thread to wait for.
|
---|
305 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
306 | * an indefinite wait.
|
---|
307 | */
|
---|
308 | RTDECL(int) RTThreadUserWait(RTTHREAD Thread, unsigned cMillies);
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Wait for the user event, return on interruption.
|
---|
312 | *
|
---|
313 | * @returns iprt status code.
|
---|
314 | * @param Thread The thread to wait for.
|
---|
315 | * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
|
---|
316 | * an indefinite wait.
|
---|
317 | */
|
---|
318 | RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, unsigned cMillies);
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Reset the user event.
|
---|
322 | *
|
---|
323 | * @returns iprt status code.
|
---|
324 | * @param Thread The thread to reset.
|
---|
325 | */
|
---|
326 | RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
|
---|
327 |
|
---|
328 |
|
---|
329 | #ifdef IN_RING3
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Adopts a non-IPRT thread.
|
---|
333 | *
|
---|
334 | * @returns IPRT status code.
|
---|
335 | * @param enmType The thread type.
|
---|
336 | * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
|
---|
337 | * @param pszName The thread name. Optional
|
---|
338 | * @param pThread Where to store the thread handle. Optional.
|
---|
339 | */
|
---|
340 | RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Gets the affinity mask of the current thread.
|
---|
344 | *
|
---|
345 | * @returns The affinity mask (bit 0 = logical cpu 0).
|
---|
346 | */
|
---|
347 | RTR3DECL(uint64_t) RTThreadGetAffinity(void);
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Sets the affinity mask of the current thread.
|
---|
351 | *
|
---|
352 | * @returns iprt status code.
|
---|
353 | * @param u64Mask Affinity mask (bit 0 = logical cpu 0).
|
---|
354 | */
|
---|
355 | RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask);
|
---|
356 |
|
---|
357 | #endif /* IN_RING3 */
|
---|
358 |
|
---|
359 | /** @} */
|
---|
360 |
|
---|
361 | __END_DECLS
|
---|
362 |
|
---|
363 | #endif
|
---|