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