VirtualBox

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

Last change on this file since 6981 was 6956, checked in by vboxsync, 17 years ago

Moved RTTLS to iprt/types.h (from thread.h)

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