VirtualBox

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

最後變更 在這個檔案從36555是 36555,由 vboxsync 提交於 14 年 前

Use DECLHIDDEN, especially in IPRT.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 23.4 KB
 
1/** @file
2 * IPRT - Threads.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.alldomusa.eu.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
74 /** The usual 32-bit size hack. */
75 RTTHREADSTATE_32BIT_HACK = 0x7fffffff
76} RTTHREADSTATE;
77
78/** Checks if a thread state indicates that the thread is sleeping. */
79#define RTTHREAD_IS_SLEEPING(enmState) ((enmState) >= RTTHREADSTATE_CRITSECT)
80
81/**
82 * Thread types.
83 * Besides identifying the purpose of the thread, the thread type is
84 * used to select the scheduling properties.
85 *
86 * The types in are placed in a rough order of ascending priority.
87 */
88typedef enum RTTHREADTYPE
89{
90 /** Invalid type. */
91 RTTHREADTYPE_INVALID = 0,
92 /** Infrequent poller thread.
93 * This type of thread will sleep for the most of the time, and do
94 * infrequent polls on resources at 0.5 sec or higher intervals.
95 */
96 RTTHREADTYPE_INFREQUENT_POLLER,
97 /** Main heavy worker thread.
98 * Thread of this type is driving asynchronous tasks in the Main
99 * API which takes a long time and might involve a bit of CPU. Like
100 * for instance creating a fixed sized VDI.
101 */
102 RTTHREADTYPE_MAIN_HEAVY_WORKER,
103 /** The emulation thread type.
104 * While being a thread with very high workload it still is vital
105 * that it gets scheduled frequently. When possible all other thread
106 * types except DEFAULT and GUI should interrupt this one ASAP when
107 * they become ready.
108 */
109 RTTHREADTYPE_EMULATION,
110 /** The default thread type.
111 * Since it doesn't say much about the purpose of the thread
112 * nothing special is normally done to the scheduling. This type
113 * should be avoided.
114 * The main thread is registered with default type during RTR3Init()
115 * and that's what the default process priority is derived from.
116 */
117 RTTHREADTYPE_DEFAULT,
118 /** The GUI thread type
119 * The GUI normally have a low workload but is frequently scheduled
120 * to handle events. When possible the scheduler should not leave
121 * threads of this kind waiting for too long (~50ms).
122 */
123 RTTHREADTYPE_GUI,
124 /** Main worker thread.
125 * Thread of this type is driving asynchronous tasks in the Main API.
126 * In most cases this means little work an a lot of waiting.
127 */
128 RTTHREADTYPE_MAIN_WORKER,
129 /** VRDP I/O thread.
130 * These threads are I/O threads in the RDP server will hang around
131 * waiting for data, process it and pass it on.
132 */
133 RTTHREADTYPE_VRDP_IO,
134 /** The debugger type.
135 * Threads involved in servicing the debugger. It must remain
136 * responsive even when things are running wild in.
137 */
138 RTTHREADTYPE_DEBUGGER,
139 /** Message pump thread.
140 * Thread pumping messages from one thread/process to another
141 * thread/process. The workload is very small, most of the time
142 * it's blocked waiting for messages to be procduced or processed.
143 * This type of thread will be favored after I/O threads.
144 */
145 RTTHREADTYPE_MSG_PUMP,
146 /** The I/O thread type.
147 * Doing I/O means shuffling data, waiting for request to arrive and
148 * for them to complete. The thread should be favored when competing
149 * with any other threads except timer threads.
150 */
151 RTTHREADTYPE_IO,
152 /** The timer thread type.
153 * A timer thread is mostly waiting for the timer to tick
154 * and then perform a little bit of work. Accuracy is important here,
155 * so the thread should be favoured over all threads. If premention can
156 * be configured at thread level, it could be made very short.
157 */
158 RTTHREADTYPE_TIMER,
159 /** Only used for validation. */
160 RTTHREADTYPE_END
161} RTTHREADTYPE;
162
163
164#ifndef IN_RC
165
166/**
167 * Get the thread handle of the current thread.
168 *
169 * @returns Thread handle.
170 */
171RTDECL(RTTHREAD) RTThreadSelf(void);
172
173/**
174 * Get the native thread handle of the current thread.
175 *
176 * @returns Native thread handle.
177 */
178RTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void);
179
180/**
181 * Millisecond granular sleep function.
182 *
183 * @returns VINF_SUCCESS on success.
184 * @returns VERR_INTERRUPTED if a signal or other asynchronous stuff happened
185 * which interrupt the peaceful sleep.
186 * @param cMillies Number of milliseconds to sleep.
187 * 0 milliseconds means yielding the timeslice - deprecated!
188 * @remark See RTThreadNanoSleep() for sleeping for smaller periods of time.
189 */
190RTDECL(int) RTThreadSleep(RTMSINTERVAL cMillies);
191
192/**
193 * Yields the CPU.
194 *
195 * @returns true if we yielded.
196 * @returns false if it's probable that we didn't yield.
197 */
198RTDECL(bool) RTThreadYield(void);
199
200
201
202/**
203 * Thread function.
204 *
205 * @returns 0 on success.
206 * @param ThreadSelf Thread handle to this thread.
207 * @param pvUser User argument.
208 */
209typedef DECLCALLBACK(int) FNRTTHREAD(RTTHREAD ThreadSelf, void *pvUser);
210/** Pointer to a FNRTTHREAD(). */
211typedef FNRTTHREAD *PFNRTTHREAD;
212
213/**
214 * Thread creation flags.
215 */
216typedef enum RTTHREADFLAGS
217{
218 /**
219 * This flag is used to keep the thread structure around so it can
220 * be waited on after termination.
221 */
222 RTTHREADFLAGS_WAITABLE = RT_BIT(0),
223 /** The bit number corresponding to the RTTHREADFLAGS_WAITABLE mask. */
224 RTTHREADFLAGS_WAITABLE_BIT = 0,
225
226 /** Mask of valid flags, use for validation. */
227 RTTHREADFLAGS_MASK = RT_BIT(0)
228} RTTHREADFLAGS;
229
230
231/**
232 * Create a new thread.
233 *
234 * @returns iprt status code.
235 * @param pThread Where to store the thread handle to the new thread. (optional)
236 * @param pfnThread The thread function.
237 * @param pvUser User argument.
238 * @param cbStack The size of the stack for the new thread.
239 * Use 0 for the default stack size.
240 * @param enmType The thread type. Used for deciding scheduling attributes
241 * of the thread.
242 * @param fFlags Flags of the RTTHREADFLAGS type (ORed together).
243 * @param pszName Thread name.
244 *
245 * @remark When called in Ring-0, this API will create a new kernel thread and not a thread in
246 * the context of the calling process.
247 */
248RTDECL(int) RTThreadCreate(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
249 RTTHREADTYPE enmType, unsigned fFlags, const char *pszName);
250
251/**
252 * Create a new thread.
253 *
254 * Same as RTThreadCreate except the name is given in the RTStrPrintfV form.
255 *
256 * @returns iprt status code.
257 * @param pThread See RTThreadCreate.
258 * @param pfnThread See RTThreadCreate.
259 * @param pvUser See RTThreadCreate.
260 * @param cbStack See RTThreadCreate.
261 * @param enmType See RTThreadCreate.
262 * @param fFlags See RTThreadCreate.
263 * @param pszName Thread name format.
264 * @param va Format arguments.
265 */
266RTDECL(int) RTThreadCreateV(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
267 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, va_list va);
268
269/**
270 * Create a new thread.
271 *
272 * Same as RTThreadCreate except the name is given in the RTStrPrintf form.
273 *
274 * @returns iprt status code.
275 * @param pThread See RTThreadCreate.
276 * @param pfnThread See RTThreadCreate.
277 * @param pvUser See RTThreadCreate.
278 * @param cbStack See RTThreadCreate.
279 * @param enmType See RTThreadCreate.
280 * @param fFlags See RTThreadCreate.
281 * @param pszName Thread name format.
282 * @param ... Format arguments.
283 */
284RTDECL(int) RTThreadCreateF(PRTTHREAD pThread, PFNRTTHREAD pfnThread, void *pvUser, size_t cbStack,
285 RTTHREADTYPE enmType, uint32_t fFlags, const char *pszNameFmt, ...);
286
287/**
288 * Gets the native thread id of a IPRT thread.
289 *
290 * @returns The native thread id.
291 * @param Thread The IPRT thread.
292 */
293RTDECL(RTNATIVETHREAD) RTThreadGetNative(RTTHREAD Thread);
294
295/**
296 * Gets the IPRT thread of a native thread.
297 *
298 * @returns The IPRT thread handle
299 * @returns NIL_RTTHREAD if not a thread known to IPRT.
300 * @param NativeThread The native thread handle/id.
301 */
302RTDECL(RTTHREAD) RTThreadFromNative(RTNATIVETHREAD NativeThread);
303
304/**
305 * Changes the type of the specified thread.
306 *
307 * @returns iprt status code.
308 * @param Thread The thread which type should be changed.
309 * @param enmType The new thread type.
310 * @remark In Ring-0 it only works if Thread == RTThreadSelf().
311 */
312RTDECL(int) RTThreadSetType(RTTHREAD Thread, RTTHREADTYPE enmType);
313
314/**
315 * Wait for the thread to terminate, resume on interruption.
316 *
317 * @returns iprt status code.
318 * Will not return VERR_INTERRUPTED.
319 * @param Thread The thread to wait for.
320 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
321 * an indefinite wait.
322 * @param prc Where to store the return code of the thread. Optional.
323 */
324RTDECL(int) RTThreadWait(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
325
326/**
327 * Wait for the thread to terminate, return on interruption.
328 *
329 * @returns iprt status code.
330 * @param Thread The thread to wait for.
331 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
332 * an indefinite wait.
333 * @param prc Where to store the return code of the thread. Optional.
334 */
335RTDECL(int) RTThreadWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies, int *prc);
336
337/**
338 * Gets the name of the current thread thread.
339 *
340 * @returns Pointer to readonly name string.
341 * @returns NULL on failure.
342 */
343RTDECL(const char *) RTThreadSelfName(void);
344
345/**
346 * Gets the name of a thread.
347 *
348 * @returns Pointer to readonly name string.
349 * @returns NULL on failure.
350 * @param Thread Thread handle of the thread to query the name of.
351 */
352RTDECL(const char *) RTThreadGetName(RTTHREAD Thread);
353
354/**
355 * Gets the type of the specified thread.
356 *
357 * @returns The thread type.
358 * @returns RTTHREADTYPE_INVALID if the thread handle is invalid.
359 * @param Thread The thread in question.
360 */
361RTDECL(RTTHREADTYPE) RTThreadGetType(RTTHREAD Thread);
362
363/**
364 * Sets the name of a thread.
365 *
366 * @returns iprt status code.
367 * @param Thread Thread handle of the thread to query the name of.
368 * @param pszName The thread name.
369 */
370RTDECL(int) RTThreadSetName(RTTHREAD Thread, const char *pszName);
371
372/**
373 * Checks if the specified thread is the main thread.
374 *
375 * @returns true if it is, false if it isn't.
376 *
377 * @param hThread The thread handle.
378 */
379RTDECL(bool) RTThreadIsMain(RTTHREAD hThread);
380
381/**
382 * Signal the user event.
383 *
384 * @returns iprt status code.
385 */
386RTDECL(int) RTThreadUserSignal(RTTHREAD Thread);
387
388/**
389 * Wait for the user event.
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 */
396RTDECL(int) RTThreadUserWait(RTTHREAD Thread, RTMSINTERVAL cMillies);
397
398/**
399 * Wait for the user event, return on interruption.
400 *
401 * @returns iprt status code.
402 * @param Thread The thread to wait for.
403 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT for
404 * an indefinite wait.
405 */
406RTDECL(int) RTThreadUserWaitNoResume(RTTHREAD Thread, RTMSINTERVAL cMillies);
407
408/**
409 * Reset the user event.
410 *
411 * @returns iprt status code.
412 * @param Thread The thread to reset.
413 */
414RTDECL(int) RTThreadUserReset(RTTHREAD Thread);
415
416/**
417 * Pokes the thread.
418 *
419 * This will signal the thread, attempting to interrupt whatever it's currently
420 * doing. This is *NOT* implemented on all platforms and may cause unresolved
421 * symbols during linking or VERR_NOT_IMPLEMENTED at runtime.
422 *
423 * @returns IPRT status code.
424 *
425 * @param hThread The thread to poke. This must not be the
426 * calling thread.
427 */
428RTDECL(int) RTThreadPoke(RTTHREAD hThread);
429
430# ifdef IN_RING0
431
432/**
433 * Check if preemption is currently enabled or not for the current thread.
434 *
435 * @note This may return true even on systems where preemption isn't
436 * possible. In that case, it means no call to RTThreadPreemptDisable
437 * has been made and interrupts are still enabled.
438 *
439 * @returns true if preemption is enabled, false if preemetion is disabled.
440 * @param hThread Must be NIL_RTTHREAD for now.
441 */
442RTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread);
443
444/**
445 * Check if preemption is pending for the current thread.
446 *
447 * This function should be called regularly when executing larger portions of
448 * code with preemption disabled.
449 *
450 * @returns true if pending, false if not.
451 * @param hThread Must be NIL_RTTHREAD for now.
452 */
453RTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread);
454
455/**
456 * Is RTThreadPreemptIsPending reliable?
457 *
458 * @returns true if reliable, false if not.
459 */
460RTDECL(bool) RTThreadPreemptIsPendingTrusty(void);
461
462/**
463 * Is preemption possible on this system.
464 *
465 * @returns true if possible, false if not.
466 */
467RTDECL(bool) RTThreadPreemptIsPossible(void);
468
469/**
470 * Preemption state saved by RTThreadPreemptDisable and used by
471 * RTThreadPreemptRestore to restore the previous state.
472 */
473typedef struct RTTHREADPREEMPTSTATE
474{
475 /** In debug builds this will be used to check for cpu migration. */
476 RTCPUID idCpu;
477# ifdef RT_OS_WINDOWS
478 /** The old IRQL. Don't touch! */
479 unsigned char uchOldIrql;
480 /** Reserved, MBZ. */
481 uint8_t bReserved1;
482 /** Reserved, MBZ. */
483 uint8_t bReserved2;
484 /** Reserved, MBZ. */
485 uint8_t bReserved3;
486# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 255, 0, 0, 0 }
487# elif defined(RT_OS_SOLARIS)
488 /** The Old PIL. Don't touch! */
489 uint32_t uOldPil;
490# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, UINT32_MAX }
491# else
492 /** Reserved, MBZ. */
493 uint32_t u32Reserved;
494# define RTTHREADPREEMPTSTATE_INITIALIZER { NIL_RTCPUID, 0 }
495# endif
496} RTTHREADPREEMPTSTATE;
497/** Pointer to a preemption state. */
498typedef RTTHREADPREEMPTSTATE *PRTTHREADPREEMPTSTATE;
499
500/**
501 * Disable preemption.
502 *
503 * A call to this function must be matched by exactly one call to
504 * RTThreadPreemptRestore().
505 *
506 * @param pState Where to store the preemption state.
507 */
508RTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState);
509
510/**
511 * Restores the preemption state, undoing a previous call to
512 * RTThreadPreemptDisable.
513 *
514 * A call to this function must be matching a previous call to
515 * RTThreadPreemptDisable.
516 *
517 * @param pState The state return by RTThreadPreemptDisable.
518 */
519RTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState);
520
521/**
522 * Check if the thread is executing in interrupt context.
523 *
524 * @returns true if in interrupt context, false if not.
525 * @param hThread Must be NIL_RTTHREAD for now.
526 */
527RTDECL(bool) RTThreadIsInInterrupt(RTTHREAD hThread);
528
529# endif /* IN_RING0 */
530
531
532# ifdef IN_RING3
533
534/**
535 * Adopts a non-IPRT thread.
536 *
537 * @returns IPRT status code.
538 * @param enmType The thread type.
539 * @param fFlags The thread flags. RTTHREADFLAGS_WAITABLE is not currently allowed.
540 * @param pszName The thread name. Optional
541 * @param pThread Where to store the thread handle. Optional.
542 */
543RTDECL(int) RTThreadAdopt(RTTHREADTYPE enmType, unsigned fFlags, const char *pszName, PRTTHREAD pThread);
544
545/**
546 * Get the thread handle of the current thread, automatically adopting alien
547 * threads.
548 *
549 * @returns Thread handle.
550 */
551RTDECL(RTTHREAD) RTThreadSelfAutoAdopt(void);
552
553/**
554 * Gets the affinity mask of the current thread.
555 *
556 * @returns The affinity mask (bit 0 = logical cpu 0).
557 */
558RTR3DECL(uint64_t) RTThreadGetAffinity(void);
559
560/**
561 * Sets the affinity mask of the current thread.
562 *
563 * @returns iprt status code.
564 * @param u64Mask Affinity mask (bit 0 = logical cpu 0).
565 */
566RTR3DECL(int) RTThreadSetAffinity(uint64_t u64Mask);
567
568/**
569 * Unblocks a thread.
570 *
571 * This function is paired with RTThreadBlocking and RTThreadBlockingDebug.
572 *
573 * @param hThread The current thread.
574 * @param enmCurState The current state, used to check for nested blocking.
575 * The new state will be running.
576 */
577RTDECL(void) RTThreadUnblocked(RTTHREAD hThread, RTTHREADSTATE enmCurState);
578
579/**
580 * Change the thread state to blocking.
581 *
582 * @param hThread The current thread.
583 * @param enmState The sleep state.
584 * @param fReallySleeping Really going to sleep now. Use false before calls
585 * to other IPRT synchronization methods.
586 */
587RTDECL(void) RTThreadBlocking(RTTHREAD hThread, RTTHREADSTATE enmState, bool fReallySleeping);
588
589/**
590 * Get the current thread state.
591 *
592 * A thread that is reported as sleeping may actually still be running inside
593 * the lock validator or/and in the code of some other IPRT synchronization
594 * primitive. Use RTThreadGetReallySleeping
595 *
596 * @returns The thread state.
597 * @param hThread The thread.
598 */
599RTDECL(RTTHREADSTATE) RTThreadGetState(RTTHREAD hThread);
600
601/**
602 * Checks if the thread is really sleeping or not.
603 *
604 * @returns RTTHREADSTATE_RUNNING if not really sleeping, otherwise the state it
605 * is sleeping in.
606 * @param hThread The thread.
607 */
608RTDECL(RTTHREADSTATE) RTThreadGetReallySleeping(RTTHREAD hThread);
609
610/**
611 * Translate a thread state into a string.
612 *
613 * @returns Pointer to a read-only string containing the state name.
614 * @param enmState The state.
615 */
616RTDECL(const char *) RTThreadStateName(RTTHREADSTATE enmState);
617
618
619/**
620 * Native thread states returned by RTThreadNativeState.
621 */
622typedef enum RTTHREADNATIVESTATE
623{
624 /** Invalid thread handle. */
625 RTTHREADNATIVESTATE_INVALID = 0,
626 /** Unable to determine the thread state. */
627 RTTHREADNATIVESTATE_UNKNOWN,
628 /** The thread is running. */
629 RTTHREADNATIVESTATE_RUNNING,
630 /** The thread is blocked. */
631 RTTHREADNATIVESTATE_BLOCKED,
632 /** The thread is suspended / stopped. */
633 RTTHREADNATIVESTATE_SUSPENDED,
634 /** The thread has terminated. */
635 RTTHREADNATIVESTATE_TERMINATED,
636 /** Make sure it's a 32-bit type. */
637 RTTHREADNATIVESTATE_32BIT_HACK = 0x7fffffff
638} RTTHREADNATIVESTATE;
639
640
641/**
642 * Get the native state of a thread.
643 *
644 * @returns Native state.
645 * @param hThread The thread handle.
646 *
647 * @remarks Not yet implemented on all systems, so have a backup plan for
648 * RTTHREADNATIVESTATE_UNKNOWN.
649 */
650RTDECL(RTTHREADNATIVESTATE) RTThreadGetNativeState(RTTHREAD hThread);
651
652
653/**
654 * Get the execution times of the specified thread
655 *
656 * @returns IPRT status code.
657 * @param pKernelTime Kernel execution time in ms (out)
658 * @param pUserTime User execution time in ms (out)
659 *
660 */
661RTR3DECL(int) RTThreadGetExecutionTimeMilli(uint64_t *pKernelTime, uint64_t *pUserTime);
662
663/** @name Thread Local Storage
664 * @{
665 */
666/**
667 * Thread termination callback for destroying a non-zero TLS entry.
668 *
669 * @remarks It is not permitable to use any RTTls APIs at this time. Doing so
670 * may lead to endless loops, crashes, and other bad stuff.
671 *
672 * @param pvValue The current value.
673 */
674typedef DECLCALLBACK(void) FNRTTLSDTOR(void *pvValue);
675/** Pointer to a FNRTTLSDTOR. */
676typedef FNRTTLSDTOR *PFNRTTLSDTOR;
677
678/**
679 * Allocates a TLS entry (index).
680 *
681 * Example code:
682 * @code
683 RTTLS g_iTls = NIL_RTTLS;
684
685 ...
686
687 // once for the process, allocate the TLS index
688 if (g_iTls == NIL_RTTLS)
689 g_iTls = RTTlsAlloc();
690
691 // set the thread-local value.
692 RTTlsSet(g_iTls, pMyData);
693
694 ...
695
696 // get the thread-local value
697 PMYDATA pMyData = (PMYDATA)RTTlsGet(g_iTls);
698
699 @endcode
700 *
701 * @returns the index of the allocated TLS entry.
702 * @returns NIL_RTTLS on failure.
703 */
704RTR3DECL(RTTLS) RTTlsAlloc(void);
705
706/**
707 * Variant of RTTlsAlloc that returns a status code.
708 *
709 * @returns IPRT status code.
710 * @retval VERR_NOT_SUPPORTED if pfnDestructor is non-NULL and the platform
711 * doesn't support this feature.
712 *
713 * @param piTls Where to store the index of the allocated TLS entry.
714 * This is set to NIL_RTTLS on failure.
715 * @param pfnDestructor Optional callback function for cleaning up on
716 * thread termination. WARNING! This feature may not
717 * be implemented everywhere.
718 */
719RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor);
720
721/**
722 * Frees a TLS entry.
723 *
724 * @returns IPRT status code.
725 * @param iTls The index of the TLS entry.
726 */
727RTR3DECL(int) RTTlsFree(RTTLS iTls);
728
729/**
730 * Get the (thread-local) value stored in a TLS entry.
731 *
732 * @returns value in given TLS entry.
733 * @retval NULL if RTTlsSet() has not yet been called on this thread, or if the
734 * TLS index is invalid.
735 *
736 * @param iTls The index of the TLS entry.
737 */
738RTR3DECL(void *) RTTlsGet(RTTLS iTls);
739
740/**
741 * Get the value stored in a TLS entry.
742 *
743 * @returns IPRT status code.
744 * @param iTls The index of the TLS entry.
745 * @param ppvValue Where to store the value. The value will be NULL if
746 * RTTlsSet has not yet been called on this thread.
747 */
748RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue);
749
750/**
751 * Set the value stored in an allocated TLS entry.
752 *
753 * @returns IPRT status.
754 * @param iTls The index of the TLS entry.
755 * @param pvValue The value to store.
756 *
757 * @remarks Note that NULL is considered a special value.
758 */
759RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue);
760
761/** @} */
762
763# endif /* IN_RING3 */
764# endif /* !IN_RC */
765
766/** @} */
767
768RT_C_DECLS_END
769
770#endif
771
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette