VirtualBox

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

Last change on this file since 52632 was 52632, checked in by vboxsync, 10 years ago

bugfix in previous commit with some new parent watcher code (disabled).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.1 KB
Line 
1/** @file
2 * IPRT - Threads.
3 */
4
5/*
6 * Copyright (C) 2006-2013 Oracle Corporation
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#include <iprt/stdarg.h>
32
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_thread RTThread - Thread Management
37 * @ingroup grp_rt
38 * @{
39 */
40
41/**
42 * The thread state.
43 */
44typedef enum RTTHREADSTATE
45{
46 /** The usual invalid 0 value. */
47 RTTHREADSTATE_INVALID = 0,
48 /** The thread is being initialized. */
49 RTTHREADSTATE_INITIALIZING,
50 /** The thread has terminated */
51 RTTHREADSTATE_TERMINATED,
52 /** Probably running. */
53 RTTHREADSTATE_RUNNING,
54
55 /** Waiting on a critical section. */
56 RTTHREADSTATE_CRITSECT,
57 /** Waiting on a event semaphore. */
58 RTTHREADSTATE_EVENT,
59 /** Waiting on a event multiple wakeup semaphore. */
60 RTTHREADSTATE_EVENT_MULTI,
61 /** Waiting on a fast mutex. */
62 RTTHREADSTATE_FAST_MUTEX,
63 /** Waiting on a mutex. */
64 RTTHREADSTATE_MUTEX,
65 /** Waiting on a read write semaphore, read (shared) access. */
66 RTTHREADSTATE_RW_READ,
67 /** Waiting on a read write semaphore, write (exclusive) access. */
68 RTTHREADSTATE_RW_WRITE,
69 /** The thread is sleeping. */
70 RTTHREADSTATE_SLEEP,
71 /** Waiting on a spin mutex. */
72 RTTHREADSTATE_SPIN_MUTEX,
73 /** End of the thread states. */
74 RTTHREADSTATE_END,
75
76 /** The usual 32-bit size hack. */
77 RTTHREADSTATE_32BIT_HACK = 0x7fffffff
78} RTTHREADSTATE;
79
80/** Checks if a thread state indicates that the thread is sleeping. */
81#define RTTHREAD_IS_SLEEPING(enmState) ((enmState) >= RTTHREADSTATE_CRITSECT)
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#ifndef IN_RC
167
168/**
169 * Checks if the IPRT thread component has been initialized.
170 *
171 * This is used to avoid calling into RTThread before the runtime has been
172 * initialized.
173 *
174 * @returns @c true if it's initialized, @c false if not.
175 */
176RTDECL(bool) RTThreadIsInitialized(void);
177
178/**
179 * Get the thread handle of the current thread.
180 *
181 * @returns Thread handle.
182 */
183RTDECL(RTTHREAD) RTThreadSelf(void);
184
185/**
186 * Get the native thread handle of the current thread.
187 *
188 * @returns Native thread handle.
189 */
190RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void);
191
192/**
193 * Millisecond granular sleep function.
194 *
195 * @returns VINF_SUCCESS on success.
196 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happened
197 * which interrupt the peaceful sleep.
198 * @param cMillies Number of milliseconds to sleep.
199 * 0 milliseconds means yielding the timeslice - deprecated!
200 * @remark See RTThreadNanoSleep() for sleeping for smaller periods of time.
201 */
202RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies);
203
204/**
205 * Millisecond granular sleep function, no logger calls.
206 *
207 * Same as RTThreadSleep, except it will never call into the IPRT logger. It
208 * can therefore safely be used in places where the logger is off limits, like
209 * at termination or init time. The electric fence heap is one consumer of
210 * this API.
211 *
212 * @returns VINF_SUCCESS on success.
213 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happened
214 * which interrupt the peaceful sleep.
215 * @param cMillies Number of milliseconds to sleep.
216 * 0 milliseconds means yielding the timeslice - deprecated!
217 */
218RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies);
219
220/**
221 * Yields the CPU.
222 *
223 * @returns true if we yielded.
224 * @returns false if it's probable that we didn't yield.
225 */
226RTDECL(bool) RTThreadYield(void);
227
228
229
230/**
231 * Thread function.
232 *
233 * @returns 0 on success.
234 * @param ThreadSelf Thread handle to this thread.
235 * @param pvUser User argument.
236 */
237typedef DECLCALLBACK(int) FNRTTHREAD(RTTHREAD ThreadSelf, void *pvUser);
238/** Pointer to a FNRTTHREAD(). */
239typedef FNRTTHREAD *PFNRTTHREAD;
240
241/**
242 * Thread creation flags.
243 */
244typedef enum RTTHREADFLAGS
245{
246 /** This flag is used to keep the thread structure around so it can
247 * be waited on after termination. @sa RTThreadWait and
248 * RTThreadWaitNoResume. Not required for RTThreadUserWait and friends!
249 */
250 RTTHREADFLAGS_WAITABLE = RT_BIT(0),
251 /** The bit number corresponding to the RTTHREADFLAGS_WAITABLE mask. */
252 RTTHREADFLAGS_WAITABLE_BIT = 0,
253
254 /** Mask of valid flags, use for validation. */
255 RTTHREADFLAGS_MASK = RT_BIT(0)
256} RTTHREADFLAGS;
257
258
259/**
260 * Create a new thread.
261 *
262 * @returns iprt status code.
263 * @param pThread Where to store the thread handle to the new thread. (optional)
264 * @param pfnThread The thread function.
265 * @param pvUser User argument.
266 * @param cbStack The size of the stack for the new thread.
267 * Use 0 for the default stack size.
268 * @param enmType The thread type. Used for deciding scheduling attributes
269 * of the thread.
270 * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
271 * @param pszName Thread name.
272 *
273 * @remark When called in Ring-0, this API will create a new kernel thread and not a thread in
274 * the context of the calling process.
275 */
276RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
277 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
278/** @copydoc RTThreadCreate */
279typedef DECLCALLBACKPTR(int, PFNRTTHREADCREATE)(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
280 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
281
282
283/**
284 * Create a new thread.
285 *
286 * Same as RTThreadCreate except the name is given in the RTStrPrintfV form.
287 *
288 * @returns iprt status code.
289 * @param pThread See RTThreadCreate.
290 * @param pfnThread See RTThreadCreate.
291 * @param pvUser See RTThreadCreate.
292 * @param cbStack See RTThreadCreate.
293 * @param enmType See RTThreadCreate.
294 * @param fFlags See RTThreadCreate.
295 * @param pszName Thread name format.
296 * @param va Format arguments.
297 */
298RTDECL(int) RTThreadCreateV(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
299 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, va_list va);
300
301/**
302 * Create a new thread.
303 *
304 * Same as RTThreadCreate except the name is given in the RTStrPrintf form.
305 *
306 * @returns iprt status code.
307 * @param pThread See RTThreadCreate.
308 * @param pfnThread See RTThreadCreate.
309 * @param pvUser See RTThreadCreate.
310 * @param cbStack See RTThreadCreate.
311 * @param enmType See RTThreadCreate.
312 * @param fFlags See RTThreadCreate.
313 * @param pszName Thread name format.
314 * @param ... Format arguments.
315 */
316RTDECL(int) RTThreadCreateF(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
317 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, ...);
318
319/**
320 * Gets the native thread id of a IPRT thread.
321 *
322 * @returns The native thread id.
323 * @param Thread The IPRT thread.
324 */
325RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
326
327/**
328 * Gets the IPRT thread of a native thread.
329 *
330 * @returns The IPRT thread handle
331 * @returns NIL_RTTHREAD if not a thread known to IPRT.
332 * @param NativeThread The native thread handle/id.
333 */
334RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
335
336/**
337 * Changes the type of the specified thread.
338 *
339 * @returns iprt status code.
340 * @param Thread The thread which type should be changed.
341 * @param enmType The new thread type.
342 * @remark In Ring-0 it only works if Thread == RTThreadSelf().
343 */
344RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
345
346/**
347 * Wait for the thread to terminate, resume on interruption.
348 *
349 * @returns iprt status code.
350 * Will not return VERR_INTERRUPTED.
351 * @param Thread The thread to wait for.
352 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
353 * an indefinite wait.
354 * @param prc Where to store the return code of the thread. Optional.
355 */
356RTDECL(int) RTThreadWait(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
357
358/**
359 * Wait for the thread to terminate, return on interruption.
360 *
361 * @returns iprt status code.
362 * @param Thread The thread to wait for.
363 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
364 * an indefinite wait.
365 * @param prc Where to store the return code of the thread. Optional.
366 */
367RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
368
369/**
370 * Gets the name of the current thread thread.
371 *
372 * @returns Pointer to readonly name string.
373 * @returns NULL on failure.
374 */
375RTDECL(const char *) RTThreadSelfName(void);
376
377/**
378 * Gets the name of a thread.
379 *
380 * @returns Pointer to readonly name string.
381 * @returns NULL on failure.
382 * @param Thread Thread handle of the thread to query the name of.
383 */
384RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
385
386/**
387 * Gets the type of the specified thread.
388 *
389 * @returns The thread type.
390 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
391 * @param Thread The thread in question.
392 */
393RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
394
395/**
396 * Sets the name of a thread.
397 *
398 * @returns iprt status code.
399 * @param Thread Thread handle of the thread to query the name of.
400 * @param pszName The thread name.
401 */
402RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
403
404/**
405 * Checks if the specified thread is the main thread.
406 *
407 * @returns true if it is, false if it isn't.
408 *
409 * @param hThread The thread handle.
410 */
411RTDECL(bool) RTThreadIsMain(RTTHREAD hThread);
412
413/**
414 * Checks if the calling thread is known to IPRT.
415 *
416 * @returns @c true if it is, @c false if it isn't.
417 */
418RTDECL(bool) RTThreadIsSelfKnown(void);
419
420/**
421 * Checks if the calling thread is know to IPRT and is alive.
422 *
423 * @returns @c true if it is, @c false if it isn't.
424 */
425RTDECL(bool) RTThreadIsSelfAlive(void);
426
427/**
428 * Checks if the calling thread is known to IPRT.
429 *
430 * @returns @c true if it is, @c false if it isn't.
431 */
432RTDECL(bool) RTThreadIsOperational(void);
433
434/**
435 * Signal the user event.
436 *
437 * @returns iprt status code.
438 */
439RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
440
441/**
442 * Wait for the user event.
443 *
444 * @returns iprt status code.
445 * @param Thread The thread to wait for.
446 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
447 * an indefinite wait.
448 */
449RTDECL(int) RTThreadUserWait(RTTHREAD Thread, RTMSINTERVAL cMillies);
450
451/**
452 * Wait for the user event, return on interruption.
453 *
454 * @returns iprt status code.
455 * @param Thread The thread to wait for.
456 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
457 * an indefinite wait.
458 */
459RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies);
460
461/**
462 * Reset the user event.
463 *
464 * @returns iprt status code.
465 * @param Thread The thread to reset.
466 */
467RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
468
469/**
470 * Pokes the thread.
471 *
472 * This will signal the thread, attempting to interrupt whatever it's currently
473 * doing. This is *NOT* implemented on all platforms and may cause unresolved
474 * symbols during linking or VERR_NOT_IMPLEMENTED at runtime.
475 *
476 * @returns IPRT status code.
477 *
478 * @param hThread The thread to poke. This must not be the
479 * calling thread.
480 */
481RTDECL(int) RTThreadPoke(RTTHREAD hThread);
482
483# ifdef IN_RING0
484
485/**
486 * Check if preemption is currently enabled or not for the current thread.
487 *
488 * @note This may return true even on systems where preemption isn't
489 * possible. In that case, it means no call to RTThreadPreemptDisable
490 * has been made and interrupts are still enabled.
491 *
492 * @returns true if preemption is enabled, false if preemetion is disabled.
493 * @param hThread Must be NIL_RTTHREAD for now.
494 */
495RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread);
496
497/**
498 * Check if preemption is pending for the current thread.
499 *
500 * This function should be called regularly when executing larger portions of
501 * code with preemption disabled.
502 *
503 * @returns true if pending, false if not.
504 * @param hThread Must be NIL_RTTHREAD for now.
505 */
506RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread);
507
508/**
509 * Is RTThreadPreemptIsPending reliable?
510 *
511 * @returns true if reliable, false if not.
512 */
513RTDECL(bool) RTThreadPreemptIsPendingTrusty(void);
514
515/**
516 * Is preemption possible on this system.
517 *
518 * @returns true if possible, false if not.
519 */
520RTDECL(bool) RTThreadPreemptIsPossible(void);
521
522/**
523 * Preemption state saved by RTThreadPreemptDisable and used by
524 * RTThreadPreemptRestore to restore the previous state.
525 */
526typedef struct RTTHREADPREEMPTSTATE
527{
528 /** In debug builds this will be used to check for cpu migration. */
529 RTCPUID idCpu;
530# ifdef RT_OS_WINDOWS
531 /** The old IRQL. Don't touch! */
532 unsigned char uchOldIrql;
533 /** Reserved, MBZ. */
534 uint8_t bReserved1;
535 /** Reserved, MBZ. */
536 uint8_t bReserved2;
537 /** Reserved, MBZ. */
538 uint8_t bReserved3;
539# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 255, 0, 0, 0 }
540# elif defined(RT_OS_HAIKU)
541 /** The cpu_state. Don't touch! */
542 uint32_t uOldCpuState;
543# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
544# elif defined(RT_OS_SOLARIS)
545 /** The Old PIL. Don't touch! */
546 uint32_t uOldPil;
547# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, UINT32_MAX }
548# else
549 /** Reserved, MBZ. */
550 uint32_t u32Reserved;
551# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
552# endif
553} RTTHREADPREEMPTSTATE;
554/** Pointer to a preemption state. */
555typedef RTTHREADPREEMPTSTATE *PRTTHREADPREEMPTSTATE;
556
557/**
558 * Disable preemption.
559 *
560 * A call to this function must be matched by exactly one call to
561 * RTThreadPreemptRestore().
562 *
563 * @param pState Where to store the preemption state.
564 */
565RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState);
566
567/**
568 * Restores the preemption state, undoing a previous call to
569 * RTThreadPreemptDisable.
570 *
571 * A call to this function must be matching a previous call to
572 * RTThreadPreemptDisable.
573 *
574 * @param pState The state return by RTThreadPreemptDisable.
575 */
576RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState);
577
578/**
579 * Check if the thread is executing in interrupt context.
580 *
581 * @returns true if in interrupt context, false if not.
582 * @param hThread Must be NIL_RTTHREAD for now.
583 */
584RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread);
585
586
587/**
588 * Thread-context events.
589 */
590typedef enum RTTHREADCTXEVENT
591{
592 /** This thread is about to be preempted. */
593 RTTHREADCTXEVENT_PREEMPTING = 0,
594 /** This thread has just been resumed. */
595 RTTHREADCTXEVENT_RESUMED,
596 /** The usual 32-bit size hack. */
597 RTTHREADCTXEVENT_32BIT_HACK = 0x7fffffff
598} RTTHREADCTXEVENT;
599
600/**
601 * Thread-context hook.
602 *
603 * @returns IPRT status code.
604 * @param enmEvent The thread-context event.
605 * @param pvUser User argument.
606 *
607 * @remarks This function may be called under different contexts, i.e. with
608 * different locks held, with/without preemption disabled depending on
609 * the event in @a enmEvent.
610 */
611typedef DECLCALLBACK(void) FNRTTHREADCTXHOOK(RTTHREADCTXEVENT enmEvent, void *pvUser);
612/** Pointer to a thread-context hook. */
613typedef FNRTTHREADCTXHOOK *PFNRTTHREADCTXHOOK;
614
615/**
616 * Initializes a thread-context hook for the current thread.
617 *
618 * This must be called once per-thread before using RTThreadCtxHooksRegister().
619 *
620 * @returns IPRT status code.
621 * @param phThreadCtx Where to store the thread-context handle.
622 *
623 * @remarks This must be called with preemption enabled!
624 */
625RTDECL(int) RTThreadCtxHooksCreate(PRTTHREADCTX phThreadCtx);
626
627/**
628 * Retains a new reference to a thread-context hook.
629 *
630 * @returns New reference count.
631 * UINT32_MAX is returned if the handle is invalid (asserted).
632 * @param phThreadCtx Pointer to the thread-context handle.
633 *
634 * @remarks This can be called from any thread. Can be called with preemption
635 * disabled.
636 */
637RTDECL(uint32_t) RTThreadCtxHooksRetain(RTTHREADCTX hThreadCtx);
638
639/**
640 * Releases a reference to a thread-context hook.
641 *
642 * @returns New reference count.
643 * @retval 0 if the thread-context hook was freed or @a hThreadCtx is
644 * NIL_RTTHREADCTX.
645 * @retval UINT32_MAX is returned if the handle is invalid (asserted).
646 *
647 * @param hThreadCtx The thread-context handle.
648 *
649 * @remarks This can be called from any thread but must be called with
650 * preemption enabled!
651 */
652RTDECL(uint32_t) RTThreadCtxHooksRelease(RTTHREADCTX hThreadCtx);
653
654/**
655 * Registers a thread-context hook for the current thread to receive
656 * notifications for all supported thread-context events.
657 *
658 * @returns IPRT status code.
659 * @param hThreadCtx The thread-context handle.
660 * @param pfnThreadHook Pointer to a thread-context hook (a callback)
661 * for all thread-context events.
662 * @param pvUser User argument (optional, can be NULL).
663 *
664 * @remarks Can be called with preemption disabled.
665 */
666RTDECL(int) RTThreadCtxHooksRegister(RTTHREADCTX hThreadCtx, PFNRTTHREADCTXHOOK pfnThreadHook, void *pvUser);
667
668/**
669 * Deregisters the thread-context hook for the current thread.
670 *
671 * @returns IPRT status code.
672 * @param hThreadCtx The thread-context handle.
673 *
674 * @remarks Can be called with preemption disabled.
675 */
676RTDECL(int) RTThreadCtxHooksDeregister(RTTHREADCTX hThreadCtx);
677
678/**
679 * Are thread-context hooks registered for the thread?
680 *
681 * @returns true if registered, false if not supported or not registered.
682 * @param hThreadCtx The thread-context handle.
683 *
684 * @remarks Can be called from any thread (but possibility of races when
685 * it's not the current thread!)
686 */
687RTDECL(bool) RTThreadCtxHooksAreRegistered(RTTHREADCTX hThreadCtx);
688
689# endif /* IN_RING0 */
690
691
692# ifdef IN_RING3
693
694/**
695 * Adopts a non-IPRT thread.
696 *
697 * @returns IPRT status code.
698 * @param enmType The thread type.
699 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
700 * @param pszName The thread name. Optional
701 * @param pThread Where to store the thread handle. Optional.
702 */
703RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
704
705/**
706 * Get the thread handle of the current thread, automatically adopting alien
707 * threads.
708 *
709 * @returns Thread handle.
710 */
711RTDECL(RTTHREAD) RTThreadSelfAutoAdopt(void);
712
713/**
714 * Gets the affinity mask of the current thread.
715 *
716 * @returns IPRT status code.
717 * @param pCpuSet Where to return the CPU affienty set of the calling
718 * thread.
719 */
720RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet);
721
722/**
723 * Sets the affinity mask of the current thread.
724 *
725 * @returns iprt status code.
726 * @param pCpuSet The set of CPUs this thread can run on. NULL means
727 * all CPUs.
728 */
729RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet);
730
731/**
732 * Binds the thread to one specific CPU.
733 *
734 * @returns iprt status code.
735 * @param idCpu The ID of the CPU to bind this thread to. Use
736 * NIL_RTCPUID to unbind it.
737 */
738RTR3DECL(int) RTThreadSetAffinityToCpu(RTCPUID idCpu);
739
740/**
741 * Unblocks a thread.
742 *
743 * This function is paired with RTThreadBlocking and RTThreadBlockingDebug.
744 *
745 * @param hThread The current thread.
746 * @param enmCurState The current state, used to check for nested blocking.
747 * The new state will be running.
748 */
749RTDECL(void) RTThreadUnblocked(RTTHREAD hThread, RTTHREADSTATE enmCurState);
750
751/**
752 * Change the thread state to blocking.
753 *
754 * @param hThread The current thread.
755 * @param enmState The sleep state.
756 * @param fReallySleeping Really going to sleep now. Use false before calls
757 * to other IPRT synchronization methods.
758 */
759RTDECL(void) RTThreadBlocking(RTTHREAD hThread, RTTHREADSTATE enmState, bool fReallySleeping);
760
761/**
762 * Get the current thread state.
763 *
764 * A thread that is reported as sleeping may actually still be running inside
765 * the lock validator or/and in the code of some other IPRT synchronization
766 * primitive. Use RTThreadGetReallySleeping
767 *
768 * @returns The thread state.
769 * @param hThread The thread.
770 */
771RTDECL(RTTHREADSTATE) RTThreadGetState(RTTHREAD hThread);
772
773/**
774 * Checks if the thread is really sleeping or not.
775 *
776 * @returns RTTHREADSTATE_RUNNING if not really sleeping, otherwise the state it
777 * is sleeping in.
778 * @param hThread The thread.
779 */
780RTDECL(RTTHREADSTATE) RTThreadGetReallySleeping(RTTHREAD hThread);
781
782/**
783 * Translate a thread state into a string.
784 *
785 * @returns Pointer to a read-only string containing the state name.
786 * @param enmState The state.
787 */
788RTDECL(const char *) RTThreadStateName(RTTHREADSTATE enmState);
789
790
791/**
792 * Native thread states returned by RTThreadNativeState.
793 */
794typedef enum RTTHREADNATIVESTATE
795{
796 /** Invalid thread handle. */
797 RTTHREADNATIVESTATE_INVALID = 0,
798 /** Unable to determine the thread state. */
799 RTTHREADNATIVESTATE_UNKNOWN,
800 /** The thread is running. */
801 RTTHREADNATIVESTATE_RUNNING,
802 /** The thread is blocked. */
803 RTTHREADNATIVESTATE_BLOCKED,
804 /** The thread is suspended / stopped. */
805 RTTHREADNATIVESTATE_SUSPENDED,
806 /** The thread has terminated. */
807 RTTHREADNATIVESTATE_TERMINATED,
808 /** Make sure it's a 32-bit type. */
809 RTTHREADNATIVESTATE_32BIT_HACK = 0x7fffffff
810} RTTHREADNATIVESTATE;
811
812
813/**
814 * Get the native state of a thread.
815 *
816 * @returns Native state.
817 * @param hThread The thread handle.
818 *
819 * @remarks Not yet implemented on all systems, so have a backup plan for
820 * RTTHREADNATIVESTATE_UNKNOWN.
821 */
822RTDECL(RTTHREADNATIVESTATE) RTThreadGetNativeState(RTTHREAD hThread);
823
824
825/**
826 * Get the execution times of the specified thread
827 *
828 * @returns IPRT status code.
829 * @param pKernelTime Kernel execution time in ms (out)
830 * @param pUserTime User execution time in ms (out)
831 *
832 */
833RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime);
834
835/** @name Thread Local Storage
836 * @{
837 */
838/**
839 * Thread termination callback for destroying a non-zero TLS entry.
840 *
841 * @remarks It is not permitable to use any RTTls APIs at this time. Doing so
842 * may lead to endless loops, crashes, and other bad stuff.
843 *
844 * @param pvValue The current value.
845 */
846typedef DECLCALLBACK(void) FNRTTLSDTOR(void *pvValue);
847/** Pointer to a FNRTTLSDTOR. */
848typedef FNRTTLSDTOR *PFNRTTLSDTOR;
849
850/**
851 * Allocates a TLS entry (index).
852 *
853 * Example code:
854 * @code
855 RTTLS g_iTls = NIL_RTTLS;
856
857 ...
858
859 // once for the process, allocate the TLS index
860 if (g_iTls == NIL_RTTLS)
861 g_iTls = RTTlsAlloc();
862
863 // set the thread-local value.
864 RTTlsSet(g_iTls, pMyData);
865
866 ...
867
868 // get the thread-local value
869 PMYDATA pMyData = (PMYDATA)RTTlsGet(g_iTls);
870
871 @endcode
872 *
873 * @returns the index of the allocated TLS entry.
874 * @returns NIL_RTTLS on failure.
875 */
876RTR3DECL(RTTLS) RTTlsAlloc(void);
877
878/**
879 * Variant of RTTlsAlloc that returns a status code.
880 *
881 * @returns IPRT status code.
882 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
883 * doesn't support this feature.
884 *
885 * @param piTls Where to store the index of the allocated TLS entry.
886 * This is set to NIL_RTTLS on failure.
887 * @param pfnDestructor Optional callback function for cleaning up on
888 * thread termination. WARNING! This feature may not
889 * be implemented everywhere.
890 */
891RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
892
893/**
894 * Frees a TLS entry.
895 *
896 * @returns IPRT status code.
897 * @param iTls The index of the TLS entry.
898 */
899RTR3DECL(int) RTTlsFree(RTTLS iTls);
900
901/**
902 * Get the (thread-local) value stored in a TLS entry.
903 *
904 * @returns value in given TLS entry.
905 * @retval NULL if RTTlsSet() has not yet been called on this thread, or if the
906 * TLS index is invalid.
907 *
908 * @param iTls The index of the TLS entry.
909 */
910RTR3DECL(void *) RTTlsGet(RTTLS iTls);
911
912/**
913 * Get the value stored in a TLS entry.
914 *
915 * @returns IPRT status code.
916 * @param iTls The index of the TLS entry.
917 * @param ppvValue Where to store the value. The value will be NULL if
918 * RTTlsSet has not yet been called on this thread.
919 */
920RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
921
922/**
923 * Set the value stored in an allocated TLS entry.
924 *
925 * @returns IPRT status.
926 * @param iTls The index of the TLS entry.
927 * @param pvValue The value to store.
928 *
929 * @remarks Note that NULL is considered a special value.
930 */
931RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
932
933/** @} */
934
935# endif /* IN_RING3 */
936# endif /* !IN_RC */
937
938/** @} */
939
940RT_C_DECLS_END
941
942#endif
943
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