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