1 | /* $Id: thread.h 52457 2014-08-22 09:05:42Z 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 | /**
|
---|
179 | * Sets the priority of the thread according to the thread type
|
---|
180 | * and current process priority.
|
---|
181 | *
|
---|
182 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
183 | * the caller on a successful return.
|
---|
184 | *
|
---|
185 | * @returns iprt status code.
|
---|
186 | * @param pThread The thread in question.
|
---|
187 | * @param enmType The thread type.
|
---|
188 | * @remark Located in sched.
|
---|
189 | */
|
---|
190 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
|
---|
191 |
|
---|
192 | #ifdef IN_RING3
|
---|
193 | # ifdef RT_OS_WINDOWS
|
---|
194 | /**
|
---|
195 | * Callback for when a native thread is detaching.
|
---|
196 | *
|
---|
197 | * It give the Win32/64 backend a chance to terminate alien
|
---|
198 | * threads properly.
|
---|
199 | */
|
---|
200 | DECLHIDDEN(void) rtThreadNativeDetach(void);
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Internal function for informing the debugger about a thread.
|
---|
204 | * @param pThread The thread. May differ from the calling thread.
|
---|
205 | */
|
---|
206 | DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread);
|
---|
207 | # endif
|
---|
208 | #endif /* IN_RING3 */
|
---|
209 |
|
---|
210 |
|
---|
211 | /* thread.cpp */
|
---|
212 | DECLCALLBACK(DECLHIDDEN(int)) rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
|
---|
213 | DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread);
|
---|
214 | DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread, int rc);
|
---|
215 | DECLHIDDEN(PRTTHREADINT) rtThreadGetByNative(RTNATIVETHREAD NativeThread);
|
---|
216 | DECLHIDDEN(PRTTHREADINT) rtThreadGet(RTTHREAD Thread);
|
---|
217 | DECLHIDDEN(int) rtThreadInit(void);
|
---|
218 | #ifdef IN_RING3
|
---|
219 | DECLHIDDEN(void) rtThreadReInitObtrusive(void);
|
---|
220 | #endif
|
---|
221 | DECLHIDDEN(void) rtThreadTerm(void);
|
---|
222 | DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
223 | #ifdef IN_RING3
|
---|
224 | DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
|
---|
225 | #endif /* !IN_RING0 */
|
---|
226 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
227 | DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls);
|
---|
228 | DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | #ifdef ___iprt_asm_h
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Gets the thread state.
|
---|
235 | *
|
---|
236 | * @returns The thread state.
|
---|
237 | * @param pThread The thread.
|
---|
238 | */
|
---|
239 | DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
|
---|
240 | {
|
---|
241 | return pThread->enmState;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Sets the thread state.
|
---|
246 | *
|
---|
247 | * @param pThread The thread.
|
---|
248 | * @param enmNewState The new thread state.
|
---|
249 | */
|
---|
250 | DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
|
---|
251 | {
|
---|
252 | AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
|
---|
253 | ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
|
---|
254 | }
|
---|
255 |
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | RT_C_DECLS_END
|
---|
259 |
|
---|
260 | #endif
|
---|