VirtualBox

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

Last change on this file since 4136 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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