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