VirtualBox

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

Last change on this file since 12822 was 11596, checked in by vboxsync, 16 years ago

iprt: Added RTThreadCreateV and RTThreadCreateF which takes a format string and arguments for the thread name.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1/** @file
2 * IPRT - Threads.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_thread_h
31#define ___iprt_thread_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35#include <iprt/stdarg.h>
36
37#ifdef IN_GC
38# error "There are no threading APIs available Guest Context!"
39#endif
40
41
42
43__BEGIN_DECLS
44
45/** @defgroup grp_rt_thread RTThread - Thread Management
46 * @ingroup grp_rt
47 * @{
48 */
49
50/**
51 * Get the thread handle of the current thread.
52 *
53 * @returns Thread handle.
54 */
55RTDECL(RTTHREAD) RTThreadSelf(void);
56
57/**
58 * Get the native thread handle of the current thread.
59 *
60 * @returns Native thread handle.
61 */
62RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void);
63
64/**
65 * Millisecond granular sleep function.
66 *
67 * @returns VINF_SUCCESS on success.
68 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happend
69 * which interrupt the peaceful sleep.
70 * @param cMillies Number of milliseconds to sleep.
71 * 0 milliseconds means yielding the timeslice - deprecated!
72 * @remark See RTThreadNanoSleep() for sleeping for smaller periods of time.
73 */
74RTDECL(int) RTThreadSleep(unsigned cMillies);
75
76/**
77 * Yields the CPU.
78 *
79 * @returns true if we yielded.
80 * @returns false if it's probable that we didn't yield.
81 */
82RTDECL(bool) RTThreadYield(void);
83
84
85
86/**
87 * Thread function.
88 *
89 * @returns 0 on success.
90 * @param ThreadSelf Thread handle to this thread.
91 * @param pvUser User argument.
92 */
93typedef DECLCALLBACK(int) FNRTTHREAD(RTTHREAD ThreadSelf, void *pvUser);
94/** Pointer to a FNRTTHREAD(). */
95typedef FNRTTHREAD *PFNRTTHREAD;
96
97/**
98 * Thread types.
99 * Besides identifying the purpose of the thread, the thread type is
100 * used to select the scheduling properties.
101 *
102 * The types in are placed in a rough order of ascending priority.
103 */
104typedef enum RTTHREADTYPE
105{
106 /** Invalid type. */
107 RTTHREADTYPE_INVALID = 0,
108 /** Infrequent poller thread.
109 * This type of thread will sleep for the most of the time, and do
110 * infrequent polls on resources at 0.5 sec or higher intervals.
111 */
112 RTTHREADTYPE_INFREQUENT_POLLER,
113 /** Main heavy worker thread.
114 * Thread of this type is driving asynchronous tasks in the Main
115 * API which takes a long time and might involve a bit of CPU. Like
116 * for instance creating a fixed sized VDI.
117 */
118 RTTHREADTYPE_MAIN_HEAVY_WORKER,
119 /** The emulation thread type.
120 * While being a thread with very high workload it still is vital
121 * that it gets scheduled frequently. When possible all other thread
122 * types except DEFAULT and GUI should interrupt this one ASAP when
123 * they become ready.
124 */
125 RTTHREADTYPE_EMULATION,
126 /** The default thread type.
127 * Since it doesn't say much about the purpose of the thread
128 * nothing special is normally done to the scheduling. This type
129 * should be avoided.
130 * The main thread is registered with default type during RTR3Init()
131 * and that's what the default process priority is derived from.
132 */
133 RTTHREADTYPE_DEFAULT,
134 /** The GUI thread type
135 * The GUI normally have a low workload but is frequently scheduled
136 * to handle events. When possible the scheduler should not leave
137 * threads of this kind waiting for too long (~50ms).
138 */
139 RTTHREADTYPE_GUI,
140 /** Main worker thread.
141 * Thread of this type is driving asynchronous tasks in the Main API.
142 * In most cases this means little work an a lot of waiting.
143 */
144 RTTHREADTYPE_MAIN_WORKER,
145 /** VRDP I/O thread.
146 * These threads are I/O threads in the RDP server will hang around
147 * waiting for data, process it and pass it on.
148 */
149 RTTHREADTYPE_VRDP_IO,
150 /** The debugger type.
151 * Threads involved in servicing the debugger. It must remain
152 * responsive even when things are running wild in.
153 */
154 RTTHREADTYPE_DEBUGGER,
155 /** Message pump thread.
156 * Thread pumping messages from one thread/process to another
157 * thread/process. The workload is very small, most of the time
158 * it's blocked waiting for messages to be procduced or processed.
159 * This type of thread will be favored after I/O threads.
160 */
161 RTTHREADTYPE_MSG_PUMP,
162 /** The I/O thread type.
163 * Doing I/O means shuffling data, waiting for request to arrive and
164 * for them to complete. The thread should be favored when competing
165 * with any other threads except timer threads.
166 */
167 RTTHREADTYPE_IO,
168 /** The timer thread type.
169 * A timer thread is mostly waiting for the timer to tick
170 * and then perform a little bit of work. Accuracy is important here,
171 * so the thread should be favoured over all threads. If premention can
172 * be configured at thread level, it could be made very short.
173 */
174 RTTHREADTYPE_TIMER,
175 /** Only used for validation. */
176 RTTHREADTYPE_END
177} RTTHREADTYPE;
178
179
180/**
181 * Thread creation flags.
182 */
183typedef enum RTTHREADFLAGS
184{
185 /**
186 * This flag is used to keep the thread structure around so it can
187 * be waited on after termination.
188 */
189 RTTHREADFLAGS_WAITABLE = RT_BIT(0),
190 /** The bit number corresponding to the RTTHREADFLAGS_WAITABLE mask. */
191 RTTHREADFLAGS_WAITABLE_BIT = 0,
192
193 /** Mask of valid flags, use for validation. */
194 RTTHREADFLAGS_MASK = RT_BIT(0)
195} RTTHREADFLAGS;
196
197
198/**
199 * Create a new thread.
200 *
201 * @returns iprt status code.
202 * @param pThread Where to store the thread handle to the new thread. (optional)
203 * @param pfnThread The thread function.
204 * @param pvUser User argument.
205 * @param cbStack The size of the stack for the new thread.
206 * Use 0 for the default stack size.
207 * @param enmType The thread type. Used for deciding scheduling attributes
208 * of the thread.
209 * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
210 * @param pszName Thread name.
211 *
212 * @remark When called in Ring-0, this API will create a new kernel thread and not a thread in
213 * the context of the calling process.
214 */
215RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
216 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
217
218/**
219 * Create a new thread.
220 *
221 * Same as RTThreadCreate except the name is given in the RTStrPrintfV form.
222 *
223 * @returns iprt status code.
224 * @param pThread See RTThreadCreate.
225 * @param pfnThread See RTThreadCreate.
226 * @param pvUser See RTThreadCreate.
227 * @param cbStack See RTThreadCreate.
228 * @param enmType See RTThreadCreate.
229 * @param fFlags See RTThreadCreate.
230 * @param pszName Thread name format.
231 * @param va Format arguments.
232 */
233RTDECL(int) RTThreadCreateV(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
234 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, va_list va);
235
236/**
237 * Create a new thread.
238 *
239 * Same as RTThreadCreate except the name is given in the RTStrPrintf form.
240 *
241 * @returns iprt status code.
242 * @param pThread See RTThreadCreate.
243 * @param pfnThread See RTThreadCreate.
244 * @param pvUser See RTThreadCreate.
245 * @param cbStack See RTThreadCreate.
246 * @param enmType See RTThreadCreate.
247 * @param fFlags See RTThreadCreate.
248 * @param pszName Thread name format.
249 * @param ... Format arguments.
250 */
251RTDECL(int) RTThreadCreateF(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
252 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, ...);
253
254/**
255 * Gets the native thread id of a IPRT thread.
256 *
257 * @returns The native thread id.
258 * @param Thread The IPRT thread.
259 */
260RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
261
262/**
263 * Gets the IPRT thread of a native thread.
264 *
265 * @returns The IPRT thread handle
266 * @returns NIL_RTTHREAD if not a thread known to IPRT.
267 * @param NativeThread The native thread handle/id.
268 */
269RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
270
271/**
272 * Changes the type of the specified thread.
273 *
274 * @returns iprt status code.
275 * @param Thread The thread which type should be changed.
276 * @param enmType The new thread type.
277 * @remark In Ring-0 it only works if Thread == RTThreadSelf().
278 */
279RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
280
281/**
282 * Wait for the thread to terminate, resume on interruption.
283 *
284 * @returns iprt status code.
285 * Will not return VERR_INTERRUPTED.
286 * @param Thread The thread to wait for.
287 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
288 * an indefinite wait.
289 * @param prc Where to store the return code of the thread. Optional.
290 */
291RTDECL(int) RTThreadWait(RTTHREAD Thread, unsigned cMillies, int *prc);
292
293/**
294 * Wait for the thread to terminate, return on interruption.
295 *
296 * @returns iprt status code.
297 * @param Thread The thread to wait for.
298 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
299 * an indefinite wait.
300 * @param prc Where to store the return code of the thread. Optional.
301 */
302RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, unsigned cMillies, int *prc);
303
304/**
305 * Gets the name of the current thread thread.
306 *
307 * @returns Pointer to readonly name string.
308 * @returns NULL on failure.
309 */
310RTDECL(const char *) RTThreadSelfName(void);
311
312/**
313 * Gets the name of a thread.
314 *
315 * @returns Pointer to readonly name string.
316 * @returns NULL on failure.
317 * @param Thread Thread handle of the thread to query the name of.
318 */
319RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
320
321/**
322 * Gets the type of the specified thread.
323 *
324 * @returns The thread type.
325 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
326 * @param Thread The thread in question.
327 */
328RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
329
330/**
331 * Sets the name of a thread.
332 *
333 * @returns iprt status code.
334 * @param Thread Thread handle of the thread to query the name of.
335 * @param pszName The thread name.
336 */
337RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
338
339/**
340 * Signal the user event.
341 *
342 * @returns iprt status code.
343 */
344RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
345
346/**
347 * Wait for the user event.
348 *
349 * @returns iprt status code.
350 * @param Thread The thread to wait for.
351 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
352 * an indefinite wait.
353 */
354RTDECL(int) RTThreadUserWait(RTTHREAD Thread, unsigned cMillies);
355
356/**
357 * Wait for the user event, return on interruption.
358 *
359 * @returns iprt status code.
360 * @param Thread The thread to wait for.
361 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
362 * an indefinite wait.
363 */
364RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, unsigned cMillies);
365
366/**
367 * Reset the user event.
368 *
369 * @returns iprt status code.
370 * @param Thread The thread to reset.
371 */
372RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
373
374
375#ifdef IN_RING3
376
377/**
378 * Adopts a non-IPRT thread.
379 *
380 * @returns IPRT status code.
381 * @param enmType The thread type.
382 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
383 * @param pszName The thread name. Optional
384 * @param pThread Where to store the thread handle. Optional.
385 */
386RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
387
388/**
389 * Gets the affinity mask of the current thread.
390 *
391 * @returns The affinity mask (bit 0 = logical cpu 0).
392 */
393RTR3DECL(uint64_t) RTThreadGetAffinity(void);
394
395/**
396 * Sets the affinity mask of the current thread.
397 *
398 * @returns iprt status code.
399 * @param u64Mask Affinity mask (bit 0 = logical cpu 0).
400 */
401RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask);
402
403/**
404 * Gets the number of write locks and critical sections the specified
405 * thread owns.
406 *
407 * This number does not include any nested lock/critect entries.
408 *
409 * Note that it probably will return 0 for non-strict builds since
410 * release builds doesn't do unnecessary diagnostic counting like this.
411 *
412 * @returns Number of locks on success (0+) and VERR_INVALID_HANDLER on failure
413 * @param Thread The thread we're inquiring about.
414 * @remarks Will only work for strict builds.
415 */
416RTDECL(int32_t) RTThreadGetWriteLockCount(RTTHREAD Thread);
417
418/**
419 * Works the THREADINT::cWriteLocks member, mostly internal.
420 *
421 * @param Thread The current thread.
422 */
423RTDECL(void) RTThreadWriteLockInc(RTTHREAD Thread);
424
425/**
426 * Works the THREADINT::cWriteLocks member, mostly internal.
427 *
428 * @param Thread The current thread.
429 */
430RTDECL(void) RTThreadWriteLockDec(RTTHREAD Thread);
431
432/**
433 * Gets the number of read locks the specified thread owns.
434 *
435 * Note that nesting read lock entry will be included in the
436 * total sum. And that it probably will return 0 for non-strict
437 * builds since release builds doesn't do unnecessary diagnostic
438 * counting like this.
439 *
440 * @returns Number of read locks on success (0+) and VERR_INVALID_HANDLER on failure
441 * @param Thread The thread we're inquiring about.
442 */
443RTDECL(int32_t) RTThreadGetReadLockCount(RTTHREAD Thread);
444
445/**
446 * Works the THREADINT::cReadLocks member.
447 *
448 * @param Thread The current thread.
449 */
450RTDECL(void) RTThreadReadLockInc(RTTHREAD Thread);
451
452/**
453 * Works the THREADINT::cReadLocks member.
454 *
455 * @param Thread The current thread.
456 */
457RTDECL(void) RTThreadReadLockDec(RTTHREAD Thread);
458
459
460/** @name Thread Local Storage
461 * @{
462 */
463/**
464 * Thread termination callback for destroying a non-zero TLS entry.
465 *
466 * @remarks It is not permittable to use any RTTls APIs at this time. Doing so
467 * may lead to endless loops, crashes, and other bad stuff.
468 *
469 * @param pvValue The current value.
470 */
471typedef DECLCALLBACK(void) FNRTTLSDTOR(void *pvValue);
472/** Pointer to a FNRTTLSDTOR. */
473typedef FNRTTLSDTOR *PFNRTTLSDTOR;
474
475/**
476 * Allocates a TLS entry.
477 *
478 * @returns the index of the allocated TLS entry.
479 * @returns NIL_RTTLS on failure.
480 */
481RTR3DECL(RTTLS) RTTlsAlloc(void);
482
483/**
484 * Allocates a TLS entry (with status code).
485 *
486 * @returns IPRT status code.
487 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
488 * doesn't support this feature.
489 *
490 * @param piTls Where to store the index of the allocated TLS entry.
491 * This is set to NIL_RTTLS on failure.
492 * @param pfnDestructor Optional callback function for cleaning up on
493 * thread termination. WARNING! This feature may not
494 * be implemented everywhere.
495 */
496RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
497
498/**
499 * Frees a TLS entry.
500 *
501 * @returns IPRT status code.
502 * @param iTls The index of the TLS entry.
503 */
504RTR3DECL(int) RTTlsFree(RTTLS iTls);
505
506/**
507 * Get the value stored in a TLS entry.
508 *
509 * @returns value in given TLS entry.
510 * @returns NULL on failure.
511 * @param iTls The index of the TLS entry.
512 */
513RTR3DECL(void *) RTTlsGet(RTTLS iTls);
514
515/**
516 * Get the value stored in a TLS entry.
517 *
518 * @returns IPRT status code.
519 * @param iTls The index of the TLS entry.
520 * @param ppvValue Where to store the value.
521 */
522RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
523
524/**
525 * Set the value stored in an allocated TLS entry.
526 *
527 * @returns IPRT status.
528 * @param iTls The index of the TLS entry.
529 * @param pvValue The value to store.
530 *
531 * @remarks Note that NULL is considered to special value.
532 */
533RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
534
535/** @} */
536
537#endif /* IN_RING3 */
538
539/** @} */
540
541__END_DECLS
542
543#endif
544
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