VirtualBox

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

Last change on this file since 19755 was 13832, checked in by vboxsync, 16 years ago

IN_GC -> IN_RC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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_RC
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 * Pokes the thread.
376 *
377 * This will signal the thread, attempting to interrupt whatever it's currently
378 * doing. This is *NOT* implemented on all platforms and may cause unresolved
379 * symbols during linking or VERR_NOT_IMPLEMENTED at runtime.
380 *
381 * @returns IPRT status code.
382 *
383 * @param hThread The thread to poke. This must not be the
384 * calling thread.
385 */
386RTDECL(int) RTThreadPoke(RTTHREAD hThread);
387
388#ifdef IN_RING0
389
390/**
391 * Check if preemption is currently enabled or not for the current thread.
392 *
393 * @returns true if preemtion is enabled, false if preemetion is disabled.
394 * @param hThread Must be NIL_RTTHREAD for now.
395 */
396RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread);
397
398/**
399 * Preemption state saved by RTThreadPreemptDisable and used by
400 * RTThreadPreemptRestore to restore the previous state.
401 */
402typedef struct RTTHREADPREEMPTSTATE
403{
404#ifdef RT_OS_WINDOWS
405 /** The old IRQL. Don't touch. */
406 unsigned char uchOldIrql;
407# define RTTHREADPREEMPTSTATE_INITIALIZER { 255 }
408#else
409 /** Dummy unused placeholder. */
410 unsigned char uchDummy;
411# define RTTHREADPREEMPTSTATE_INITIALIZER { 0 }
412#endif
413} RTTHREADPREEMPTSTATE;
414/** Pointer to a preemption state. */
415typedef RTTHREADPREEMPTSTATE *PRTTHREADPREEMPTSTATE;
416
417/**
418 * Disable preemption.
419 *
420 * A call to this function must be matched by exactly one call to
421 * RTThreadPreemptRestore().
422 *
423 * @param pState Where to store the preemption state.
424 */
425RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState);
426
427/**
428 * Restores the preemption state, undoing a previous call to
429 * RTThreadPreemptDisable.
430 *
431 * A call to this function must be matching a previous call to
432 * RTThreadPreemptDisable.
433 *
434 * @param pState The state return by RTThreadPreemptDisable.
435 */
436RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState);
437
438#endif /* IN_RING0 */
439
440
441#ifdef IN_RING3
442
443/**
444 * Adopts a non-IPRT thread.
445 *
446 * @returns IPRT status code.
447 * @param enmType The thread type.
448 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
449 * @param pszName The thread name. Optional
450 * @param pThread Where to store the thread handle. Optional.
451 */
452RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
453
454/**
455 * Gets the affinity mask of the current thread.
456 *
457 * @returns The affinity mask (bit 0 = logical cpu 0).
458 */
459RTR3DECL(uint64_t) RTThreadGetAffinity(void);
460
461/**
462 * Sets the affinity mask of the current thread.
463 *
464 * @returns iprt status code.
465 * @param u64Mask Affinity mask (bit 0 = logical cpu 0).
466 */
467RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask);
468
469/**
470 * Gets the number of write locks and critical sections the specified
471 * thread owns.
472 *
473 * This number does not include any nested lock/critect entries.
474 *
475 * Note that it probably will return 0 for non-strict builds since
476 * release builds doesn't do unnecessary diagnostic counting like this.
477 *
478 * @returns Number of locks on success (0+) and VERR_INVALID_HANDLER on failure
479 * @param Thread The thread we're inquiring about.
480 * @remarks Will only work for strict builds.
481 */
482RTDECL(int32_t) RTThreadGetWriteLockCount(RTTHREAD Thread);
483
484/**
485 * Works the THREADINT::cWriteLocks member, mostly internal.
486 *
487 * @param Thread The current thread.
488 */
489RTDECL(void) RTThreadWriteLockInc(RTTHREAD Thread);
490
491/**
492 * Works the THREADINT::cWriteLocks member, mostly internal.
493 *
494 * @param Thread The current thread.
495 */
496RTDECL(void) RTThreadWriteLockDec(RTTHREAD Thread);
497
498/**
499 * Gets the number of read locks the specified thread owns.
500 *
501 * Note that nesting read lock entry will be included in the
502 * total sum. And that it probably will return 0 for non-strict
503 * builds since release builds doesn't do unnecessary diagnostic
504 * counting like this.
505 *
506 * @returns Number of read locks on success (0+) and VERR_INVALID_HANDLER on failure
507 * @param Thread The thread we're inquiring about.
508 */
509RTDECL(int32_t) RTThreadGetReadLockCount(RTTHREAD Thread);
510
511/**
512 * Works the THREADINT::cReadLocks member.
513 *
514 * @param Thread The current thread.
515 */
516RTDECL(void) RTThreadReadLockInc(RTTHREAD Thread);
517
518/**
519 * Works the THREADINT::cReadLocks member.
520 *
521 * @param Thread The current thread.
522 */
523RTDECL(void) RTThreadReadLockDec(RTTHREAD Thread);
524
525
526/** @name Thread Local Storage
527 * @{
528 */
529/**
530 * Thread termination callback for destroying a non-zero TLS entry.
531 *
532 * @remarks It is not permittable to use any RTTls APIs at this time. Doing so
533 * may lead to endless loops, crashes, and other bad stuff.
534 *
535 * @param pvValue The current value.
536 */
537typedef DECLCALLBACK(void) FNRTTLSDTOR(void *pvValue);
538/** Pointer to a FNRTTLSDTOR. */
539typedef FNRTTLSDTOR *PFNRTTLSDTOR;
540
541/**
542 * Allocates a TLS entry.
543 *
544 * @returns the index of the allocated TLS entry.
545 * @returns NIL_RTTLS on failure.
546 */
547RTR3DECL(RTTLS) RTTlsAlloc(void);
548
549/**
550 * Allocates a TLS entry (with status code).
551 *
552 * @returns IPRT status code.
553 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
554 * doesn't support this feature.
555 *
556 * @param piTls Where to store the index of the allocated TLS entry.
557 * This is set to NIL_RTTLS on failure.
558 * @param pfnDestructor Optional callback function for cleaning up on
559 * thread termination. WARNING! This feature may not
560 * be implemented everywhere.
561 */
562RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
563
564/**
565 * Frees a TLS entry.
566 *
567 * @returns IPRT status code.
568 * @param iTls The index of the TLS entry.
569 */
570RTR3DECL(int) RTTlsFree(RTTLS iTls);
571
572/**
573 * Get the value stored in a TLS entry.
574 *
575 * @returns value in given TLS entry.
576 * @returns NULL on failure.
577 * @param iTls The index of the TLS entry.
578 */
579RTR3DECL(void *) RTTlsGet(RTTLS iTls);
580
581/**
582 * Get the value stored in a TLS entry.
583 *
584 * @returns IPRT status code.
585 * @param iTls The index of the TLS entry.
586 * @param ppvValue Where to store the value.
587 */
588RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
589
590/**
591 * Set the value stored in an allocated TLS entry.
592 *
593 * @returns IPRT status.
594 * @param iTls The index of the TLS entry.
595 * @param pvValue The value to store.
596 *
597 * @remarks Note that NULL is considered to special value.
598 */
599RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
600
601/** @} */
602
603#endif /* IN_RING3 */
604
605/** @} */
606
607__END_DECLS
608
609#endif
610
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