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