VirtualBox

source: vbox/trunk/include/iprt/thread.h@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
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 */
46RTDECL(RTTHREAD) RTThreadSelf(void);
47
48/**
49 * Get the native thread handle of the current thread.
50 *
51 * @returns Native thread handle.
52 */
53RTDECL(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 */
65RTDECL(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 */
73RTDECL(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 */
84typedef DECLCALLBACK(int) FNRTTHREAD(RTTHREAD ThreadSelf, void *pvUser);
85/** Pointer to a FNRTTHREAD(). */
86typedef 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 rought order of ascending priority.
94 */
95typedef 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_LAST
168} RTTHREADTYPE;
169
170
171/**
172 * Thread creation flags.
173 */
174typedef 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 */
206RTDECL(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 */
215RTDECL(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 */
224RTDECL(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 */
234RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
235
236#ifdef IN_RING3
237/**
238 * Gets the type of the specified thread.
239 *
240 * @returns The thread type.
241 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
242 * @param Thread The thread in question.
243 */
244RTR3DECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
245
246/**
247 * Adopts a non-IPRT thread.
248 *
249 * @returns IPRT status code.
250 * @param enmType The thread type.
251 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
252 * @param pszName The thread name. Optional
253 * @param pThread Where to store the thread handle. Optional.
254 */
255RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
256
257/**
258 * Gets the name of the current thread thread.
259 *
260 * @returns Pointer to readonly name string.
261 * @returns NULL on failure.
262 */
263RTR3DECL(const char *) RTThreadSelfName(void);
264
265/**
266 * Gets the name of a thread.
267 *
268 * @returns Pointer to readonly name string.
269 * @returns NULL on failure.
270 * @param Thread Thread handle of the thread to query the name of.
271 */
272RTR3DECL(const char *) RTThreadGetName(RTTHREAD Thread);
273
274/**
275 * Sets the name of a thread.
276 *
277 * @returns iprt status code.
278 * @param Thread Thread handle of the thread to query the name of.
279 * @param pszName The thread name.
280 */
281RTR3DECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
282
283/**
284 * Signal the user event.
285 *
286 * @returns iprt status code.
287 */
288RTR3DECL(int) RTThreadUserSignal(RTTHREAD Thread);
289
290/**
291 * Wait for the user event.
292 *
293 * @returns iprt status code.
294 * @param Thread The thread to wait for.
295 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
296 * an indefinite wait.
297 */
298RTR3DECL(int) RTThreadUserWait(RTTHREAD Thread, unsigned cMillies);
299
300/**
301 * Wait for the user event, return on interruption.
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 */
308RTR3DECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, unsigned cMillies);
309
310/**
311 * Reset the user event.
312 *
313 * @returns iprt status code.
314 * @param Thread The thread to reset.
315 */
316RTR3DECL(int) RTThreadUserReset(RTTHREAD Thread);
317
318/**
319 * Wait for the thread to terminate, resume on interruption.
320 *
321 * @returns iprt status code.
322 * Will not return VERR_INTERRUPTED.
323 * @param Thread The thread to wait for.
324 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
325 * an indefinite wait.
326 * @param prc Where to store the return code of the thread. Optional.
327 */
328RTR3DECL(int) RTThreadWait(RTTHREAD Thread, unsigned cMillies, int *prc);
329
330/**
331 * Wait for the thread to terminate, return on interruption.
332 *
333 * @returns iprt status code.
334 * @param Thread The thread to wait for.
335 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
336 * an indefinite wait.
337 * @param prc Where to store the return code of the thread. Optional.
338 */
339RTR3DECL(int) RTThreadWaitNoResume(RTTHREAD Thread, unsigned cMillies, int *prc);
340
341/**
342 * Gets the affinity mask of the current thread.
343 *
344 * @returns The affinity mask (bit 0 = logical cpu 0).
345 */
346RTR3DECL(uint64_t) RTThreadGetAffinity(void);
347
348/**
349 * Sets the affinity mask of the current thread.
350 *
351 * @returns iprt status code.
352 * @param u64Mask Affinity mask (bit 0 = logical cpu 0).
353 */
354RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask);
355#endif /* IN_RING3 */
356
357/** @} */
358
359__END_DECLS
360
361#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette