VirtualBox

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

Last change on this file since 78382 was 77982, checked in by vboxsync, 6 years ago

IPRT: Addressed todo in rtSchedNativeCheckThreadTypes (sched-linux.cpp) about limiting the transition checking to thread types that are actually in use. bugref:7929

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