1 | /* $Id: thread.h 94718 2022-04-27 09:27:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Internal RTThread header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef IPRT_INCLUDED_INTERNAL_thread_h
|
---|
28 | #define IPRT_INCLUDED_INTERNAL_thread_h
|
---|
29 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
30 | # pragma once
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <iprt/types.h>
|
---|
34 | #include <iprt/thread.h>
|
---|
35 | #include <iprt/avl.h>
|
---|
36 | #ifdef IN_RING3
|
---|
37 | # include <iprt/process.h>
|
---|
38 | # include <iprt/critsect.h>
|
---|
39 | #endif
|
---|
40 | #include "internal/lockvalidator.h"
|
---|
41 | #include "internal/magics.h"
|
---|
42 | #ifdef RT_WITH_ICONV_CACHE
|
---|
43 | # include "internal/string.h"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | RT_C_DECLS_BEGIN
|
---|
47 |
|
---|
48 |
|
---|
49 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
50 | /** The number of TLS entries for the generic implementation. */
|
---|
51 | # define RTTHREAD_TLS_ENTRIES 64
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Internal representation of a thread.
|
---|
56 | */
|
---|
57 | typedef struct RTTHREADINT
|
---|
58 | {
|
---|
59 | /** Avl node core - the key is the native thread id. */
|
---|
60 | AVLPVNODECORE Core;
|
---|
61 | /** Magic value (RTTHREADINT_MAGIC). */
|
---|
62 | uint32_t u32Magic;
|
---|
63 | /** Reference counter. */
|
---|
64 | uint32_t volatile cRefs;
|
---|
65 | /** The current thread state. */
|
---|
66 | RTTHREADSTATE volatile enmState;
|
---|
67 | /** Set when really sleeping. */
|
---|
68 | bool volatile fReallySleeping;
|
---|
69 | #if defined(RT_OS_WINDOWS) && defined(IN_RING3)
|
---|
70 | /** The thread handle
|
---|
71 | * This is not valid until the create function has returned! */
|
---|
72 | uintptr_t hThread;
|
---|
73 | #endif
|
---|
74 | #if defined(RT_OS_LINUX) && defined(IN_RING3)
|
---|
75 | /** The thread ID.
|
---|
76 | * This is not valid before rtThreadMain has been called by the new thread. */
|
---|
77 | pid_t tid;
|
---|
78 | #endif
|
---|
79 | #if defined(RT_OS_SOLARIS) && defined(IN_RING0)
|
---|
80 | /** Debug thread ID needed for thread_join. */
|
---|
81 | uint64_t tid;
|
---|
82 | #endif
|
---|
83 | /** The user event semaphore. */
|
---|
84 | RTSEMEVENTMULTI EventUser;
|
---|
85 | /** The terminated event semaphore. */
|
---|
86 | RTSEMEVENTMULTI EventTerminated;
|
---|
87 | /** The thread type. */
|
---|
88 | RTTHREADTYPE enmType;
|
---|
89 | /** The thread creation flags. (RTTHREADFLAGS) */
|
---|
90 | unsigned fFlags;
|
---|
91 | /** Internal flags. (RTTHREADINT_FLAGS_ *) */
|
---|
92 | uint32_t fIntFlags;
|
---|
93 | /** The result code. */
|
---|
94 | int rc;
|
---|
95 | /** Thread function. */
|
---|
96 | PFNRTTHREAD pfnThread;
|
---|
97 | /** Thread function argument. */
|
---|
98 | void *pvUser;
|
---|
99 | /** Actual stack size. */
|
---|
100 | size_t cbStack;
|
---|
101 | #ifdef IN_RING3
|
---|
102 | /** The lock validator data. */
|
---|
103 | RTLOCKVALPERTHREAD LockValidator;
|
---|
104 | #endif /* IN_RING3 */
|
---|
105 | #ifdef RT_WITH_ICONV_CACHE
|
---|
106 | /** Handle cache for iconv.
|
---|
107 | * @remarks ASSUMES sizeof(void *) >= sizeof(iconv_t). */
|
---|
108 | void *ahIconvs[RTSTRICONV_END];
|
---|
109 | #endif
|
---|
110 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
111 | /** The TLS entries for this thread. */
|
---|
112 | void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
|
---|
113 | #endif
|
---|
114 | /** Thread name. */
|
---|
115 | char szName[RTTHREAD_NAME_LEN];
|
---|
116 | } RTTHREADINT;
|
---|
117 | /** Pointer to the internal representation of a thread. */
|
---|
118 | typedef RTTHREADINT *PRTTHREADINT;
|
---|
119 |
|
---|
120 |
|
---|
121 | /** @name RTTHREADINT::fIntFlags Masks and Bits.
|
---|
122 | * @{ */
|
---|
123 | /** Set if the thread is an alien thread.
|
---|
124 | * Clear if the thread was created by IPRT. */
|
---|
125 | #define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
|
---|
126 | /** Set if the thread has terminated.
|
---|
127 | * Clear if the thread is running. */
|
---|
128 | #define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
|
---|
129 | /** This bit is set if the thread is in the AVL tree. */
|
---|
130 | #define RTTHREADINT_FLAG_IN_TREE_BIT 2
|
---|
131 | /** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
|
---|
132 | #define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
|
---|
133 | /** Set if it's the main thread. */
|
---|
134 | #define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
|
---|
135 | /** @} */
|
---|
136 |
|
---|
137 | /** Counters for each thread type. */
|
---|
138 | extern DECL_HIDDEN_DATA(uint32_t volatile) g_acRTThreadTypeStats[RTTHREADTYPE_END];
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Initialize the native part of the thread management.
|
---|
143 | *
|
---|
144 | * Generally a TLS entry will be allocated at this point (Ring-3).
|
---|
145 | *
|
---|
146 | * @returns iprt status code.
|
---|
147 | */
|
---|
148 | DECLHIDDEN(int) rtThreadNativeInit(void);
|
---|
149 |
|
---|
150 | #ifdef IN_RING3
|
---|
151 | /**
|
---|
152 | * Called when IPRT was first initialized in unobtrusive mode and later changed
|
---|
153 | * to obtrustive.
|
---|
154 | *
|
---|
155 | * This is only applicable in ring-3.
|
---|
156 | */
|
---|
157 | DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void);
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Create a native thread.
|
---|
162 | * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
|
---|
163 | *
|
---|
164 | * @returns iprt status code.
|
---|
165 | * @param pThreadInt The thread data structure for the thread.
|
---|
166 | * @param pNativeThread Where to store the native thread identifier.
|
---|
167 | */
|
---|
168 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Adopts a thread, this is called immediately after allocating the
|
---|
172 | * thread structure.
|
---|
173 | *
|
---|
174 | * @param pThread Pointer to the thread structure.
|
---|
175 | */
|
---|
176 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread);
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Called from rtThreadDestroy so that the TLS entry and any native data in the
|
---|
180 | * thread structure can be cleared.
|
---|
181 | *
|
---|
182 | * @param pThread The thread structure.
|
---|
183 | */
|
---|
184 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread);
|
---|
185 |
|
---|
186 | #ifdef IN_RING3
|
---|
187 | /**
|
---|
188 | * Called to check whether the thread is still alive or not before we start
|
---|
189 | * waiting.
|
---|
190 | *
|
---|
191 | * This is a kludge to deal with windows threads being killed wholesale in
|
---|
192 | * certain process termination scenarios and we don't want to hang the last
|
---|
193 | * thread because it's waiting on the semaphore of a dead thread.
|
---|
194 | *
|
---|
195 | * @returns true if alive, false if not.
|
---|
196 | * @param pThread The thread structure.
|
---|
197 | */
|
---|
198 | DECLHIDDEN(bool) rtThreadNativeIsAliveKludge(PRTTHREADINT pThread);
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | #ifdef IN_RING0
|
---|
202 | /**
|
---|
203 | * Called from rtThreadWait when the last thread has completed in order to make
|
---|
204 | * sure it's all the way out of IPRT before RTR0Term is called.
|
---|
205 | *
|
---|
206 | * @param pThread The thread structure.
|
---|
207 | */
|
---|
208 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread);
|
---|
209 | #endif
|
---|
210 |
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Sets the priority of the thread according to the thread type
|
---|
214 | * and current process priority.
|
---|
215 | *
|
---|
216 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
217 | * the caller on a successful return.
|
---|
218 | *
|
---|
219 | * @returns iprt status code.
|
---|
220 | * @param pThread The thread in question.
|
---|
221 | * @param enmType The thread type.
|
---|
222 | * @remark Located in sched.
|
---|
223 | */
|
---|
224 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
|
---|
225 |
|
---|
226 | #ifdef IN_RING3
|
---|
227 | # ifdef RT_OS_WINDOWS
|
---|
228 | /**
|
---|
229 | * Callback for when a native thread is detaching.
|
---|
230 | *
|
---|
231 | * It give the Win32/64 backend a chance to terminate alien
|
---|
232 | * threads properly.
|
---|
233 | */
|
---|
234 | DECLHIDDEN(void) rtThreadNativeDetach(void);
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Internal function for informing the debugger about a thread.
|
---|
238 | * @param pThread The thread. May differ from the calling thread.
|
---|
239 | */
|
---|
240 | DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread);
|
---|
241 | # endif
|
---|
242 | #endif /* IN_RING3 */
|
---|
243 |
|
---|
244 |
|
---|
245 | /* thread.cpp */
|
---|
246 | DECL_HIDDEN_CALLBACK(int) rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
|
---|
247 | DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread);
|
---|
248 | DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread, int rc);
|
---|
249 | DECLHIDDEN(PRTTHREADINT) rtThreadGetByNative(RTNATIVETHREAD NativeThread);
|
---|
250 | DECLHIDDEN(PRTTHREADINT) rtThreadGet(RTTHREAD Thread);
|
---|
251 | DECLHIDDEN(int) rtThreadInit(void);
|
---|
252 | #ifdef IN_RING3
|
---|
253 | DECLHIDDEN(void) rtThreadReInitObtrusive(void);
|
---|
254 | #endif
|
---|
255 | DECLHIDDEN(void) rtThreadTerm(void);
|
---|
256 | DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
257 | #ifdef IN_RING3
|
---|
258 | DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
|
---|
259 | #endif /* !IN_RING0 */
|
---|
260 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
261 | DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls);
|
---|
262 | DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
|
---|
263 | #endif
|
---|
264 | #ifdef RT_OS_WINDOWS
|
---|
265 | DECLHIDDEN(void) rtThreadWinTlsDestruction(void); /* in tls-win.cpp */
|
---|
266 | #endif
|
---|
267 |
|
---|
268 | /* thread-posix.cpp */
|
---|
269 | #ifdef IN_RING3
|
---|
270 | # if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2) && !defined(RT_OS_DARWIN)
|
---|
271 | # define RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
|
---|
272 | # endif
|
---|
273 | # ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
|
---|
274 | DECLHIDDEN(bool) rtThreadPosixPriorityProxyStart(void);
|
---|
275 | DECLHIDDEN(int) rtThreadPosixPriorityProxyCall(PRTTHREADINT pTargetThread, PFNRT pfnFunction, int cArgs, ...);
|
---|
276 | # endif
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | #ifdef IPRT_INCLUDED_asm_h
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Gets the thread state.
|
---|
283 | *
|
---|
284 | * @returns The thread state.
|
---|
285 | * @param pThread The thread.
|
---|
286 | */
|
---|
287 | DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
|
---|
288 | {
|
---|
289 | return pThread->enmState;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Sets the thread state.
|
---|
294 | *
|
---|
295 | * @param pThread The thread.
|
---|
296 | * @param enmNewState The new thread state.
|
---|
297 | */
|
---|
298 | DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
|
---|
299 | {
|
---|
300 | AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
|
---|
301 | ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
|
---|
302 | }
|
---|
303 |
|
---|
304 | #endif
|
---|
305 |
|
---|
306 | RT_C_DECLS_END
|
---|
307 |
|
---|
308 | #endif /* !IPRT_INCLUDED_INTERNAL_thread_h */
|
---|