VirtualBox

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

Last change on this file since 75155 was 72639, checked in by vboxsync, 6 years ago

IPRT,VMM: Try use KeShouldYieldProcessor to implement RTThreadPreemptIsPending on Windows. That'll save us a lot of pain with changing _KPRCB structures. bugref:5102

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.8 KB
Line 
1/** @file
2 * IPRT - Threads.
3 */
4
5/*
6 * Copyright (C) 2006-2017 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 produced 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#ifndef RT_OS_LINUX /* XXX crashes genksyms at least on 32-bit Linux hosts */
279/** @copydoc RTThreadCreate */
280typedef DECLCALLBACKPTR(int, PFNRTTHREADCREATE)(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
281 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
282#endif
283
284
285/**
286 * Create a new thread.
287 *
288 * Same as RTThreadCreate except the name is given in the RTStrPrintfV form.
289 *
290 * @returns iprt status code.
291 * @param pThread See RTThreadCreate.
292 * @param pfnThread See RTThreadCreate.
293 * @param pvUser See RTThreadCreate.
294 * @param cbStack See RTThreadCreate.
295 * @param enmType See RTThreadCreate.
296 * @param fFlags See RTThreadCreate.
297 * @param pszName Thread name format.
298 * @param va Format arguments.
299 */
300RTDECL(int) RTThreadCreateV(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
301 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, va_list va) RT_IPRT_FORMAT_ATTR(7, 0);
302
303/**
304 * Create a new thread.
305 *
306 * Same as RTThreadCreate except the name is given in the RTStrPrintf form.
307 *
308 * @returns iprt status code.
309 * @param pThread See RTThreadCreate.
310 * @param pfnThread See RTThreadCreate.
311 * @param pvUser See RTThreadCreate.
312 * @param cbStack See RTThreadCreate.
313 * @param enmType See RTThreadCreate.
314 * @param fFlags See RTThreadCreate.
315 * @param pszName Thread name format.
316 * @param ... Format arguments.
317 */
318RTDECL(int) RTThreadCreateF(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
319 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, ...) RT_IPRT_FORMAT_ATTR(7, 8);
320
321/**
322 * Gets the native thread id of a IPRT thread.
323 *
324 * @returns The native thread id.
325 * @param Thread The IPRT thread.
326 */
327RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
328
329/**
330 * Gets the native thread handle for a IPRT thread.
331 *
332 * @returns The thread handle. INVALID_HANDLE_VALUE on failure.
333 * @param hThread The IPRT thread handle.
334 *
335 * @note Windows only.
336 * @note Only valid after parent returns from the thread creation call.
337 */
338RTDECL(uintptr_t) RTThreadGetNativeHandle(RTTHREAD hThread);
339
340/**
341 * Gets the IPRT thread of a native thread.
342 *
343 * @returns The IPRT thread handle
344 * @returns NIL_RTTHREAD if not a thread known to IPRT.
345 * @param NativeThread The native thread handle/id.
346 */
347RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
348
349/**
350 * Changes the type of the specified thread.
351 *
352 * @returns iprt status code.
353 * @param Thread The thread which type should be changed.
354 * @param enmType The new thread type.
355 * @remark In Ring-0 it only works if Thread == RTThreadSelf().
356 */
357RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
358
359/**
360 * Wait for the thread to terminate, resume on interruption.
361 *
362 * @returns iprt status code.
363 * Will not return VERR_INTERRUPTED.
364 * @param Thread The thread to wait for.
365 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
366 * an indefinite wait.
367 * @param prc Where to store the return code of the thread. Optional.
368 */
369RTDECL(int) RTThreadWait(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
370
371/**
372 * Wait for the thread to terminate, return on interruption.
373 *
374 * @returns iprt status code.
375 * @param Thread The thread to wait for.
376 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
377 * an indefinite wait.
378 * @param prc Where to store the return code of the thread. Optional.
379 */
380RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
381
382/**
383 * Gets the name of the current thread thread.
384 *
385 * @returns Pointer to readonly name string.
386 * @returns NULL on failure.
387 */
388RTDECL(const char *) RTThreadSelfName(void);
389
390/**
391 * Gets the name of a thread.
392 *
393 * @returns Pointer to readonly name string.
394 * @returns NULL on failure.
395 * @param Thread Thread handle of the thread to query the name of.
396 */
397RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
398
399/**
400 * Gets the type of the specified thread.
401 *
402 * @returns The thread type.
403 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
404 * @param Thread The thread in question.
405 */
406RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
407
408/**
409 * Sets the name of a thread.
410 *
411 * @returns iprt status code.
412 * @param Thread Thread handle of the thread to query the name of.
413 * @param pszName The thread name.
414 */
415RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
416
417/**
418 * Checks if the specified thread is the main thread.
419 *
420 * @returns true if it is, false if it isn't.
421 *
422 * @param hThread The thread handle.
423 */
424RTDECL(bool) RTThreadIsMain(RTTHREAD hThread);
425
426/**
427 * Checks if the calling thread is known to IPRT.
428 *
429 * @returns @c true if it is, @c false if it isn't.
430 */
431RTDECL(bool) RTThreadIsSelfKnown(void);
432
433/**
434 * Checks if the calling thread is know to IPRT and is alive.
435 *
436 * @returns @c true if it is, @c false if it isn't.
437 */
438RTDECL(bool) RTThreadIsSelfAlive(void);
439
440/**
441 * Checks if the calling thread is known to IPRT.
442 *
443 * @returns @c true if it is, @c false if it isn't.
444 */
445RTDECL(bool) RTThreadIsOperational(void);
446
447/**
448 * Signal the user event.
449 *
450 * @returns iprt status code.
451 */
452RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
453
454/**
455 * Wait for the user event.
456 *
457 * @returns iprt status code.
458 * @param Thread The thread to wait for.
459 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
460 * an indefinite wait.
461 */
462RTDECL(int) RTThreadUserWait(RTTHREAD Thread, RTMSINTERVAL cMillies);
463
464/**
465 * Wait for the user event, return on interruption.
466 *
467 * @returns iprt status code.
468 * @param Thread The thread to wait for.
469 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
470 * an indefinite wait.
471 */
472RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies);
473
474/**
475 * Reset the user event.
476 *
477 * @returns iprt status code.
478 * @param Thread The thread to reset.
479 */
480RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
481
482/**
483 * Pokes the thread.
484 *
485 * This will wake up or/and signal the thread, attempting to interrupt whatever
486 * it's currently doing.
487 *
488 * The posixy version of this will send a signal to the thread, quite likely
489 * waking it up from normal sleeps, waits, and I/O. When IPRT is in
490 * non-obtrusive mode, the posixy version will definitely return
491 * VERR_NOT_IMPLEMENTED, and it may also do so if no usable signal was found.
492 *
493 * On Windows the thread will be alerted, waking it up from most sleeps and
494 * waits, but not probably very little in the I/O area (needs testing). On NT
495 * 3.50 and 3.1 VERR_NOT_IMPLEMENTED will be returned.
496 *
497 * @returns IPRT status code.
498 *
499 * @param hThread The thread to poke. This must not be the
500 * calling thread.
501 *
502 * @note This is *NOT* implemented on all platforms and may cause unresolved
503 * symbols during linking or VERR_NOT_IMPLEMENTED at runtime.
504 *
505 */
506RTDECL(int) RTThreadPoke(RTTHREAD hThread);
507
508# ifdef IN_RING0
509
510/**
511 * Check if preemption is currently enabled or not for the current thread.
512 *
513 * @note This may return true even on systems where preemption isn't
514 * possible. In that case, it means no call to RTThreadPreemptDisable
515 * has been made and interrupts are still enabled.
516 *
517 * @returns true if preemption is enabled, false if preemetion is disabled.
518 * @param hThread Must be NIL_RTTHREAD for now.
519 */
520RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread);
521
522/**
523 * Check if preemption is pending for the current thread.
524 *
525 * This function should be called regularly when executing larger portions of
526 * code with preemption disabled.
527 *
528 * @returns true if pending, false if not.
529 * @param hThread Must be NIL_RTTHREAD for now.
530 *
531 * @note If called with interrupts disabled, the NT kernel may temporarily
532 * re-enable them while checking.
533 */
534RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread);
535
536/**
537 * Is RTThreadPreemptIsPending reliable?
538 *
539 * @returns true if reliable, false if not.
540 */
541RTDECL(bool) RTThreadPreemptIsPendingTrusty(void);
542
543/**
544 * Is preemption possible on this system.
545 *
546 * @returns true if possible, false if not.
547 */
548RTDECL(bool) RTThreadPreemptIsPossible(void);
549
550/**
551 * Preemption state saved by RTThreadPreemptDisable and used by
552 * RTThreadPreemptRestore to restore the previous state.
553 */
554typedef struct RTTHREADPREEMPTSTATE
555{
556 /** In debug builds this will be used to check for cpu migration. */
557 RTCPUID idCpu;
558# ifdef RT_OS_WINDOWS
559 /** The old IRQL. Don't touch! */
560 unsigned char uchOldIrql;
561 /** Reserved, MBZ. */
562 uint8_t bReserved1;
563 /** Reserved, MBZ. */
564 uint8_t bReserved2;
565 /** Reserved, MBZ. */
566 uint8_t bReserved3;
567# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 255, 0, 0, 0 }
568# elif defined(RT_OS_HAIKU)
569 /** The cpu_state. Don't touch! */
570 uint32_t uOldCpuState;
571# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
572# elif defined(RT_OS_SOLARIS)
573 /** The Old PIL. Don't touch! */
574 uint32_t uOldPil;
575# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, UINT32_MAX }
576# else
577 /** Reserved, MBZ. */
578 uint32_t u32Reserved;
579# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
580# endif
581} RTTHREADPREEMPTSTATE;
582/** Pointer to a preemption state. */
583typedef RTTHREADPREEMPTSTATE *PRTTHREADPREEMPTSTATE;
584
585/**
586 * Disable preemption.
587 *
588 * A call to this function must be matched by exactly one call to
589 * RTThreadPreemptRestore().
590 *
591 * @param pState Where to store the preemption state.
592 */
593RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState);
594
595/**
596 * Restores the preemption state, undoing a previous call to
597 * RTThreadPreemptDisable.
598 *
599 * A call to this function must be matching a previous call to
600 * RTThreadPreemptDisable.
601 *
602 * @param pState The state return by RTThreadPreemptDisable.
603 */
604RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState);
605
606/**
607 * Check if the thread is executing in interrupt context.
608 *
609 * @returns true if in interrupt context, false if not.
610 * @param hThread Must be NIL_RTTHREAD for now.
611 */
612RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread);
613
614
615/**
616 * Thread context swithcing events.
617 */
618typedef enum RTTHREADCTXEVENT
619{
620 /** This thread is being scheduled out on the current CPU (includes preemption,
621 * waiting, sleep and whatever else may trigger scheduling). */
622 RTTHREADCTXEVENT_OUT = 0,
623 /** This thread is being scheduled in on the current CPU and will resume
624 * execution. */
625 RTTHREADCTXEVENT_IN,
626 /** The usual 32-bit size hack. */
627 RTTHREADCTXEVENT_32BIT_HACK = 0x7fffffff
628} RTTHREADCTXEVENT;
629
630/**
631 * Thread context switching hook callback.
632 *
633 * This hook function is called when a thread is scheduled and preempted. Check
634 * @a enmEvent to see which it is. Since the function is being called from
635 * hooks inside the scheduler, it is limited what you can do from this function.
636 * Do NOT acquire locks, sleep or yield the thread for instance. IRQ safe
637 * spinlocks are fine though.
638 *
639 * @returns IPRT status code.
640 * @param enmEvent The thread-context event. Please quitely ignore unknown
641 * events, we may add more (thread exit, ++) later.
642 * @param pvUser User argument.
643 */
644typedef DECLCALLBACK(void) FNRTTHREADCTXHOOK(RTTHREADCTXEVENT enmEvent, void *pvUser);
645/** Pointer to a context switching hook. */
646typedef FNRTTHREADCTXHOOK *PFNRTTHREADCTXHOOK;
647
648/**
649 * Initializes a thread context switching hook for the current thread.
650 *
651 * The hook is created as disabled, use RTThreadCtxHookEnable to enable it.
652 *
653 * @returns IPRT status code.
654 * @param phCtxHook Where to store the hook handle.
655 * @param fFlags Reserved for future extensions, must be zero.
656 * @param pfnCallback Pointer to a the hook function (callback) that
657 * should be called for all context switching events
658 * involving the current thread.
659 * @param pvUser User argument that will be passed to @a pfnCallback.
660 * @remarks Preemption must be enabled.
661 */
662RTDECL(int) RTThreadCtxHookCreate(PRTTHREADCTXHOOK phCtxHook, uint32_t fFlags, PFNRTTHREADCTXHOOK pfnCallback, void *pvUser);
663
664/**
665 * Destroys a thread context switching hook.
666 *
667 * Caller must make sure the hook is disabled before the final reference is
668 * released. Recommended to call this on the owning thread, otherwise the
669 * memory backing it may on some systems only be released when the thread
670 * terminates.
671 *
672 * @returns IPRT status code.
673 *
674 * @param hCtxHook The context hook handle. NIL_RTTHREADCTXHOOK is
675 * ignored and the function will return VINF_SUCCESS.
676 * @remarks Preemption must be enabled.
677 * @remarks Do not call from FNRTTHREADCTXHOOK.
678 */
679RTDECL(int) RTThreadCtxHookDestroy(RTTHREADCTXHOOK hCtxHook);
680
681/**
682 * Enables the context switching hooks for the current thread.
683 *
684 * @returns IPRT status code.
685 * @param hCtxHook The context hook handle.
686 * @remarks Should be called with preemption disabled.
687 */
688RTDECL(int) RTThreadCtxHookEnable(RTTHREADCTXHOOK hCtxHook);
689
690/**
691 * Disables the thread context switching hook for the current thread.
692 *
693 * Will not assert or fail if called twice or with a NIL handle.
694 *
695 * @returns IPRT status code.
696 * @param hCtxHook The context hook handle. NIL_RTTHREADCTXHOOK is
697 * ignored and the function wil return VINF_SUCCESS.
698 * @remarks Should be called with preemption disabled.
699 * @remarks Do not call from FNRTTHREADCTXHOOK.
700 */
701RTDECL(int) RTThreadCtxHookDisable(RTTHREADCTXHOOK hCtxHook);
702
703/**
704 * Is the thread context switching hook enabled?
705 *
706 * @returns true if registered, false if not supported or not registered.
707 * @param hCtxHook The context hook handle. NIL_RTTHREADCTXHOOK is
708 * ignored and the function will return false.
709 *
710 * @remarks Can be called from any thread, though is naturally subject to races
711 * when not called from the thread associated with the hook.
712 */
713RTDECL(bool) RTThreadCtxHookIsEnabled(RTTHREADCTXHOOK hCtxHook);
714
715# endif /* IN_RING0 */
716
717
718# ifdef IN_RING3
719
720/**
721 * Adopts a non-IPRT thread.
722 *
723 * @returns IPRT status code.
724 * @param enmType The thread type.
725 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
726 * @param pszName The thread name. Optional
727 * @param pThread Where to store the thread handle. Optional.
728 */
729RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
730
731/**
732 * Get the thread handle of the current thread, automatically adopting alien
733 * threads.
734 *
735 * @returns Thread handle.
736 */
737RTDECL(RTTHREAD) RTThreadSelfAutoAdopt(void);
738
739/**
740 * Gets the affinity mask of the current thread.
741 *
742 * @returns IPRT status code.
743 * @param pCpuSet Where to return the CPU affienty set of the calling
744 * thread.
745 */
746RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet);
747
748/**
749 * Sets the affinity mask of the current thread.
750 *
751 * @returns iprt status code.
752 * @param pCpuSet The set of CPUs this thread can run on. NULL means
753 * all CPUs.
754 */
755RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet);
756
757/**
758 * Binds the thread to one specific CPU.
759 *
760 * @returns iprt status code.
761 * @param idCpu The ID of the CPU to bind this thread to. Use
762 * NIL_RTCPUID to unbind it.
763 */
764RTR3DECL(int) RTThreadSetAffinityToCpu(RTCPUID idCpu);
765
766/**
767 * Unblocks a thread.
768 *
769 * This function is paired with RTThreadBlocking and RTThreadBlockingDebug.
770 *
771 * @param hThread The current thread.
772 * @param enmCurState The current state, used to check for nested blocking.
773 * The new state will be running.
774 */
775RTDECL(void) RTThreadUnblocked(RTTHREAD hThread, RTTHREADSTATE enmCurState);
776
777/**
778 * Change the thread state to blocking.
779 *
780 * @param hThread The current thread.
781 * @param enmState The sleep state.
782 * @param fReallySleeping Really going to sleep now. Use false before calls
783 * to other IPRT synchronization methods.
784 */
785RTDECL(void) RTThreadBlocking(RTTHREAD hThread, RTTHREADSTATE enmState, bool fReallySleeping);
786
787/**
788 * Get the current thread state.
789 *
790 * A thread that is reported as sleeping may actually still be running inside
791 * the lock validator or/and in the code of some other IPRT synchronization
792 * primitive. Use RTThreadGetReallySleeping
793 *
794 * @returns The thread state.
795 * @param hThread The thread.
796 */
797RTDECL(RTTHREADSTATE) RTThreadGetState(RTTHREAD hThread);
798
799/**
800 * Checks if the thread is really sleeping or not.
801 *
802 * @returns RTTHREADSTATE_RUNNING if not really sleeping, otherwise the state it
803 * is sleeping in.
804 * @param hThread The thread.
805 */
806RTDECL(RTTHREADSTATE) RTThreadGetReallySleeping(RTTHREAD hThread);
807
808/**
809 * Translate a thread state into a string.
810 *
811 * @returns Pointer to a read-only string containing the state name.
812 * @param enmState The state.
813 */
814RTDECL(const char *) RTThreadStateName(RTTHREADSTATE enmState);
815
816
817/**
818 * Native thread states returned by RTThreadNativeState.
819 */
820typedef enum RTTHREADNATIVESTATE
821{
822 /** Invalid thread handle. */
823 RTTHREADNATIVESTATE_INVALID = 0,
824 /** Unable to determine the thread state. */
825 RTTHREADNATIVESTATE_UNKNOWN,
826 /** The thread is running. */
827 RTTHREADNATIVESTATE_RUNNING,
828 /** The thread is blocked. */
829 RTTHREADNATIVESTATE_BLOCKED,
830 /** The thread is suspended / stopped. */
831 RTTHREADNATIVESTATE_SUSPENDED,
832 /** The thread has terminated. */
833 RTTHREADNATIVESTATE_TERMINATED,
834 /** Make sure it's a 32-bit type. */
835 RTTHREADNATIVESTATE_32BIT_HACK = 0x7fffffff
836} RTTHREADNATIVESTATE;
837
838
839/**
840 * Get the native state of a thread.
841 *
842 * @returns Native state.
843 * @param hThread The thread handle.
844 *
845 * @remarks Not yet implemented on all systems, so have a backup plan for
846 * RTTHREADNATIVESTATE_UNKNOWN.
847 */
848RTDECL(RTTHREADNATIVESTATE) RTThreadGetNativeState(RTTHREAD hThread);
849
850
851/**
852 * Get the execution times of the specified thread
853 *
854 * @returns IPRT status code.
855 * @param pKernelTime Kernel execution time in ms (out)
856 * @param pUserTime User execution time in ms (out)
857 *
858 */
859RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime);
860
861/** @name Thread Local Storage
862 * @{
863 */
864/**
865 * Thread termination callback for destroying a non-zero TLS entry.
866 *
867 * @remarks It is not permitable to use any RTTls APIs at this time. Doing so
868 * may lead to endless loops, crashes, and other bad stuff.
869 *
870 * @param pvValue The current value.
871 */
872typedef DECLCALLBACK(void) FNRTTLSDTOR(void *pvValue);
873/** Pointer to a FNRTTLSDTOR. */
874typedef FNRTTLSDTOR *PFNRTTLSDTOR;
875
876/**
877 * Allocates a TLS entry (index).
878 *
879 * Example code:
880 * @code
881 RTTLS g_iTls = NIL_RTTLS;
882
883 ...
884
885 // once for the process, allocate the TLS index
886 if (g_iTls == NIL_RTTLS)
887 g_iTls = RTTlsAlloc();
888
889 // set the thread-local value.
890 RTTlsSet(g_iTls, pMyData);
891
892 ...
893
894 // get the thread-local value
895 PMYDATA pMyData = (PMYDATA)RTTlsGet(g_iTls);
896
897 @endcode
898 *
899 * @returns the index of the allocated TLS entry.
900 * @returns NIL_RTTLS on failure.
901 */
902RTR3DECL(RTTLS) RTTlsAlloc(void);
903
904/**
905 * Variant of RTTlsAlloc that returns a status code.
906 *
907 * @returns IPRT status code.
908 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
909 * doesn't support this feature.
910 *
911 * @param piTls Where to store the index of the allocated TLS entry.
912 * This is set to NIL_RTTLS on failure.
913 * @param pfnDestructor Optional callback function for cleaning up on
914 * thread termination. WARNING! This feature may not
915 * be implemented everywhere.
916 */
917RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
918
919/**
920 * Frees a TLS entry.
921 *
922 * @returns IPRT status code.
923 * @param iTls The index of the TLS entry.
924 */
925RTR3DECL(int) RTTlsFree(RTTLS iTls);
926
927/**
928 * Get the (thread-local) value stored in a TLS entry.
929 *
930 * @returns value in given TLS entry.
931 * @retval NULL if RTTlsSet() has not yet been called on this thread, or if the
932 * TLS index is invalid.
933 *
934 * @param iTls The index of the TLS entry.
935 */
936RTR3DECL(void *) RTTlsGet(RTTLS iTls);
937
938/**
939 * Get the value stored in a TLS entry.
940 *
941 * @returns IPRT status code.
942 * @param iTls The index of the TLS entry.
943 * @param ppvValue Where to store the value. The value will be NULL if
944 * RTTlsSet has not yet been called on this thread.
945 */
946RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
947
948/**
949 * Set the value stored in an allocated TLS entry.
950 *
951 * @returns IPRT status.
952 * @param iTls The index of the TLS entry.
953 * @param pvValue The value to store.
954 *
955 * @remarks Note that NULL is considered a special value.
956 */
957RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
958
959/** @} */
960
961# endif /* IN_RING3 */
962# endif /* !IN_RC */
963
964/** @} */
965
966RT_C_DECLS_END
967
968#endif
969
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