VirtualBox

source: vbox/trunk/src/VBox/Runtime/include/internal/thread.h@ 25638

Last change on this file since 25638 was 25638, checked in by vboxsync, 15 years ago

iprt,pdmcritsect: Added RTSemEvent[Set|Add|Remove]Signaller so that we can validate who is signalling an event if we like and, more importantly, detect deadlocks involving event semaphores. More attempts at dealing with the races (and bugs) in the all-other-threads-blocking detection in tstRTLockValidator.cpp, adding RTThreadGetReallySleeping and RTThreadGetNativeState in the process.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette