VirtualBox

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

Last change on this file since 96052 was 96052, checked in by vboxsync, 2 years ago

IPRT/nocrt: Added no-CRT per-thread data like errno and strtok state. Implemented strtol and errno accessor. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.8 KB
Line 
1/* $Id: thread.h 96052 2022-08-05 10:48:48Z 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.virtualbox.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#if defined(IPRT_NO_CRT) && defined(IN_RING3)
46# include "internal/nocrt.h"
47#endif
48
49RT_C_DECLS_BEGIN
50
51
52#ifdef IPRT_WITH_GENERIC_TLS
53/** The number of TLS entries for the generic implementation. */
54# define RTTHREAD_TLS_ENTRIES 64
55#endif
56
57/**
58 * Internal representation of a thread.
59 */
60typedef struct RTTHREADINT
61{
62 /** Avl node core - the key is the native thread id. */
63 AVLPVNODECORE Core;
64 /** Magic value (RTTHREADINT_MAGIC). */
65 uint32_t u32Magic;
66 /** Reference counter. */
67 uint32_t volatile cRefs;
68 /** The current thread state. */
69 RTTHREADSTATE volatile enmState;
70 /** Set when really sleeping. */
71 bool volatile fReallySleeping;
72#if defined(RT_OS_WINDOWS) && defined(IN_RING3)
73 /** The thread handle
74 * This is not valid until the create function has returned! */
75 uintptr_t hThread;
76#endif
77#if defined(RT_OS_LINUX) && defined(IN_RING3)
78 /** The thread ID.
79 * This is not valid before rtThreadMain has been called by the new thread. */
80 pid_t tid;
81#endif
82#if defined(RT_OS_SOLARIS) && defined(IN_RING0)
83 /** Debug thread ID needed for thread_join. */
84 uint64_t tid;
85#endif
86 /** The user event semaphore. */
87 RTSEMEVENTMULTI EventUser;
88 /** The terminated event semaphore. */
89 RTSEMEVENTMULTI EventTerminated;
90 /** The thread type. */
91 RTTHREADTYPE enmType;
92 /** The thread creation flags. (RTTHREADFLAGS) */
93 unsigned fFlags;
94 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
95 uint32_t fIntFlags;
96 /** The result code. */
97 int rc;
98 /** Thread function. */
99 PFNRTTHREAD pfnThread;
100 /** Thread function argument. */
101 void *pvUser;
102 /** Actual stack size. */
103 size_t cbStack;
104#ifdef IN_RING3
105 /** The lock validator data. */
106 RTLOCKVALPERTHREAD LockValidator;
107#endif /* IN_RING3 */
108#ifdef RT_WITH_ICONV_CACHE
109 /** Handle cache for iconv.
110 * @remarks ASSUMES sizeof(void *) >= sizeof(iconv_t). */
111 void *ahIconvs[RTSTRICONV_END];
112#endif
113#ifdef IPRT_WITH_GENERIC_TLS
114 /** The TLS entries for this thread. */
115 void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
116#endif
117 /** Thread name. */
118 char szName[RTTHREAD_NAME_LEN];
119#if defined(IPRT_NO_CRT) && defined(IN_RING3)
120 /** No-CRT per thread data. */
121 RTNOCRTTHREADDATA NoCrt;
122#endif
123} RTTHREADINT;
124/** Pointer to the internal representation of a thread. */
125typedef RTTHREADINT *PRTTHREADINT;
126
127
128/** @name RTTHREADINT::fIntFlags Masks and Bits.
129 * @{ */
130/** Set if the thread is an alien thread.
131 * Clear if the thread was created by IPRT. */
132#define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
133/** Set if the thread has terminated.
134 * Clear if the thread is running. */
135#define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
136/** This bit is set if the thread is in the AVL tree. */
137#define RTTHREADINT_FLAG_IN_TREE_BIT 2
138/** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
139#define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
140/** Set if it's the main thread. */
141#define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
142/** @} */
143
144/** Counters for each thread type. */
145extern DECL_HIDDEN_DATA(uint32_t volatile) g_acRTThreadTypeStats[RTTHREADTYPE_END];
146
147
148/**
149 * Initialize the native part of the thread management.
150 *
151 * Generally a TLS entry will be allocated at this point (Ring-3).
152 *
153 * @returns iprt status code.
154 */
155DECLHIDDEN(int) rtThreadNativeInit(void);
156
157#ifdef IN_RING3
158/**
159 * Called when IPRT was first initialized in unobtrusive mode and later changed
160 * to obtrustive.
161 *
162 * This is only applicable in ring-3.
163 */
164DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void);
165#endif
166
167/**
168 * Create a native thread.
169 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
170 *
171 * @returns iprt status code.
172 * @param pThreadInt The thread data structure for the thread.
173 * @param pNativeThread Where to store the native thread identifier.
174 */
175DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
176
177/**
178 * Adopts a thread, this is called immediately after allocating the
179 * thread structure.
180 *
181 * @param pThread Pointer to the thread structure.
182 */
183DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread);
184
185/**
186 * Called from rtThreadDestroy so that the TLS entry and any native data in the
187 * thread structure can be cleared.
188 *
189 * @param pThread The thread structure.
190 */
191DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread);
192
193#ifdef IN_RING3
194/**
195 * Called to check whether the thread is still alive or not before we start
196 * waiting.
197 *
198 * This is a kludge to deal with windows threads being killed wholesale in
199 * certain process termination scenarios and we don't want to hang the last
200 * thread because it's waiting on the semaphore of a dead thread.
201 *
202 * @returns true if alive, false if not.
203 * @param pThread The thread structure.
204 */
205DECLHIDDEN(bool) rtThreadNativeIsAliveKludge(PRTTHREADINT pThread);
206#endif
207
208#ifdef IN_RING0
209/**
210 * Called from rtThreadWait when the last thread has completed in order to make
211 * sure it's all the way out of IPRT before RTR0Term is called.
212 *
213 * @param pThread The thread structure.
214 */
215DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread);
216#endif
217
218
219/**
220 * Sets the priority of the thread according to the thread type
221 * and current process priority.
222 *
223 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
224 * the caller on a successful return.
225 *
226 * @returns iprt status code.
227 * @param pThread The thread in question.
228 * @param enmType The thread type.
229 * @remark Located in sched.
230 */
231DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
232
233#ifdef IN_RING3
234# ifdef RT_OS_WINDOWS
235/**
236 * Callback for when a native thread is detaching.
237 *
238 * It give the Win32/64 backend a chance to terminate alien
239 * threads properly.
240 */
241DECLHIDDEN(void) rtThreadNativeDetach(void);
242
243/**
244 * Internal function for informing the debugger about a thread.
245 * @param pThread The thread. May differ from the calling thread.
246 */
247DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread);
248# endif
249#endif /* IN_RING3 */
250
251
252/* thread.cpp */
253DECL_HIDDEN_CALLBACK(int) rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
254DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread);
255DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread, int rc);
256DECLHIDDEN(PRTTHREADINT) rtThreadGetByNative(RTNATIVETHREAD NativeThread);
257DECLHIDDEN(PRTTHREADINT) rtThreadGet(RTTHREAD Thread);
258DECLHIDDEN(int) rtThreadInit(void);
259#ifdef IN_RING3
260DECLHIDDEN(void) rtThreadReInitObtrusive(void);
261#endif
262DECLHIDDEN(void) rtThreadTerm(void);
263DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
264#ifdef IN_RING3
265DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
266#endif /* !IN_RING0 */
267#ifdef IPRT_WITH_GENERIC_TLS
268DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls);
269DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
270#endif
271#ifdef RT_OS_WINDOWS
272DECLHIDDEN(void) rtThreadWinTlsDestruction(void); /* in tls-win.cpp */
273#endif
274
275/* thread-posix.cpp */
276#ifdef IN_RING3
277# if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2) && !defined(RT_OS_DARWIN)
278# define RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
279# endif
280# ifdef RTTHREAD_POSIX_WITH_CREATE_PRIORITY_PROXY
281DECLHIDDEN(bool) rtThreadPosixPriorityProxyStart(void);
282DECLHIDDEN(int) rtThreadPosixPriorityProxyCall(PRTTHREADINT pTargetThread, PFNRT pfnFunction, int cArgs, ...);
283# endif
284#endif
285
286#ifdef IPRT_INCLUDED_asm_h
287
288/**
289 * Gets the thread state.
290 *
291 * @returns The thread state.
292 * @param pThread The thread.
293 */
294DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
295{
296 return pThread->enmState;
297}
298
299/**
300 * Sets the thread state.
301 *
302 * @param pThread The thread.
303 * @param enmNewState The new thread state.
304 */
305DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
306{
307 AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
308 ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
309}
310
311#endif
312
313RT_C_DECLS_END
314
315#endif /* !IPRT_INCLUDED_INTERNAL_thread_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette