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