VirtualBox

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

Last change on this file since 77231 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.1 KB
Line 
1/* $Id: thread.h 76585 2019-01-01 06:31:29Z 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
140/**
141 * Initialize the native part of the thread management.
142 *
143 * Generally a TLS entry will be allocated at this point (Ring-3).
144 *
145 * @returns iprt status code.
146 */
147DECLHIDDEN(int) rtThreadNativeInit(void);
148
149#ifdef IN_RING3
150/**
151 * Called when IPRT was first initialized in unobtrusive mode and later changed
152 * to obtrustive.
153 *
154 * This is only applicable in ring-3.
155 */
156DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void);
157#endif
158
159/**
160 * Create a native thread.
161 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
162 *
163 * @returns iprt status code.
164 * @param pThreadInt The thread data structure for the thread.
165 * @param pNativeThread Where to store the native thread identifier.
166 */
167DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
168
169/**
170 * Adopts a thread, this is called immediately after allocating the
171 * thread structure.
172 *
173 * @param pThread Pointer to the thread structure.
174 */
175DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread);
176
177/**
178 * Called from rtThreadDestroy so that the TLS entry and any native data in the
179 * thread structure can be cleared.
180 *
181 * @param pThread The thread structure.
182 */
183DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread);
184
185#ifdef IN_RING3
186/**
187 * Called to check whether the thread is still alive or not before we start
188 * waiting.
189 *
190 * This is a kludge to deal with windows threads being killed wholesale in
191 * certain process termination scenarios and we don't want to hang the last
192 * thread because it's waiting on the semaphore of a dead thread.
193 *
194 * @returns true if alive, false if not.
195 * @param pThread The thread structure.
196 */
197DECLHIDDEN(bool) rtThreadNativeIsAliveKludge(PRTTHREADINT pThread);
198#endif
199
200#ifdef IN_RING0
201/**
202 * Called from rtThreadWait when the last thread has completed in order to make
203 * sure it's all the way out of IPRT before RTR0Term is called.
204 *
205 * @param pThread The thread structure.
206 */
207DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread);
208#endif
209
210
211/**
212 * Sets the priority of the thread according to the thread type
213 * and current process priority.
214 *
215 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
216 * the caller on a successful return.
217 *
218 * @returns iprt status code.
219 * @param pThread The thread in question.
220 * @param enmType The thread type.
221 * @remark Located in sched.
222 */
223DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
224
225#ifdef IN_RING3
226# ifdef RT_OS_WINDOWS
227/**
228 * Callback for when a native thread is detaching.
229 *
230 * It give the Win32/64 backend a chance to terminate alien
231 * threads properly.
232 */
233DECLHIDDEN(void) rtThreadNativeDetach(void);
234
235/**
236 * Internal function for informing the debugger about a thread.
237 * @param pThread The thread. May differ from the calling thread.
238 */
239DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread);
240# endif
241#endif /* IN_RING3 */
242
243
244/* thread.cpp */
245DECLCALLBACK(DECLHIDDEN(int)) rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
246DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread);
247DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread, int rc);
248DECLHIDDEN(PRTTHREADINT) rtThreadGetByNative(RTNATIVETHREAD NativeThread);
249DECLHIDDEN(PRTTHREADINT) rtThreadGet(RTTHREAD Thread);
250DECLHIDDEN(int) rtThreadInit(void);
251#ifdef IN_RING3
252DECLHIDDEN(void) rtThreadReInitObtrusive(void);
253#endif
254DECLHIDDEN(void) rtThreadTerm(void);
255DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
256#ifdef IN_RING3
257DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
258#endif /* !IN_RING0 */
259#ifdef IPRT_WITH_GENERIC_TLS
260DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls);
261DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
262#endif
263
264#ifdef IPRT_INCLUDED_asm_h
265
266/**
267 * Gets the thread state.
268 *
269 * @returns The thread state.
270 * @param pThread The thread.
271 */
272DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
273{
274 return pThread->enmState;
275}
276
277/**
278 * Sets the thread state.
279 *
280 * @param pThread The thread.
281 * @param enmNewState The new thread state.
282 */
283DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
284{
285 AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
286 ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
287}
288
289#endif
290
291RT_C_DECLS_END
292
293#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