1 | /* $Id: thread.h 28903 2010-04-29 14:58:12Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Internal RTThread header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 represenation 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 | int rtThreadNativeInit(void);
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Create a native thread.
|
---|
144 | * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
|
---|
145 | *
|
---|
146 | * @returns iprt status code.
|
---|
147 | * @param pThreadInt The thread data structure for the thread.
|
---|
148 | * @param pNativeThread Where to store the native thread identifier.
|
---|
149 | */
|
---|
150 | int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Adopts a thread, this is called immediately after allocating the
|
---|
154 | * thread structure.
|
---|
155 | *
|
---|
156 | * @param pThread Pointer to the thread structure.
|
---|
157 | */
|
---|
158 | int rtThreadNativeAdopt(PRTTHREADINT pThread);
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Sets the priority of the thread according to the thread type
|
---|
162 | * and current process priority.
|
---|
163 | *
|
---|
164 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
165 | * the caller on a successful return.
|
---|
166 | *
|
---|
167 | * @returns iprt status code.
|
---|
168 | * @param pThread The thread in question.
|
---|
169 | * @param enmType The thread type.
|
---|
170 | * @remark Located in sched.
|
---|
171 | */
|
---|
172 | int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
|
---|
173 |
|
---|
174 | #ifdef IN_RING3
|
---|
175 | # ifdef RT_OS_WINDOWS
|
---|
176 | /**
|
---|
177 | * Callback for when a native thread is detaching.
|
---|
178 | *
|
---|
179 | * It give the Win32/64 backend a chance to terminate alien
|
---|
180 | * threads properly.
|
---|
181 | */
|
---|
182 | void rtThreadNativeDetach(void);
|
---|
183 | # endif
|
---|
184 | #endif /* !IN_RING0 */
|
---|
185 |
|
---|
186 |
|
---|
187 | /* thread.cpp */
|
---|
188 | int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
|
---|
189 | void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
|
---|
190 | const char *pszFile, unsigned uLine, RTUINTPTR uId);
|
---|
191 | void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
|
---|
192 | uint32_t rtThreadRelease(PRTTHREADINT pThread);
|
---|
193 | void rtThreadTerminate(PRTTHREADINT pThread, int rc);
|
---|
194 | PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
|
---|
195 | PRTTHREADINT rtThreadGet(RTTHREAD Thread);
|
---|
196 | int rtThreadInit(void);
|
---|
197 | void rtThreadTerm(void);
|
---|
198 | void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
199 | #ifdef IN_RING3
|
---|
200 | int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
|
---|
201 | #endif /* !IN_RING0 */
|
---|
202 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
203 | void rtThreadClearTlsEntry(RTTLS iTls);
|
---|
204 | void rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | #ifdef ___iprt_asm_h
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Gets the thread state.
|
---|
211 | *
|
---|
212 | * @returns The thread state.
|
---|
213 | * @param pThread The thread.
|
---|
214 | */
|
---|
215 | DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
|
---|
216 | {
|
---|
217 | return pThread->enmState;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Sets the thread state.
|
---|
222 | *
|
---|
223 | * @param pThread The thread.
|
---|
224 | * @param enmNewState The new thread state.
|
---|
225 | */
|
---|
226 | DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
|
---|
227 | {
|
---|
228 | AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
|
---|
229 | ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
|
---|
230 | }
|
---|
231 |
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | RT_C_DECLS_END
|
---|
235 |
|
---|
236 | #endif
|
---|