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