VirtualBox

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

最後變更 在這個檔案從197是 197,由 vboxsync 提交於 18 年 前

A stab at generic timers (untested).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 8.0 KB
 
1/* $Id: thread.h 197 2007-01-20 01:22:45Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Internal RTThread header.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef __thread_h__
23#define __thread_h__
24
25#include <iprt/types.h>
26#include <iprt/thread.h>
27#ifdef IN_RING3
28# include <iprt/process.h>
29# include <iprt/critsect.h>
30# include <iprt/avl.h>
31#endif
32
33__BEGIN_DECLS
34
35
36#ifdef IN_RING3
37
38
39/**
40 * The thread state.
41 */
42typedef enum RTTHREADSTATE
43{
44 /** The usual invalid 0 value. */
45 RTTHREADSTATE_INVALID = 0,
46 /** The thread is being initialized. */
47 RTTHREADSTATE_INITIALIZING,
48 /** The thread has terminated */
49 RTTHREADSTATE_TERMINATED,
50 /** Probably running. */
51 RTTHREADSTATE_RUNNING,
52 /** Waiting on a critical section. */
53 RTTHREADSTATE_CRITSECT,
54 /** Waiting on a mutex. */
55 RTTHREADSTATE_MUTEX,
56 /** Waiting on a event semaphore. */
57 RTTHREADSTATE_EVENT,
58 /** Waiting on a event multiple wakeup semaphore. */
59 RTTHREADSTATE_EVENTMULTI,
60 /** The thread is sleeping. */
61 RTTHREADSTATE_SLEEP,
62 /** The usual 32-bit size hack. */
63 RTTHREADSTATE_32BIT_HACK = 0x7fffffff
64} RTTHREADSTATE;
65
66
67/** Checks if a thread state indicates that the thread is sleeping. */
68#define RTTHREAD_IS_SLEEPING(enmState) ( (enmState) == RTTHREADSTATE_CRITSECT \
69 || (enmState) == RTTHREADSTATE_MUTEX \
70 || (enmState) == RTTHREADSTATE_EVENT \
71 || (enmState) == RTTHREADSTATE_EVENTMULTI \
72 || (enmState) == RTTHREADSTATE_SLEEP \
73 )
74
75/** Max thread name length. */
76#define RTTHREAD_NAME_LEN 16
77
78/**
79 * Internal represenation of a thread.
80 */
81typedef struct RTTHREADINT
82{
83 /** Avl node core - the key is the native thread id. */
84 AVLPVNODECORE Core;
85 /** Magic value (RTTHREADINT_MAGIC). */
86 uint32_t u32Magic;
87 /** Reference counter. */
88 uint32_t volatile cRefs;
89 /** The current thread state. */
90 RTTHREADSTATE volatile enmState;
91#ifdef __WIN__
92 /** The thread handle.
93 * This is not valid until the create function has returned! */
94 uintptr_t hThread;
95#endif
96 /** The user event semaphore. */
97 RTSEMEVENTMULTI EventUser;
98 /** The terminated event semaphore. */
99 RTSEMEVENTMULTI EventTerminated;
100 /** The thread type. */
101 RTTHREADTYPE enmType;
102 /** The thread creation flags. (RTTHREADFLAGS) */
103 unsigned fFlags;
104 /** Internal flags. (RTTHREADINT_FLAGS_ *) */
105 uint32_t fIntFlags;
106 /** The result code. */
107 int rc;
108 /** Thread function. */
109 PFNRTTHREAD pfnThread;
110 /** Thread function argument. */
111 void *pvUser;
112 /** Actual stack size. */
113 size_t cbStack;
114 /** What we're blocking on. */
115 union RTTHREADINTBLOCKID
116 {
117 uint64_t u64;
118 PRTCRITSECT pCritSect;
119 RTSEMEVENT Event;
120 RTSEMEVENTMULTI EventMulti;
121 RTSEMMUTEX Mutex;
122 } Block;
123 /** Where we're blocking. */
124 const char volatile *pszBlockFile;
125 /** Where we're blocking. */
126 unsigned volatile uBlockLine;
127 /** Where we're blocking. */
128 RTUINTPTR volatile uBlockId;
129 /** Thread name. */
130 char szName[RTTHREAD_NAME_LEN];
131} RTTHREADINT, *PRTTHREADINT;
132
133/** RTTHREADINT::u32Magic value. (Gilbert Keith Chesterton) */
134#define RTTHREADINT_MAGIC 0x18740529
135/** RTTHREADINT::u32Magic value for a dead thread. */
136#define RTTHREADINT_MAGIC_DEAD 0x19360614
137
138/** @name RTTHREADINT::fIntFlags Masks and Bits.
139 * @{ */
140/** Set if the thread is an alien thread.
141 * Clear if the thread was created by IPRT. */
142#define RTTHREADINT_FLAGS_ALIEN BIT(0)
143/** Set if the thread has terminated.
144 * Clear if the thread is running. */
145#define RTTHREADINT_FLAGS_TERMINATED BIT(1)
146/** This bit is set if the thread is in the AVL tree. */
147#define RTTHREADINT_FLAG_IN_TREE_BIT 2
148/** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
149#define RTTHREADINT_FLAG_IN_TREE BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
150/** @} */
151
152
153/**
154 * Initialize the native part of the thread management.
155 *
156 * Generally a TLS entry will be allocated at this point.
157 *
158 * @returns iprt status code.
159 */
160int rtThreadNativeInit(void);
161
162/**
163 * Sets the priority of the thread according to the thread type
164 * and current process priority.
165 *
166 * The RTTHREADINT::enmType member has not yet been updated and will be updated by
167 * the caller on a successful return.
168 *
169 * @returns iprt status code.
170 * @param Thread The thread in question.
171 * @param enmType The thread type.
172 * @remark Located in sched.
173 */
174int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
175
176/**
177 * Create a native thread.
178 * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
179 *
180 * @returns iprt status code.
181 * @param pThreadInt The thread data structure for the thread.
182 * @param pNativeThread Where to store the native thread identifier.
183 */
184int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
185
186/**
187 * Adopts a thread, this is called immediately after allocating the
188 * thread structure.
189 *
190 * @param pThread Pointer to the thread structure.
191 */
192int rtThreadNativeAdopt(PRTTHREADINT pThread);
193
194#ifdef __WIN__
195/**
196 * Callback for when a native thread is detaching.
197 *
198 * It give the Win32/64 backend a chance to terminate alien
199 * threads properly.
200 */
201void rtThreadNativeDetach(void);
202#endif
203
204int rtThreadInit(void);
205int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
206void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
207PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
208PRTTHREADINT rtThreadGet(RTTHREAD Thread);
209uint32_t rtThreadRelease(PRTTHREADINT pThread);
210int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
211void rtThreadTerminate(PRTTHREADINT pThread, int rc);
212void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
213 const char *pszFile, unsigned uLine, RTUINTPTR uId);
214void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
215
216
217#elif defined(IN_RING0)
218
219/**
220 * Argument package for a ring-0 thread.
221 */
222typedef struct RTR0THREADARGS
223{
224 /** The thread function. */
225 PFNRTTHREAD pfnThread;
226 /** The thread function argument. */
227 void *pvUser;
228 /** The thread type. */
229 RTTHREADTYPE enmType;
230} RTR0THREADARGS, *PRTR0THREADARGS;
231
232
233
234int rtThreadMain(PRTR0THREADARGS pArgs, RTNATIVETHREAD NativeThread);
235
236/**
237 * Do the actual thread creation.
238 *
239 * @returns IPRT status code.
240 * On failure, no thread has been created.
241 * @param pArgs The argument package.
242 * @param pNativeThread Where to return the native thread handle.
243 */
244int rtThreadNativeCreate(PRTR0THREADARGS pArgs, PRTNATIVETHREAD pNativeThread);
245
246/**
247 * Do the actual thread priority change.
248 *
249 * @returns IPRT status code.
250 * @param Thread The thread which priority should be changed.
251 * This is currently restricted to the current thread.
252 * @param enmType The new thread priority type (valid).
253 */
254int rtThreadNativeSetPriority(RTTHREAD Thread, RTTHREADTYPE enmType);
255
256#endif /* !IN_RING0 */
257
258__END_DECLS
259
260#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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