3 * IPRT - Internal RTThread header.
7 * Copyright (C) 2006-2017 Oracle Corporation
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.
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.
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.
30 #include <iprt/types.h>
31 #include <iprt/thread.h>
34 # include <iprt/process.h>
35 # include <iprt/critsect.h>
37 #include "internal/lockvalidator.h"
38 #include "internal/magics.h"
39 #ifdef RT_WITH_ICONV_CACHE
40 # include "internal/string.h"
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
54 * Internal representation of a thread.
56 typedef struct RTTHREADINT
58 /** Avl node core - the key is the native thread id. */
60 /** Magic value (RTTHREADINT_MAGIC). */
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)
70 * This is not valid until the create function has returned! */
73 #if defined(RT_OS_LINUX) && defined(IN_RING3)
75 * This is not valid before rtThreadMain has been called by the new thread. */
78 #if defined(RT_OS_SOLARIS) && defined(IN_RING0)
79 /** Debug thread ID needed for thread_join. */
82 /** The user event semaphore. */
83 RTSEMEVENTMULTI EventUser
;
84 /** The terminated event semaphore. */
85 RTSEMEVENTMULTI EventTerminated
;
86 /** The thread type. */
88 /** The thread creation flags. (RTTHREADFLAGS) */
90 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
92 /** The result code. */
94 /** Thread function. */
95 PFNRTTHREAD pfnThread
;
96 /** Thread function argument. */
98 /** Actual stack size. */
101 /** The lock validator data. */
102 RTLOCKVALPERTHREAD LockValidator
;
103 #endif /* IN_RING3 */
104 #ifdef RT_WITH_ICONV_CACHE
105 /** Handle cache for iconv.
106 * @remarks ASSUMES sizeof(void *) >= sizeof(iconv_t). */
107 void *ahIconvs
[RTSTRICONV_END
];
109 #ifdef IPRT_WITH_GENERIC_TLS
110 /** The TLS entries for this thread. */
111 void *apvTlsEntries
[RTTHREAD_TLS_ENTRIES
];
114 char szName
[RTTHREAD_NAME_LEN
];
116 /** Pointer to the internal representation of a thread. */
117 typedef RTTHREADINT
*PRTTHREADINT
;
120 /** @name RTTHREADINT::fIntFlags Masks and Bits.
122 /** Set if the thread is an alien thread.
123 * Clear if the thread was created by IPRT. */
124 #define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
125 /** Set if the thread has terminated.
126 * Clear if the thread is running. */
127 #define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
128 /** This bit is set if the thread is in the AVL tree. */
129 #define RTTHREADINT_FLAG_IN_TREE_BIT 2
130 /** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
131 #define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
132 /** Set if it's the main thread. */
133 #define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
138 * Initialize the native part of the thread management.
140 * Generally a TLS entry will be allocated at this point (Ring-3).
142 * @returns iprt status code.
144 DECLHIDDEN(int) rtThreadNativeInit(void);
148 * Called when IPRT was first initialized in unobtrusive mode and later changed
151 * This is only applicable in ring-3.
153 DECLHIDDEN(void) rtThreadNativeReInitObtrusive(void);
157 * Create a native thread.
158 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
160 * @returns iprt status code.
161 * @param pThreadInt The thread data structure for the thread.
162 * @param pNativeThread Where to store the native thread identifier.
164 DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt
, PRTNATIVETHREAD pNativeThread
);
167 * Adopts a thread, this is called immediately after allocating the
170 * @param pThread Pointer to the thread structure.
172 DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread
);
175 * Called from rtThreadDestroy so that the TLS entry and any native data in the
176 * thread structure can be cleared.
178 * @param pThread The thread structure.
180 DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread
);
184 * Called to check whether the thread is still alive or not before we start
187 * This is a kludge to deal with windows threads being killed wholesale in
188 * certain process termination scenarios and we don't want to hang the last
189 * thread because it's waiting on the semaphore of a dead thread.
191 * @returns true if alive, false if not.
192 * @param pThread The thread structure.
194 DECLHIDDEN(bool) rtThreadNativeIsAliveKludge(PRTTHREADINT pThread
);
199 * Called from rtThreadWait when the last thread has completed in order to make
200 * sure it's all the way out of IPRT before RTR0Term is called.
202 * @param pThread The thread structure.
204 DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread
);
209 * Sets the priority of the thread according to the thread type
210 * and current process priority.
212 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
213 * the caller on a successful return.
215 * @returns iprt status code.
216 * @param pThread The thread in question.
217 * @param enmType The thread type.
218 * @remark Located in sched.
220 DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread
, RTTHREADTYPE enmType
);
223 # ifdef RT_OS_WINDOWS
225 * Callback for when a native thread is detaching.
227 * It give the Win32/64 backend a chance to terminate alien
230 DECLHIDDEN(void) rtThreadNativeDetach(void);
233 * Internal function for informing the debugger about a thread.
234 * @param pThread The thread. May differ from the calling thread.
236 DECLHIDDEN(void) rtThreadNativeInformDebugger(PRTTHREADINT pThread
);
238 #endif /* IN_RING3 */
242 DECLCALLBACK(DECLHIDDEN(int)) rtThreadMain(PRTTHREADINT pThread
, RTNATIVETHREAD NativeThread
, const char *pszThreadName
);
243 DECLHIDDEN(uint32_t) rtThreadRelease(PRTTHREADINT pThread
);
244 DECLHIDDEN(void) rtThreadTerminate(PRTTHREADINT pThread
, int rc
);
245 DECLHIDDEN(PRTTHREADINT
) rtThreadGetByNative(RTNATIVETHREAD NativeThread
);
246 DECLHIDDEN(PRTTHREADINT
) rtThreadGet(RTTHREAD Thread
);
247 DECLHIDDEN(int) rtThreadInit(void);
249 DECLHIDDEN(void) rtThreadReInitObtrusive(void);
251 DECLHIDDEN(void) rtThreadTerm(void);
252 DECLHIDDEN(void) rtThreadInsert(PRTTHREADINT pThread
, RTNATIVETHREAD NativeThread
);
254 DECLHIDDEN(int) rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority
);
255 #endif /* !IN_RING0 */
256 #ifdef IPRT_WITH_GENERIC_TLS
257 DECLHIDDEN(void) rtThreadClearTlsEntry(RTTLS iTls
);
258 DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread
); /* in tls-generic.cpp */
264 * Gets the thread state.
266 * @returns The thread state.
267 * @param pThread The thread.
269 DECLINLINE(RTTHREADSTATE
) rtThreadGetState(PRTTHREADINT pThread
)
271 return pThread
->enmState
;
275 * Sets the thread state.
277 * @param pThread The thread.
278 * @param enmNewState The new thread state.
280 DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread
, RTTHREADSTATE enmNewState
)
282 AssertCompile(sizeof(pThread
->enmState
) == sizeof(uint32_t));
283 ASMAtomicWriteU32((uint32_t volatile *)&pThread
->enmState
, enmNewState
);