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