VirtualBox

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

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

IPRT,PDMCritSect: Lock validation can only be performed in ring-3; fixed #PF on 32-bit darwin with debug builds. Hopefully fixed the recursion issue on windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: thread.h 25406 2009-12-15 14:23:53Z 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#if defined(RT_OS_WINDOWS) && defined(IN_RING3)
70 /** The thread handle
71 * This is not valid until the create function has returned! */
72 uintptr_t hThread;
73#endif
74 /** The user event semaphore. */
75 RTSEMEVENTMULTI EventUser;
76 /** The terminated event semaphore. */
77 RTSEMEVENTMULTI EventTerminated;
78 /** The thread type. */
79 RTTHREADTYPE enmType;
80 /** The thread creation flags. (RTTHREADFLAGS) */
81 unsigned fFlags;
82 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
83 uint32_t fIntFlags;
84 /** The result code. */
85 int rc;
86 /** Thread function. */
87 PFNRTTHREAD pfnThread;
88 /** Thread function argument. */
89 void *pvUser;
90 /** Actual stack size. */
91 size_t cbStack;
92#ifdef IN_RING3
93 /** The lock validator data. */
94 RTLOCKVALIDATORPERTHREAD LockValidator;
95#endif /* IN_RING3 */
96#ifdef IPRT_WITH_GENERIC_TLS
97 /** The TLS entries for this thread. */
98 void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
99#endif
100 /** Thread name. */
101 char szName[RTTHREAD_NAME_LEN];
102} RTTHREADINT, *PRTTHREADINT;
103
104
105/** @name RTTHREADINT::fIntFlags Masks and Bits.
106 * @{ */
107/** Set if the thread is an alien thread.
108 * Clear if the thread was created by IPRT. */
109#define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
110/** Set if the thread has terminated.
111 * Clear if the thread is running. */
112#define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
113/** This bit is set if the thread is in the AVL tree. */
114#define RTTHREADINT_FLAG_IN_TREE_BIT 2
115/** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
116#define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
117/** Set if it's the main thread. */
118#define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
119/** @} */
120
121
122/**
123 * Initialize the native part of the thread management.
124 *
125 * Generally a TLS entry will be allocated at this point (Ring-3).
126 *
127 * @returns iprt status code.
128 */
129int rtThreadNativeInit(void);
130
131/**
132 * Create a native thread.
133 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
134 *
135 * @returns iprt status code.
136 * @param pThreadInt The thread data structure for the thread.
137 * @param pNativeThread Where to store the native thread identifier.
138 */
139int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
140
141/**
142 * Adopts a thread, this is called immediately after allocating the
143 * thread structure.
144 *
145 * @param pThread Pointer to the thread structure.
146 */
147int rtThreadNativeAdopt(PRTTHREADINT pThread);
148
149/**
150 * Sets the priority of the thread according to the thread type
151 * and current process priority.
152 *
153 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
154 * the caller on a successful return.
155 *
156 * @returns iprt status code.
157 * @param pThread The thread in question.
158 * @param enmType The thread type.
159 * @remark Located in sched.
160 */
161int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
162
163#ifdef IN_RING3
164# ifdef RT_OS_WINDOWS
165/**
166 * Callback for when a native thread is detaching.
167 *
168 * It give the Win32/64 backend a chance to terminate alien
169 * threads properly.
170 */
171void rtThreadNativeDetach(void);
172# endif
173#endif /* !IN_RING0 */
174
175
176/* thread.cpp */
177int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
178void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
179 const char *pszFile, unsigned uLine, RTUINTPTR uId);
180void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
181uint32_t rtThreadRelease(PRTTHREADINT pThread);
182void rtThreadTerminate(PRTTHREADINT pThread, int rc);
183PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
184PRTTHREADINT rtThreadGet(RTTHREAD Thread);
185int rtThreadInit(void);
186void rtThreadTerm(void);
187void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
188#ifdef IN_RING3
189int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
190#endif /* !IN_RING0 */
191#ifdef IPRT_WITH_GENERIC_TLS
192void rtThreadClearTlsEntry(RTTLS iTls);
193void rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
194#endif
195
196/**
197 * Gets the thread state.
198 *
199 * @returns The thread state.
200 * @param pThread The thread.
201 */
202DECLINLINE(RTTHREADSTATE) rtThreadGetState(PRTTHREADINT pThread)
203{
204 return pThread->enmState;
205}
206
207RT_C_DECLS_END
208
209#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