VirtualBox

source: vbox/trunk/src/VBox/Runtime/include/internal/thread.h

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.1 KB
Line 
1/* $Id: thread.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Internal RTThread header.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
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 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
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
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.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef IPRT_INCLUDED_INTERNAL_thread_h
38#define IPRT_INCLUDED_INTERNAL_thread_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#include <iprt/types.h>
44#include <iprt/thread.h>
45#include <iprt/avl.h>
46#ifdef IN_RING3
47# include <iprt/process.h>
48# include <iprt/critsect.h>
49#endif
50#include "internal/lockvalidator.h"
51#include "internal/magics.h"
52#ifdef RT_WITH_ICONV_CACHE
53# include "internal/string.h"
54#endif
55#if defined(IPRT_NO_CRT) && defined(IN_RING3)
56# include "internal/nocrt.h"
57#endif
58
59RT_C_DECLS_BEGIN
60
61
62#ifdef IPRT_WITH_GENERIC_TLS
63/** The number of TLS entries for the generic implementation. */
64# define RTTHREAD_TLS_ENTRIES 64
65#endif
66
67/**
68 * Internal representation of a thread.
69 */
70typedef 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;
80 /** Set when really sleeping. */
81 bool volatile fReallySleeping;
82#if defined(RT_OS_WINDOWS) && defined(IN_RING3)
83 /** The thread handle
84 * This is not valid until the create function has returned! */
85 uintptr_t hThread;
86#endif
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
92#if defined(RT_OS_SOLARIS) && defined(IN_RING0)
93 /** Debug thread ID needed for thread_join. */
94 uint64_t tid;
95#endif
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;
114#ifdef IN_RING3
115 /** The lock validator data. */
116 RTLOCKVALPERTHREAD LockValidator;
117#endif /* IN_RING3 */
118#ifdef RT_WITH_ICONV_CACHE
119 /** Handle cache for iconv.
120 * @remarks ASSUMES sizeof(void *) >= sizeof(iconv_t). */
121 void *ahIconvs[RTSTRICONV_END];
122#endif
123#ifdef IPRT_WITH_GENERIC_TLS
124 /** The TLS entries for this thread. */
125 void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
126#endif
127 /** Thread name. */
128 char szName[RTTHREAD_NAME_LEN];
129#if defined(IPRT_NO_CRT) && defined(IN_RING3)
130 /** No-CRT per thread data. */
131 RTNOCRTTHREADDATA NoCrt;
132#endif
133} RTTHREADINT;
134/** Pointer to the internal representation of a thread. */
135typedef RTTHREADINT *PRTTHREADINT;
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. */
142#define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
143/** Set if the thread has terminated.
144 * Clear if the thread is running. */
145#define RTTHREADINT_FLAGS_TERMINATED RT_BIT(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 */
149#define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
150/** Set if it's the main thread. */
151#define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
152/** @} */
153
154/** Counters for each thread type. */
155extern DECL_HIDDEN_DATA(uint32_t volatile) g_acRTThreadTypeStats[RTTHREADTYPE_END];
156
157
158/**
159 * Initialize the native part of the thread management.
160 *
161 * Generally a TLS entry will be allocated at this point (Ring-3).
162 *
163 * @returns iprt status code.
164 */
165DECLHIDDEN(int) rtThreadNativeInit(void);
166
167#ifdef IN_RING3
168/**
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 */
174DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void);
175#endif
176
177/**
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 */
185DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
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 */
193DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread);
194
195/**
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 */
201DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread);
202
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 */
215DECLHIDDEN(bool) rtThreadNativeIsAliveKludge(PRTTHREADINT pThread);
216#endif
217
218#ifdef IN_RING0
219/**
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 */
225DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread);
226#endif
227
228
229/**
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.
237 * @param pThread The thread in question.
238 * @param enmType The thread type.
239 * @remark Located in sched.
240 */
241DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
242
243#ifdef IN_RING3
244# ifdef RT_OS_WINDOWS
245/**
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 */
251DECLHIDDEN(void) rtThreadNativeDetach(void);
252
253/**
254 * Internal function for informing the debugger about a thread.
255 * @param pThread The thread. May differ from the calling thread.
256 */
257DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread);
258# endif
259#endif /* IN_RING3 */
260
261
262/* thread.cpp */
263DECL_HIDDEN_CALLBACK(int) rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
264DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread);
265DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread, int rc);
266DECLHIDDEN(PRTTHREADINT) rtThreadGetByNative(RTNATIVETHREAD NativeThread);
267DECLHIDDEN(PRTTHREADINT) rtThreadGet(RTTHREAD Thread);
268DECLHIDDEN(int) rtThreadInit(void);
269#ifdef IN_RING3
270DECLHIDDEN(void) rtThreadReInitObtrusive(void);
271#endif
272DECLHIDDEN(void) rtThreadTerm(void);
273DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
274#ifdef IN_RING3
275DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
276#endif /* !IN_RING0 */
277#ifdef IPRT_WITH_GENERIC_TLS
278DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls);
279DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
280#endif
281#ifdef RT_OS_WINDOWS
282DECLHIDDEN(void) rtThreadWinTlsDestruction(void); /* in tls-win.cpp */
283#endif
284
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
291DECLHIDDEN(bool) rtThreadPosixPriorityProxyStart(void);
292DECLHIDDEN(int) rtThreadPosixPriorityProxyCall(PRTTHREADINT pTargetThread, PFNRT pfnFunction, int cArgs, ...);
293# endif
294#endif
295
296#ifdef IPRT_INCLUDED_asm_h
297
298/**
299 * Gets the thread state.
300 *
301 * @returns The thread state.
302 * @param pThread The thread.
303 */
304DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
305{
306 return pThread->enmState;
307}
308
309/**
310 * Sets the thread state.
311 *
312 * @param pThread The thread.
313 * @param enmNewState The new thread state.
314 */
315DECLINLINE(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
323RT_C_DECLS_END
324
325#endif /* !IPRT_INCLUDED_INTERNAL_thread_h */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use