VirtualBox

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

Last change on this file since 6961 was 6961, checked in by vboxsync, 17 years ago

Generic implementation (in case it's needed).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1/* $Id: thread.h 6961 2008-02-14 17:39:02Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Internal RTThread header.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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/magics.h"
38
39__BEGIN_DECLS
40
41
42
43/**
44 * The thread state.
45 */
46typedef enum RTTHREADSTATE
47{
48 /** The usual invalid 0 value. */
49 RTTHREADSTATE_INVALID = 0,
50 /** The thread is being initialized. */
51 RTTHREADSTATE_INITIALIZING,
52 /** The thread has terminated */
53 RTTHREADSTATE_TERMINATED,
54 /** Probably running. */
55 RTTHREADSTATE_RUNNING,
56 /** Waiting on a critical section. */
57 RTTHREADSTATE_CRITSECT,
58 /** Waiting on a mutex. */
59 RTTHREADSTATE_MUTEX,
60 /** Waiting on a event semaphore. */
61 RTTHREADSTATE_EVENT,
62 /** Waiting on a event multiple wakeup semaphore. */
63 RTTHREADSTATE_EVENTMULTI,
64 /** The thread is sleeping. */
65 RTTHREADSTATE_SLEEP,
66 /** The usual 32-bit size hack. */
67 RTTHREADSTATE_32BIT_HACK = 0x7fffffff
68} RTTHREADSTATE;
69
70
71/** Checks if a thread state indicates that the thread is sleeping. */
72#define RTTHREAD_IS_SLEEPING(enmState) ( (enmState) == RTTHREADSTATE_CRITSECT \
73 || (enmState) == RTTHREADSTATE_MUTEX \
74 || (enmState) == RTTHREADSTATE_EVENT \
75 || (enmState) == RTTHREADSTATE_EVENTMULTI \
76 || (enmState) == RTTHREADSTATE_SLEEP \
77 )
78
79/** Max thread name length. */
80#define RTTHREAD_NAME_LEN 16
81#ifdef IPRT_WITH_GENERIC_TLS
82/** The number of TLS entries for the generic implementation. */
83# define RTTHREAD_TLS_ENTRIES 64
84#endif
85
86/**
87 * Internal represenation of a thread.
88 */
89typedef struct RTTHREADINT
90{
91 /** Avl node core - the key is the native thread id. */
92 AVLPVNODECORE Core;
93 /** Magic value (RTTHREADINT_MAGIC). */
94 uint32_t u32Magic;
95 /** Reference counter. */
96 uint32_t volatile cRefs;
97 /** The current thread state. */
98 RTTHREADSTATE volatile enmState;
99#if defined(RT_OS_WINDOWS) && defined(IN_RING3)
100 /** The thread handle
101 * This is not valid until the create function has returned! */
102 uintptr_t hThread;
103#endif
104 /** The user event semaphore. */
105 RTSEMEVENTMULTI EventUser;
106 /** The terminated event semaphore. */
107 RTSEMEVENTMULTI EventTerminated;
108 /** The thread type. */
109 RTTHREADTYPE enmType;
110 /** The thread creation flags. (RTTHREADFLAGS) */
111 unsigned fFlags;
112 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
113 uint32_t fIntFlags;
114 /** The result code. */
115 int rc;
116 /** Thread function. */
117 PFNRTTHREAD pfnThread;
118 /** Thread function argument. */
119 void *pvUser;
120 /** Actual stack size. */
121 size_t cbStack;
122#ifdef IN_RING3
123 /** What we're blocking on. */
124 union RTTHREADINTBLOCKID
125 {
126 uint64_t u64;
127 PRTCRITSECT pCritSect;
128 RTSEMEVENT Event;
129 RTSEMEVENTMULTI EventMulti;
130 RTSEMMUTEX Mutex;
131 } Block;
132 /** Where we're blocking. */
133 const char volatile *pszBlockFile;
134 /** Where we're blocking. */
135 unsigned volatile uBlockLine;
136 /** Where we're blocking. */
137 RTUINTPTR volatile uBlockId;
138#endif /* IN_RING3 */
139#ifdef IPRT_WITH_GENERIC_TLS
140 /** The TLS entries for this thread. */
141 void *apvTlsEntries[RTTHREAD_TLS_ENTRIES];
142#endif
143 /** Thread name. */
144 char szName[RTTHREAD_NAME_LEN];
145} RTTHREADINT, *PRTTHREADINT;
146
147
148/** @name RTTHREADINT::fIntFlags Masks and Bits.
149 * @{ */
150/** Set if the thread is an alien thread.
151 * Clear if the thread was created by IPRT. */
152#define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
153/** Set if the thread has terminated.
154 * Clear if the thread is running. */
155#define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
156/** This bit is set if the thread is in the AVL tree. */
157#define RTTHREADINT_FLAG_IN_TREE_BIT 2
158/** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
159#define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
160/** @} */
161
162
163/**
164 * Initialize the native part of the thread management.
165 *
166 * Generally a TLS entry will be allocated at this point (Ring-3).
167 *
168 * @returns iprt status code.
169 */
170int rtThreadNativeInit(void);
171
172/**
173 * Create a native thread.
174 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
175 *
176 * @returns iprt status code.
177 * @param pThreadInt The thread data structure for the thread.
178 * @param pNativeThread Where to store the native thread identifier.
179 */
180int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
181
182/**
183 * Adopts a thread, this is called immediately after allocating the
184 * thread structure.
185 *
186 * @param pThread Pointer to the thread structure.
187 */
188int rtThreadNativeAdopt(PRTTHREADINT pThread);
189
190/**
191 * Sets the priority of the thread according to the thread type
192 * and current process priority.
193 *
194 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
195 * the caller on a successful return.
196 *
197 * @returns iprt status code.
198 * @param Thread The thread in question.
199 * @param enmType The thread type.
200 * @remark Located in sched.
201 */
202int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
203
204#ifdef IN_RING3
205# ifdef RT_OS_WINDOWS
206/**
207 * Callback for when a native thread is detaching.
208 *
209 * It give the Win32/64 backend a chance to terminate alien
210 * threads properly.
211 */
212void rtThreadNativeDetach(void);
213# endif
214#endif /* !IN_RING0 */
215
216
217/* thread.cpp */
218int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
219void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
220 const char *pszFile, unsigned uLine, RTUINTPTR uId);
221void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
222uint32_t rtThreadRelease(PRTTHREADINT pThread);
223void rtThreadTerminate(PRTTHREADINT pThread, int rc);
224PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
225PRTTHREADINT rtThreadGet(RTTHREAD Thread);
226int rtThreadInit(void);
227void rtThreadTerm(void);
228void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
229#ifdef IN_RING3
230int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
231#endif /* !IN_RING0 */
232#ifdef IPRT_WITH_GENERIC_TLS
233void rtThreadClearTlsEntry(RTTLS iTls);
234void rtThreadTlsDestruction(PRTTHREADINT pThread); /* in tls-generic.cpp */
235#endif
236
237__END_DECLS
238
239#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