VirtualBox

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

Last change on this file since 94719 was 94719, checked in by vboxsync, 3 years ago

IPRT/thread.h: Made RTTHREAD_NAME_LEN public.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.7 KB
Line 
1/** @file
2 * IPRT - Threads.
3 */
4
5/*
6 * Copyright (C) 2006-2022 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_INCLUDED_thread_h
27#define IPRT_INCLUDED_thread_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#include <iprt/stdarg.h>
35
36
37RT_C_DECLS_BEGIN
38
39/** @defgroup grp_rt_thread RTThread - Thread Management
40 * @ingroup grp_rt
41 * @{
42 */
43
44/**
45 * The thread state.
46 */
47typedef enum RTTHREADSTATE
48{
49 /** The usual invalid 0 value. */
50 RTTHREADSTATE_INVALID = 0,
51 /** The thread is being initialized. */
52 RTTHREADSTATE_INITIALIZING,
53 /** The thread has terminated */
54 RTTHREADSTATE_TERMINATED,
55 /** Probably running. */
56 RTTHREADSTATE_RUNNING,
57
58 /** Waiting on a critical section. */
59 RTTHREADSTATE_CRITSECT,
60 /** Waiting on a event semaphore. */
61 RTTHREADSTATE_EVENT,
62 /** Waiting on a event multiple wakeup semaphore. */
63 RTTHREADSTATE_EVENT_MULTI,
64 /** Waiting on a fast mutex. */
65 RTTHREADSTATE_FAST_MUTEX,
66 /** Waiting on a mutex. */
67 RTTHREADSTATE_MUTEX,
68 /** Waiting on a read write semaphore, read (shared) access. */
69 RTTHREADSTATE_RW_READ,
70 /** Waiting on a read write semaphore, write (exclusive) access. */
71 RTTHREADSTATE_RW_WRITE,
72 /** The thread is sleeping. */
73 RTTHREADSTATE_SLEEP,
74 /** Waiting on a spin mutex. */
75 RTTHREADSTATE_SPIN_MUTEX,
76 /** End of the thread states. */
77 RTTHREADSTATE_END,
78
79 /** The usual 32-bit size hack. */
80 RTTHREADSTATE_32BIT_HACK = 0x7fffffff
81} RTTHREADSTATE;
82
83/** Checks if a thread state indicates that the thread is sleeping. */
84#define RTTHREAD_IS_SLEEPING(enmState) ((enmState) >= RTTHREADSTATE_CRITSECT)
85
86/**
87 * Thread types.
88 * Besides identifying the purpose of the thread, the thread type is
89 * used to select the scheduling properties.
90 *
91 * The types in are placed in a rough order of ascending priority.
92 */
93typedef enum RTTHREADTYPE
94{
95 /** Invalid type. */
96 RTTHREADTYPE_INVALID = 0,
97 /** Infrequent poller thread.
98 * This type of thread will sleep for the most of the time, and do
99 * infrequent polls on resources at 0.5 sec or higher intervals.
100 */
101 RTTHREADTYPE_INFREQUENT_POLLER,
102 /** Main heavy worker thread.
103 * Thread of this type is driving asynchronous tasks in the Main
104 * API which takes a long time and might involve a bit of CPU. Like
105 * for instance creating a fixed sized VDI.
106 */
107 RTTHREADTYPE_MAIN_HEAVY_WORKER,
108 /** The emulation thread type.
109 * While being a thread with very high workload it still is vital
110 * that it gets scheduled frequently. When possible all other thread
111 * types except DEFAULT and GUI should interrupt this one ASAP when
112 * they become ready.
113 */
114 RTTHREADTYPE_EMULATION,
115 /** The default thread type.
116 * Since it doesn't say much about the purpose of the thread
117 * nothing special is normally done to the scheduling. This type
118 * should be avoided.
119 * The main thread is registered with default type during RTR3Init()
120 * and that's what the default process priority is derived from.
121 */
122 RTTHREADTYPE_DEFAULT,
123 /** The GUI thread type
124 * The GUI normally have a low workload but is frequently scheduled
125 * to handle events. When possible the scheduler should not leave
126 * threads of this kind waiting for too long (~50ms).
127 */
128 RTTHREADTYPE_GUI,
129 /** Main worker thread.
130 * Thread of this type is driving asynchronous tasks in the Main API.
131 * In most cases this means little work an a lot of waiting.
132 */
133 RTTHREADTYPE_MAIN_WORKER,
134 /** VRDP I/O thread.
135 * These threads are I/O threads in the RDP server will hang around
136 * waiting for data, process it and pass it on.
137 */
138 RTTHREADTYPE_VRDP_IO,
139 /** The debugger type.
140 * Threads involved in servicing the debugger. It must remain
141 * responsive even when things are running wild in.
142 */
143 RTTHREADTYPE_DEBUGGER,
144 /** Message pump thread.
145 * Thread pumping messages from one thread/process to another
146 * thread/process. The workload is very small, most of the time
147 * it's blocked waiting for messages to be produced or processed.
148 * This type of thread will be favored after I/O threads.
149 */
150 RTTHREADTYPE_MSG_PUMP,
151 /** The I/O thread type.
152 * Doing I/O means shuffling data, waiting for request to arrive and
153 * for them to complete. The thread should be favored when competing
154 * with any other threads except timer threads.
155 */
156 RTTHREADTYPE_IO,
157 /** The timer thread type.
158 * A timer thread is mostly waiting for the timer to tick
159 * and then perform a little bit of work. Accuracy is important here,
160 * so the thread should be favoured over all threads. If premention can
161 * be configured at thread level, it could be made very short.
162 */
163 RTTHREADTYPE_TIMER,
164 /** Only used for validation. */
165 RTTHREADTYPE_END
166} RTTHREADTYPE;
167
168
169#if !defined(IN_RC) || defined(DOXYGEN_RUNNING)
170
171/**
172 * Checks if the IPRT thread component has been initialized.
173 *
174 * This is used to avoid calling into RTThread before the runtime has been
175 * initialized.
176 *
177 * @returns @c true if it's initialized, @c false if not.
178 */
179RTDECL(bool) RTThreadIsInitialized(void);
180
181/**
182 * Get the thread handle of the current thread.
183 *
184 * @returns Thread handle.
185 */
186RTDECL(RTTHREAD) RTThreadSelf(void);
187
188/**
189 * Get the native thread handle of the current thread.
190 *
191 * @returns Native thread handle.
192 */
193RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void);
194
195/**
196 * Millisecond granular sleep function.
197 *
198 * @returns VINF_SUCCESS on success.
199 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happened
200 * which interrupt the peaceful sleep.
201 * @param cMillies Number of milliseconds to sleep.
202 * 0 milliseconds means yielding the timeslice - deprecated!
203 * @remark See RTThreadNanoSleep() for sleeping for smaller periods of time.
204 */
205RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies);
206
207/**
208 * Millisecond granular sleep function, no logger calls.
209 *
210 * Same as RTThreadSleep, except it will never call into the IPRT logger. It
211 * can therefore safely be used in places where the logger is off limits, like
212 * at termination or init time. The electric fence heap is one consumer of
213 * this API.
214 *
215 * @returns VINF_SUCCESS on success.
216 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happened
217 * which interrupt the peaceful sleep.
218 * @param cMillies Number of milliseconds to sleep.
219 * 0 milliseconds means yielding the timeslice - deprecated!
220 */
221RTDECL(int) RTThreadSleepNoLog(RTMSINTERVAL cMillies);
222
223/**
224 * Yields the CPU.
225 *
226 * @returns true if we yielded.
227 * @returns false if it's probable that we didn't yield.
228 */
229RTDECL(bool) RTThreadYield(void);
230
231
232
233/**
234 * Thread function.
235 *
236 * @returns 0 on success.
237 * @param ThreadSelf Thread handle to this thread.
238 * @param pvUser User argument.
239 */
240typedef DECLCALLBACKTYPE(int, FNRTTHREAD,(RTTHREAD ThreadSelf, void *pvUser));
241/** Pointer to a FNRTTHREAD(). */
242typedef FNRTTHREAD *PFNRTTHREAD;
243
244/**
245 * Thread creation flags.
246 */
247typedef enum RTTHREADFLAGS
248{
249 /** This flag is used to keep the thread structure around so it can
250 * be waited on after termination. @sa RTThreadWait and
251 * RTThreadWaitNoResume. Not required for RTThreadUserWait and friends!
252 */
253 RTTHREADFLAGS_WAITABLE = RT_BIT(0),
254 /** The bit number corresponding to the RTTHREADFLAGS_WAITABLE mask. */
255 RTTHREADFLAGS_WAITABLE_BIT = 0,
256
257 /** Call CoInitializeEx w/ COINIT_MULTITHREADED, COINIT_DISABLE_OLE1DDE and
258 * COINIT_SPEED_OVER_MEMORY. Ignored on non-windows platforms. */
259 RTTHREADFLAGS_COM_MTA = RT_BIT(1),
260 /** Call CoInitializeEx w/ COINIT_APARTMENTTHREADED and
261 * COINIT_SPEED_OVER_MEMORY. Ignored on non-windows platforms. */
262 RTTHREADFLAGS_COM_STA = RT_BIT(2),
263
264 /** Mask all signals that we can mask. Ignored on most non-posix platforms.
265 * @note RTThreadPoke() will not necessarily work for a thread create with
266 * this flag. */
267 RTTHREADFLAGS_NO_SIGNALS = RT_BIT(3),
268
269 /** Mask of valid flags, use for validation. */
270 RTTHREADFLAGS_MASK = UINT32_C(0xf)
271} RTTHREADFLAGS;
272
273/** Max thread name length (including zero terminator). */
274#define RTTHREAD_NAME_LEN 16
275
276/**
277 * Create a new thread.
278 *
279 * @returns iprt status code.
280 * @param pThread Where to store the thread handle to the new thread. (optional)
281 * @param pfnThread The thread function.
282 * @param pvUser User argument.
283 * @param cbStack The size of the stack for the new thread.
284 * Use 0 for the default stack size.
285 * @param enmType The thread type. Used for deciding scheduling attributes
286 * of the thread.
287 * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
288 * @param pszName Thread name.
289 *
290 * @remark When called in Ring-0, this API will create a new kernel thread and not a thread in
291 * the context of the calling process.
292 */
293RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
294 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
295#ifndef RT_OS_LINUX /* XXX crashes genksyms at least on 32-bit Linux hosts */
296/** Pointer to a RTThreadCreate function. */
297typedef DECLCALLBACKPTR(int, PFNRTTHREADCREATE,(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
298 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName));
299#endif
300
301
302/**
303 * Create a new thread.
304 *
305 * Same as RTThreadCreate except the name is given in the RTStrPrintfV form.
306 *
307 * @returns iprt status code.
308 * @param pThread See RTThreadCreate.
309 * @param pfnThread See RTThreadCreate.
310 * @param pvUser See RTThreadCreate.
311 * @param cbStack See RTThreadCreate.
312 * @param enmType See RTThreadCreate.
313 * @param fFlags See RTThreadCreate.
314 * @param pszNameFmt Thread name format.
315 * @param va Format arguments.
316 */
317RTDECL(int) RTThreadCreateV(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
318 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, va_list va) RT_IPRT_FORMAT_ATTR(7, 0);
319
320/**
321 * Create a new thread.
322 *
323 * Same as RTThreadCreate except the name is given in the RTStrPrintf form.
324 *
325 * @returns iprt status code.
326 * @param pThread See RTThreadCreate.
327 * @param pfnThread See RTThreadCreate.
328 * @param pvUser See RTThreadCreate.
329 * @param cbStack See RTThreadCreate.
330 * @param enmType See RTThreadCreate.
331 * @param fFlags See RTThreadCreate.
332 * @param pszNameFmt Thread name format.
333 * @param ... Format arguments.
334 */
335RTDECL(int) RTThreadCreateF(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
336 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, ...) RT_IPRT_FORMAT_ATTR(7, 8);
337
338/**
339 * Gets the native thread id of a IPRT thread.
340 *
341 * @returns The native thread id.
342 * @param Thread The IPRT thread.
343 */
344RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
345
346/**
347 * Gets the native thread handle for a IPRT thread.
348 *
349 * @returns The thread handle. INVALID_HANDLE_VALUE on failure.
350 * @param hThread The IPRT thread handle.
351 *
352 * @note Windows only.
353 * @note Only valid after parent returns from the thread creation call.
354 */
355RTDECL(uintptr_t) RTThreadGetNativeHandle(RTTHREAD hThread);
356
357/**
358 * Gets the IPRT thread of a native thread.
359 *
360 * @returns The IPRT thread handle
361 * @returns NIL_RTTHREAD if not a thread known to IPRT.
362 * @param NativeThread The native thread handle/id.
363 */
364RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
365
366/**
367 * Changes the type of the specified thread.
368 *
369 * @returns iprt status code.
370 * @param Thread The thread which type should be changed.
371 * @param enmType The new thread type.
372 * @remark In Ring-0 it only works if Thread == RTThreadSelf().
373 */
374RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
375
376/**
377 * Wait for the thread to terminate, resume on interruption.
378 *
379 * @returns iprt status code.
380 * Will not return VERR_INTERRUPTED.
381 * @param Thread The thread to wait for.
382 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
383 * an indefinite wait.
384 * @param prc Where to store the return code of the thread. Optional.
385 */
386RTDECL(int) RTThreadWait(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
387
388/**
389 * Wait for the thread to terminate, return on interruption.
390 *
391 * @returns iprt status code.
392 * @param Thread The thread to wait for.
393 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
394 * an indefinite wait.
395 * @param prc Where to store the return code of the thread. Optional.
396 */
397RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
398
399/**
400 * Gets the name of the current thread thread.
401 *
402 * @returns Pointer to readonly name string.
403 * @returns NULL on failure.
404 */
405RTDECL(const char *) RTThreadSelfName(void);
406
407/**
408 * Gets the name of a thread.
409 *
410 * @returns Pointer to readonly name string.
411 * @returns NULL on failure.
412 * @param Thread Thread handle of the thread to query the name of.
413 */
414RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
415
416/**
417 * Gets the type of the specified thread.
418 *
419 * @returns The thread type.
420 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
421 * @param Thread The thread in question.
422 */
423RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
424
425/**
426 * Sets the name of a thread.
427 *
428 * @returns iprt status code.
429 * @param Thread Thread handle of the thread to query the name of.
430 * @param pszName The thread name.
431 */
432RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
433
434/**
435 * Checks if the specified thread is the main thread.
436 *
437 * @returns true if it is, false if it isn't.
438 *
439 * @param hThread The thread handle.
440 */
441RTDECL(bool) RTThreadIsMain(RTTHREAD hThread);
442
443/**
444 * Checks if the calling thread is known to IPRT.
445 *
446 * @returns @c true if it is, @c false if it isn't.
447 */
448RTDECL(bool) RTThreadIsSelfKnown(void);
449
450/**
451 * Checks if the calling thread is know to IPRT and is alive.
452 *
453 * @returns @c true if it is, @c false if it isn't.
454 */
455RTDECL(bool) RTThreadIsSelfAlive(void);
456
457#ifdef IN_RING0
458/**
459 * Checks whether the specified thread is terminating.
460 *
461 * @retval VINF_SUCCESS if not terminating.
462 * @retval VINF_THREAD_IS_TERMINATING if terminating.
463 * @retval VERR_INVALID_HANDLE if hThread is not NIL_RTTHREAD.
464 * @retval VERR_NOT_SUPPORTED if the OS doesn't provide ways to check.
465 *
466 * @param hThread The thread to query about, NIL_RTTHREAD is an alias for
467 * the calling thread. Must be NIL_RTTHREAD for now.
468 *
469 * @note Not suppored on all OSes, so check for VERR_NOT_SUPPORTED.
470 */
471RTDECL(int) RTThreadQueryTerminationStatus(RTTHREAD hThread);
472#endif
473
474/**
475 * Signal the user event.
476 *
477 * @returns iprt status code.
478 */
479RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
480
481/**
482 * Wait for the user event.
483 *
484 * @returns iprt status code.
485 * @param Thread The thread to wait for.
486 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
487 * an indefinite wait.
488 */
489RTDECL(int) RTThreadUserWait(RTTHREAD Thread, RTMSINTERVAL cMillies);
490
491/**
492 * Wait for the user event, return on interruption.
493 *
494 * @returns iprt status code.
495 * @param Thread The thread to wait for.
496 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
497 * an indefinite wait.
498 */
499RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies);
500
501/**
502 * Reset the user event.
503 *
504 * @returns iprt status code.
505 * @param Thread The thread to reset.
506 */
507RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
508
509/**
510 * Pokes the thread.
511 *
512 * This will wake up or/and signal the thread, attempting to interrupt whatever
513 * it's currently doing.
514 *
515 * The posixy version of this will send a signal to the thread, quite likely
516 * waking it up from normal sleeps, waits, and I/O. When IPRT is in
517 * non-obtrusive mode, the posixy version will definitely return
518 * VERR_NOT_IMPLEMENTED, and it may also do so if no usable signal was found.
519 *
520 * On Windows the thread will be alerted, waking it up from most sleeps and
521 * waits, but not probably very little in the I/O area (needs testing). On NT
522 * 3.50 and 3.1 VERR_NOT_IMPLEMENTED will be returned.
523 *
524 * @returns IPRT status code.
525 *
526 * @param hThread The thread to poke. This must not be the
527 * calling thread.
528 *
529 * @note This is *NOT* implemented on all platforms and may cause unresolved
530 * symbols during linking or VERR_NOT_IMPLEMENTED at runtime.
531 *
532 */
533RTDECL(int) RTThreadPoke(RTTHREAD hThread);
534
535/**
536 * Controls the masking of the signal used by RTThreadPoke on posix systems.
537 *
538 * This function is not available on non-posix systems.
539 *
540 * @returns IPRT status code.
541 *
542 * @param hThread The current thread.
543 * @param fEnable Whether to enable poking (unblock) or to disable it
544 * (block the signal).
545 */
546RTDECL(int) RTThreadControlPokeSignal(RTTHREAD hThread, bool fEnable);
547
548
549# ifdef IN_RING0
550
551/**
552 * Check if preemption is currently enabled or not for the current thread.
553 *
554 * @note This may return true even on systems where preemption isn't
555 * possible. In that case, it means no call to RTThreadPreemptDisable
556 * has been made and interrupts are still enabled.
557 *
558 * @returns true if preemption is enabled, false if preemetion is disabled.
559 * @param hThread Must be NIL_RTTHREAD for now.
560 */
561RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread);
562
563/**
564 * Check if preemption is pending for the current thread.
565 *
566 * This function should be called regularly when executing larger portions of
567 * code with preemption disabled.
568 *
569 * @returns true if pending, false if not.
570 * @param hThread Must be NIL_RTTHREAD for now.
571 *
572 * @note If called with interrupts disabled, the NT kernel may temporarily
573 * re-enable them while checking.
574 */
575RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread);
576
577/**
578 * Is RTThreadPreemptIsPending reliable?
579 *
580 * @returns true if reliable, false if not.
581 */
582RTDECL(bool) RTThreadPreemptIsPendingTrusty(void);
583
584/**
585 * Is preemption possible on this system.
586 *
587 * @returns true if possible, false if not.
588 */
589RTDECL(bool) RTThreadPreemptIsPossible(void);
590
591/**
592 * Preemption state saved by RTThreadPreemptDisable and used by
593 * RTThreadPreemptRestore to restore the previous state.
594 */
595typedef struct RTTHREADPREEMPTSTATE
596{
597 /** In debug builds this will be used to check for cpu migration. */
598 RTCPUID idCpu;
599# ifdef RT_OS_WINDOWS
600 /** The old IRQL. Don't touch! */
601 unsigned char uchOldIrql;
602 /** Reserved, MBZ. */
603 uint8_t bReserved1;
604 /** Reserved, MBZ. */
605 uint8_t bReserved2;
606 /** Reserved, MBZ. */
607 uint8_t bReserved3;
608# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 255, 0, 0, 0 }
609# elif defined(RT_OS_HAIKU)
610 /** The cpu_state. Don't touch! */
611 uint32_t uOldCpuState;
612# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
613# elif defined(RT_OS_SOLARIS)
614 /** The Old PIL. Don't touch! */
615 uint32_t uOldPil;
616# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, UINT32_MAX }
617# else
618 /** Reserved, MBZ. */
619 uint32_t u32Reserved;
620# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
621# endif
622} RTTHREADPREEMPTSTATE;
623/** Pointer to a preemption state. */
624typedef RTTHREADPREEMPTSTATE *PRTTHREADPREEMPTSTATE;
625
626/**
627 * Disable preemption.
628 *
629 * A call to this function must be matched by exactly one call to
630 * RTThreadPreemptRestore().
631 *
632 * @param pState Where to store the preemption state.
633 */
634RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState);
635
636/**
637 * Restores the preemption state, undoing a previous call to
638 * RTThreadPreemptDisable.
639 *
640 * A call to this function must be matching a previous call to
641 * RTThreadPreemptDisable.
642 *
643 * @param pState The state return by RTThreadPreemptDisable.
644 */
645RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState);
646
647/**
648 * Check if the thread is executing in interrupt context.
649 *
650 * @returns true if in interrupt context, false if not.
651 * @param hThread Must be NIL_RTTHREAD for now.
652 */
653RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread);
654
655
656/**
657 * Thread context swithcing events.
658 */
659typedef enum RTTHREADCTXEVENT
660{
661 /** This thread is being scheduled out on the current CPU (includes preemption,
662 * waiting, sleep and whatever else may trigger scheduling). */
663 RTTHREADCTXEVENT_OUT = 0,
664 /** This thread is being scheduled in on the current CPU and will resume
665 * execution. */
666 RTTHREADCTXEVENT_IN,
667 /** The usual 32-bit size hack. */
668 RTTHREADCTXEVENT_32BIT_HACK = 0x7fffffff
669} RTTHREADCTXEVENT;
670
671/**
672 * Thread context switching hook callback.
673 *
674 * This hook function is called when a thread is scheduled and preempted. Check
675 * @a enmEvent to see which it is. Since the function is being called from
676 * hooks inside the scheduler, it is limited what you can do from this function.
677 * Do NOT acquire locks, sleep or yield the thread for instance. IRQ safe
678 * spinlocks are fine though.
679 *
680 * @returns IPRT status code.
681 * @param enmEvent The thread-context event. Please quitely ignore unknown
682 * events, we may add more (thread exit, ++) later.
683 * @param pvUser User argument.
684 */
685typedef DECLCALLBACKTYPE(void, FNRTTHREADCTXHOOK,(RTTHREADCTXEVENT enmEvent, void *pvUser));
686/** Pointer to a context switching hook. */
687typedef FNRTTHREADCTXHOOK *PFNRTTHREADCTXHOOK;
688
689/**
690 * Initializes a thread context switching hook for the current thread.
691 *
692 * The hook is created as disabled, use RTThreadCtxHookEnable to enable it.
693 *
694 * @returns IPRT status code.
695 * @param phCtxHook Where to store the hook handle.
696 * @param fFlags Reserved for future extensions, must be zero.
697 * @param pfnCallback Pointer to a the hook function (callback) that
698 * should be called for all context switching events
699 * involving the current thread.
700 * @param pvUser User argument that will be passed to @a pfnCallback.
701 * @remarks Preemption must be enabled.
702 */
703RTDECL(int) RTThreadCtxHookCreate(PRTTHREADCTXHOOK phCtxHook, uint32_t fFlags, PFNRTTHREADCTXHOOK pfnCallback, void *pvUser);
704
705/**
706 * Destroys a thread context switching hook.
707 *
708 * Caller must make sure the hook is disabled before the final reference is
709 * released. Recommended to call this on the owning thread, otherwise the
710 * memory backing it may on some systems only be released when the thread
711 * terminates.
712 *
713 * @returns IPRT status code.
714 *
715 * @param hCtxHook The context hook handle. NIL_RTTHREADCTXHOOK is
716 * ignored and the function will return VINF_SUCCESS.
717 * @remarks Preemption must be enabled.
718 * @remarks Do not call from FNRTTHREADCTXHOOK.
719 */
720RTDECL(int) RTThreadCtxHookDestroy(RTTHREADCTXHOOK hCtxHook);
721
722/**
723 * Enables the context switching hooks for the current thread.
724 *
725 * @returns IPRT status code.
726 * @param hCtxHook The context hook handle.
727 * @remarks Should be called with preemption disabled.
728 */
729RTDECL(int) RTThreadCtxHookEnable(RTTHREADCTXHOOK hCtxHook);
730
731/**
732 * Disables the thread context switching hook for the current thread.
733 *
734 * Will not assert or fail if called twice or with a NIL handle.
735 *
736 * @returns IPRT status code.
737 * @param hCtxHook The context hook handle. NIL_RTTHREADCTXHOOK is
738 * ignored and the function wil return VINF_SUCCESS.
739 * @remarks Should be called with preemption disabled.
740 * @remarks Do not call from FNRTTHREADCTXHOOK.
741 */
742RTDECL(int) RTThreadCtxHookDisable(RTTHREADCTXHOOK hCtxHook);
743
744/**
745 * Is the thread context switching hook enabled?
746 *
747 * @returns true if registered, false if not supported or not registered.
748 * @param hCtxHook The context hook handle. NIL_RTTHREADCTXHOOK is
749 * ignored and the function will return false.
750 *
751 * @remarks Can be called from any thread, though is naturally subject to races
752 * when not called from the thread associated with the hook.
753 */
754RTDECL(bool) RTThreadCtxHookIsEnabled(RTTHREADCTXHOOK hCtxHook);
755
756# endif /* IN_RING0 */
757
758
759# ifdef IN_RING3
760
761/**
762 * Adopts a non-IPRT thread.
763 *
764 * @returns IPRT status code.
765 * @param enmType The thread type.
766 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
767 * @param pszName The thread name. Optional
768 * @param pThread Where to store the thread handle. Optional.
769 */
770RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
771
772/**
773 * Get the thread handle of the current thread, automatically adopting alien
774 * threads.
775 *
776 * @returns Thread handle.
777 */
778RTDECL(RTTHREAD) RTThreadSelfAutoAdopt(void);
779
780/**
781 * Gets the affinity mask of the current thread.
782 *
783 * @returns IPRT status code.
784 * @param pCpuSet Where to return the CPU affienty set of the calling
785 * thread.
786 */
787RTR3DECL(int) RTThreadGetAffinity(PRTCPUSET pCpuSet);
788
789/**
790 * Sets the affinity mask of the current thread.
791 *
792 * @returns iprt status code.
793 * @param pCpuSet The set of CPUs this thread can run on. NULL means
794 * all CPUs.
795 */
796RTR3DECL(int) RTThreadSetAffinity(PCRTCPUSET pCpuSet);
797
798/**
799 * Binds the thread to one specific CPU.
800 *
801 * @returns iprt status code.
802 * @param idCpu The ID of the CPU to bind this thread to. Use
803 * NIL_RTCPUID to unbind it.
804 */
805RTR3DECL(int) RTThreadSetAffinityToCpu(RTCPUID idCpu);
806
807/**
808 * Unblocks a thread.
809 *
810 * This function is paired with RTThreadBlocking and RTThreadBlockingDebug.
811 *
812 * @param hThread The current thread.
813 * @param enmCurState The current state, used to check for nested blocking.
814 * The new state will be running.
815 */
816RTDECL(void) RTThreadUnblocked(RTTHREAD hThread, RTTHREADSTATE enmCurState);
817
818/**
819 * Change the thread state to blocking.
820 *
821 * @param hThread The current thread.
822 * @param enmState The sleep state.
823 * @param fReallySleeping Really going to sleep now. Use false before calls
824 * to other IPRT synchronization methods.
825 */
826RTDECL(void) RTThreadBlocking(RTTHREAD hThread, RTTHREADSTATE enmState, bool fReallySleeping);
827
828/**
829 * Get the current thread state.
830 *
831 * A thread that is reported as sleeping may actually still be running inside
832 * the lock validator or/and in the code of some other IPRT synchronization
833 * primitive. Use RTThreadGetReallySleeping
834 *
835 * @returns The thread state.
836 * @param hThread The thread.
837 */
838RTDECL(RTTHREADSTATE) RTThreadGetState(RTTHREAD hThread);
839
840/**
841 * Checks if the thread is really sleeping or not.
842 *
843 * @returns RTTHREADSTATE_RUNNING if not really sleeping, otherwise the state it
844 * is sleeping in.
845 * @param hThread The thread.
846 */
847RTDECL(RTTHREADSTATE) RTThreadGetReallySleeping(RTTHREAD hThread);
848
849/**
850 * Translate a thread state into a string.
851 *
852 * @returns Pointer to a read-only string containing the state name.
853 * @param enmState The state.
854 */
855RTDECL(const char *) RTThreadStateName(RTTHREADSTATE enmState);
856
857
858/**
859 * Native thread states returned by RTThreadNativeState.
860 */
861typedef enum RTTHREADNATIVESTATE
862{
863 /** Invalid thread handle. */
864 RTTHREADNATIVESTATE_INVALID = 0,
865 /** Unable to determine the thread state. */
866 RTTHREADNATIVESTATE_UNKNOWN,
867 /** The thread is running. */
868 RTTHREADNATIVESTATE_RUNNING,
869 /** The thread is blocked. */
870 RTTHREADNATIVESTATE_BLOCKED,
871 /** The thread is suspended / stopped. */
872 RTTHREADNATIVESTATE_SUSPENDED,
873 /** The thread has terminated. */
874 RTTHREADNATIVESTATE_TERMINATED,
875 /** Make sure it's a 32-bit type. */
876 RTTHREADNATIVESTATE_32BIT_HACK = 0x7fffffff
877} RTTHREADNATIVESTATE;
878
879
880/**
881 * Get the native state of a thread.
882 *
883 * @returns Native state.
884 * @param hThread The thread handle.
885 *
886 * @remarks Not yet implemented on all systems, so have a backup plan for
887 * RTTHREADNATIVESTATE_UNKNOWN.
888 */
889RTDECL(RTTHREADNATIVESTATE) RTThreadGetNativeState(RTTHREAD hThread);
890
891
892/**
893 * Get the execution times of the specified thread
894 *
895 * @returns IPRT status code.
896 * @param pKernelTime Kernel execution time in ms (out)
897 * @param pUserTime User execution time in ms (out)
898 *
899 */
900RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime);
901
902/** @name Thread Local Storage
903 * @{
904 */
905/**
906 * Thread termination callback for destroying a non-zero TLS entry.
907 *
908 * @remarks It is not permitable to use any RTTls APIs at this time. Doing so
909 * may lead to endless loops, crashes, and other bad stuff.
910 *
911 * @param pvValue The current value.
912 */
913typedef DECLCALLBACKTYPE(void, FNRTTLSDTOR,(void *pvValue));
914/** Pointer to a FNRTTLSDTOR. */
915typedef FNRTTLSDTOR *PFNRTTLSDTOR;
916
917/**
918 * Allocates a TLS entry (index).
919 *
920 * Example code:
921 * @code
922 RTTLS g_iTls = NIL_RTTLS;
923
924 ...
925
926 // once for the process, allocate the TLS index
927 if (g_iTls == NIL_RTTLS)
928 g_iTls = RTTlsAlloc();
929
930 // set the thread-local value.
931 RTTlsSet(g_iTls, pMyData);
932
933 ...
934
935 // get the thread-local value
936 PMYDATA pMyData = (PMYDATA)RTTlsGet(g_iTls);
937
938 @endcode
939 *
940 * @returns the index of the allocated TLS entry.
941 * @returns NIL_RTTLS on failure.
942 */
943RTR3DECL(RTTLS) RTTlsAlloc(void);
944
945/**
946 * Variant of RTTlsAlloc that returns a status code.
947 *
948 * @returns IPRT status code.
949 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
950 * doesn't support this feature.
951 *
952 * @param piTls Where to store the index of the allocated TLS entry.
953 * This is set to NIL_RTTLS on failure.
954 * @param pfnDestructor Optional callback function for cleaning up on
955 * thread termination.
956 * @note In static builds on windows, the destructor will only be invoked for
957 * IPRT threads.
958 * @note There are probably OS specific restrictions on what operations you
959 * are allowed to perform from a TLS destructor, so keep it simple.
960 */
961RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
962
963/**
964 * Frees a TLS entry.
965 *
966 * @returns IPRT status code.
967 * @param iTls The index of the TLS entry.
968 */
969RTR3DECL(int) RTTlsFree(RTTLS iTls);
970
971/**
972 * Get the (thread-local) value stored in a TLS entry.
973 *
974 * @returns value in given TLS entry.
975 * @retval NULL if RTTlsSet() has not yet been called on this thread, or if the
976 * TLS index is invalid.
977 *
978 * @param iTls The index of the TLS entry.
979 */
980RTR3DECL(void *) RTTlsGet(RTTLS iTls);
981
982/**
983 * Get the value stored in a TLS entry.
984 *
985 * @returns IPRT status code.
986 * @param iTls The index of the TLS entry.
987 * @param ppvValue Where to store the value. The value will be NULL if
988 * RTTlsSet has not yet been called on this thread.
989 */
990RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
991
992/**
993 * Set the value stored in an allocated TLS entry.
994 *
995 * @returns IPRT status.
996 * @param iTls The index of the TLS entry.
997 * @param pvValue The value to store.
998 *
999 * @remarks Note that NULL is considered a special value.
1000 */
1001RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
1002
1003/** @} */
1004
1005# endif /* IN_RING3 */
1006#endif /* !IN_RC || defined(DOXYGEN_RUNNING) */
1007
1008/** @} */
1009
1010RT_C_DECLS_END
1011
1012#endif /* !IPRT_INCLUDED_thread_h */
1013
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