1 | /* $Id: thread.h 28800 2010-04-27 08:22:32Z 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 |
|
---|
40 | RT_C_DECLS_BEGIN
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | /** Max thread name length. */
|
---|
46 | #define RTTHREAD_NAME_LEN 16
|
---|
47 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
48 | /** The number of TLS entries for the generic implementation. */
|
---|
49 | # define RTTHREAD_TLS_ENTRIES 64
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Internal represenation of a thread.
|
---|
54 | */
|
---|
55 | typedef struct RTTHREADINT
|
---|
56 | {
|
---|
57 | /** Avl node core - the key is the native thread id. */
|
---|
58 | AVLPVNODECORE Core;
|
---|
59 | /** Magic value (RTTHREADINT_MAGIC). */
|
---|
60 | uint32_t u32Magic;
|
---|
61 | /** Reference counter. */
|
---|
62 | uint32_t volatile cRefs;
|
---|
63 | /** The current thread state. */
|
---|
64 | RTTHREADSTATE volatile enmState;
|
---|
65 | /** Set when really sleeping. */
|
---|
66 | bool volatile fReallySleeping;
|
---|
67 | #if defined(RT_OS_WINDOWS) && defined(IN_RING3)
|
---|
68 | /** The thread handle
|
---|
69 | * This is not valid until the create function has returned! */
|
---|
70 | uintptr_t hThread;
|
---|
71 | #endif
|
---|
72 | #if defined(RT_OS_LINUX) && defined(IN_RING3)
|
---|
73 | /** The thread ID.
|
---|
74 | * This is not valid before rtThreadMain has been called by the new thread. */
|
---|
75 | pid_t tid;
|
---|
76 | #endif
|
---|
77 | /** The user event semaphore. */
|
---|
78 | RTSEMEVENTMULTI EventUser;
|
---|
79 | /** The terminated event semaphore. */
|
---|
80 | RTSEMEVENTMULTI EventTerminated;
|
---|
81 | /** The thread type. */
|
---|
82 | RTTHREADTYPE enmType;
|
---|
83 | /** The thread creation flags. (RTTHREADFLAGS) */
|
---|
84 | unsigned fFlags;
|
---|
85 | /** Internal flags. (RTTHREADINT_FLAGS_ *) */
|
---|
86 | uint32_t fIntFlags;
|
---|
87 | /** The result code. */
|
---|
88 | int rc;
|
---|
89 | /** Thread function. */
|
---|
90 | PFNRTTHREAD pfnThread;
|
---|
91 | /** Thread function argument. */
|
---|
92 | void *pvUser;
|
---|
93 | /** Actual stack size. */
|
---|
94 | size_t cbStack;
|
---|
95 | #ifdef IN_RING3
|
---|
96 | /** The lock validator data. */
|
---|
97 | RTLOCKVALPERTHREAD LockValidator;
|
---|
98 | #endif /* IN_RING3 */
|
---|
99 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
100 | /** The TLS entries for this thread. */
|
---|
101 | void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
|
---|
102 | #endif
|
---|
103 | /** Thread name. */
|
---|
104 | char szName[RTTHREAD_NAME_LEN];
|
---|
105 | } RTTHREADINT;
|
---|
106 | /** Pointer to the internal representation of a thread. */
|
---|
107 | typedef RTTHREADINT *PRTTHREADINT;
|
---|
108 |
|
---|
109 |
|
---|
110 | /** @name RTTHREADINT::fIntFlags Masks and Bits.
|
---|
111 | * @{ */
|
---|
112 | /** Set if the thread is an alien thread.
|
---|
113 | * Clear if the thread was created by IPRT. */
|
---|
114 | #define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
|
---|
115 | /** Set if the thread has terminated.
|
---|
116 | * Clear if the thread is running. */
|
---|
117 | #define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
|
---|
118 | /** This bit is set if the thread is in the AVL tree. */
|
---|
119 | #define RTTHREADINT_FLAG_IN_TREE_BIT 2
|
---|
120 | /** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
|
---|
121 | #define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
|
---|
122 | /** Set if it's the main thread. */
|
---|
123 | #define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
|
---|
124 | /** @} */
|
---|
125 |
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Initialize the native part of the thread management.
|
---|
129 | *
|
---|
130 | * Generally a TLS entry will be allocated at this point (Ring-3).
|
---|
131 | *
|
---|
132 | * @returns iprt status code.
|
---|
133 | */
|
---|
134 | int rtThreadNativeInit(void);
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Create a native thread.
|
---|
138 | * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
|
---|
139 | *
|
---|
140 | * @returns iprt status code.
|
---|
141 | * @param pThreadInt The thread data structure for the thread.
|
---|
142 | * @param pNativeThread Where to store the native thread identifier.
|
---|
143 | */
|
---|
144 | int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Adopts a thread, this is called immediately after allocating the
|
---|
148 | * thread structure.
|
---|
149 | *
|
---|
150 | * @param pThread Pointer to the thread structure.
|
---|
151 | */
|
---|
152 | int rtThreadNativeAdopt(PRTTHREADINT pThread);
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Sets the priority of the thread according to the thread type
|
---|
156 | * and current process priority.
|
---|
157 | *
|
---|
158 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
159 | * the caller on a successful return.
|
---|
160 | *
|
---|
161 | * @returns iprt status code.
|
---|
162 | * @param pThread The thread in question.
|
---|
163 | * @param enmType The thread type.
|
---|
164 | * @remark Located in sched.
|
---|
165 | */
|
---|
166 | int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
|
---|
167 |
|
---|
168 | #ifdef IN_RING3
|
---|
169 | # ifdef RT_OS_WINDOWS
|
---|
170 | /**
|
---|
171 | * Callback for when a native thread is detaching.
|
---|
172 | *
|
---|
173 | * It give the Win32/64 backend a chance to terminate alien
|
---|
174 | * threads properly.
|
---|
175 | */
|
---|
176 | void rtThreadNativeDetach(void);
|
---|
177 | # endif
|
---|
178 | #endif /* !IN_RING0 */
|
---|
179 |
|
---|
180 |
|
---|
181 | /* thread.cpp */
|
---|
182 | int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
|
---|
183 | void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
|
---|
184 | const char *pszFile, unsigned uLine, RTUINTPTR uId);
|
---|
185 | void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
|
---|
186 | uint32_t rtThreadRelease(PRTTHREADINT pThread);
|
---|
187 | void rtThreadTerminate(PRTTHREADINT pThread, int rc);
|
---|
188 | PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
|
---|
189 | PRTTHREADINT rtThreadGet(RTTHREAD Thread);
|
---|
190 | int rtThreadInit(void);
|
---|
191 | void rtThreadTerm(void);
|
---|
192 | void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
193 | #ifdef IN_RING3
|
---|
194 | int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
|
---|
195 | #endif /* !IN_RING0 */
|
---|
196 | #ifdef IPRT_WITH_GENERIC_TLS
|
---|
197 | void rtThreadClearTlsEntry(RTTLS iTls);
|
---|
198 | void rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | #ifdef ___iprt_asm_h
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Gets the thread state.
|
---|
205 | *
|
---|
206 | * @returns The thread state.
|
---|
207 | * @param pThread The thread.
|
---|
208 | */
|
---|
209 | DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
|
---|
210 | {
|
---|
211 | return pThread->enmState;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Sets the thread state.
|
---|
216 | *
|
---|
217 | * @param pThread The thread.
|
---|
218 | * @param enmNewState The new thread state.
|
---|
219 | */
|
---|
220 | DECLINLINE(void) rtThreadSetState(PRTTHREADINT pThread, RTTHREADSTATE enmNewState)
|
---|
221 | {
|
---|
222 | AssertCompile(sizeof(pThread->enmState) == sizeof(uint32_t));
|
---|
223 | ASMAtomicWriteU32((uint32_t volatile *)&pThread->enmState, enmNewState);
|
---|
224 | }
|
---|
225 |
|
---|
226 | #endif
|
---|
227 |
|
---|
228 | RT_C_DECLS_END
|
---|
229 |
|
---|
230 | #endif
|
---|