VirtualBox

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

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

IPRT: Added RTThreadIsMain().

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: thread.h 23124 2009-09-18 11:52:32Z 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/magics.h"
42
43RT_C_DECLS_BEGIN
44
45
46
47
48/** Max thread name length. */
49#define RTTHREAD_NAME_LEN 16
50#ifdef IPRT_WITH_GENERIC_TLS
51/** The number of TLS entries for the generic implementation. */
52# define RTTHREAD_TLS_ENTRIES 64
53#endif
54
55/**
56 * Internal represenation of a thread.
57 */
58typedef struct RTTHREADINT
59{
60 /** Avl node core - the key is the native thread id. */
61 AVLPVNODECORE Core;
62 /** Magic value (RTTHREADINT_MAGIC). */
63 uint32_t u32Magic;
64 /** Reference counter. */
65 uint32_t volatile cRefs;
66 /** The current thread state. */
67 RTTHREADSTATE volatile enmState;
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 /** The user event semaphore. */
74 RTSEMEVENTMULTI EventUser;
75 /** The terminated event semaphore. */
76 RTSEMEVENTMULTI EventTerminated;
77 /** The thread type. */
78 RTTHREADTYPE enmType;
79 /** The thread creation flags. (RTTHREADFLAGS) */
80 unsigned fFlags;
81 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
82 uint32_t fIntFlags;
83 /** The result code. */
84 int rc;
85 /** Thread function. */
86 PFNRTTHREAD pfnThread;
87 /** Thread function argument. */
88 void *pvUser;
89 /** Actual stack size. */
90 size_t cbStack;
91#ifdef IN_RING3
92 /** What we're blocking on. */
93 union RTTHREADINTBLOCKID
94 {
95 uint64_t u64;
96 PRTCRITSECT pCritSect;
97 RTSEMEVENT Event;
98 RTSEMEVENTMULTI EventMulti;
99 RTSEMMUTEX Mutex;
100 } Block;
101 /** Where we're blocking. */
102 const char volatile *pszBlockFile;
103 /** Where we're blocking. */
104 unsigned volatile uBlockLine;
105 /** Where we're blocking. */
106 RTUINTPTR volatile uBlockId;
107 /** Number of registered write locks, mutexes and critsects that this thread owns. */
108 int32_t volatile cWriteLocks;
109 /** Number of registered read locks that this thread owns, nesting included. */
110 int32_t volatile cReadLocks;
111#endif /* IN_RING3 */
112#ifdef IPRT_WITH_GENERIC_TLS
113 /** The TLS entries for this thread. */
114 void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
115#endif
116 /** Thread name. */
117 char szName[RTTHREAD_NAME_LEN];
118} RTTHREADINT, *PRTTHREADINT;
119
120
121/** @name RTTHREADINT::fIntFlags Masks and Bits.
122 * @{ */
123/** Set if the thread is an alien thread.
124 * Clear if the thread was created by IPRT. */
125#define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
126/** Set if the thread has terminated.
127 * Clear if the thread is running. */
128#define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
129/** This bit is set if the thread is in the AVL tree. */
130#define RTTHREADINT_FLAG_IN_TREE_BIT 2
131/** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
132#define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
133/** Set if it's the main thread. */
134#define RTTHREADINT_FLAGS_MAIN RT_BIT(3)
135/** @} */
136
137
138/**
139 * Initialize the native part of the thread management.
140 *
141 * Generally a TLS entry will be allocated at this point (Ring-3).
142 *
143 * @returns iprt status code.
144 */
145int rtThreadNativeInit(void);
146
147/**
148 * Create a native thread.
149 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
150 *
151 * @returns iprt status code.
152 * @param pThreadInt The thread data structure for the thread.
153 * @param pNativeThread Where to store the native thread identifier.
154 */
155int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
156
157/**
158 * Adopts a thread, this is called immediately after allocating the
159 * thread structure.
160 *
161 * @param pThread Pointer to the thread structure.
162 */
163int rtThreadNativeAdopt(PRTTHREADINT pThread);
164
165/**
166 * Sets the priority of the thread according to the thread type
167 * and current process priority.
168 *
169 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
170 * the caller on a successful return.
171 *
172 * @returns iprt status code.
173 * @param pThread The thread in question.
174 * @param enmType The thread type.
175 * @remark Located in sched.
176 */
177int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
178
179#ifdef IN_RING3
180# ifdef RT_OS_WINDOWS
181/**
182 * Callback for when a native thread is detaching.
183 *
184 * It give the Win32/64 backend a chance to terminate alien
185 * threads properly.
186 */
187void rtThreadNativeDetach(void);
188# endif
189#endif /* !IN_RING0 */
190
191
192/* thread.cpp */
193int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
194void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
195 const char *pszFile, unsigned uLine, RTUINTPTR uId);
196void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
197uint32_t rtThreadRelease(PRTTHREADINT pThread);
198void rtThreadTerminate(PRTTHREADINT pThread, int rc);
199PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
200PRTTHREADINT rtThreadGet(RTTHREAD Thread);
201int rtThreadInit(void);
202void rtThreadTerm(void);
203void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
204#ifdef IN_RING3
205int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
206#endif /* !IN_RING0 */
207#ifdef IPRT_WITH_GENERIC_TLS
208void rtThreadClearTlsEntry(RTTLS iTls);
209void rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
210#endif
211
212RT_C_DECLS_END
213
214#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