1 | /* $Id: thread.h 403 2007-01-28 08:45:05Z 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.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 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 | #include <iprt/avl.h>
|
---|
28 | #ifdef IN_RING3
|
---|
29 | # include <iprt/process.h>
|
---|
30 | # include <iprt/critsect.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | __BEGIN_DECLS
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * The thread state.
|
---|
39 | */
|
---|
40 | typedef enum RTTHREADSTATE
|
---|
41 | {
|
---|
42 | /** The usual invalid 0 value. */
|
---|
43 | RTTHREADSTATE_INVALID = 0,
|
---|
44 | /** The thread is being initialized. */
|
---|
45 | RTTHREADSTATE_INITIALIZING,
|
---|
46 | /** The thread has terminated */
|
---|
47 | RTTHREADSTATE_TERMINATED,
|
---|
48 | /** Probably running. */
|
---|
49 | RTTHREADSTATE_RUNNING,
|
---|
50 | /** Waiting on a critical section. */
|
---|
51 | RTTHREADSTATE_CRITSECT,
|
---|
52 | /** Waiting on a mutex. */
|
---|
53 | RTTHREADSTATE_MUTEX,
|
---|
54 | /** Waiting on a event semaphore. */
|
---|
55 | RTTHREADSTATE_EVENT,
|
---|
56 | /** Waiting on a event multiple wakeup semaphore. */
|
---|
57 | RTTHREADSTATE_EVENTMULTI,
|
---|
58 | /** The thread is sleeping. */
|
---|
59 | RTTHREADSTATE_SLEEP,
|
---|
60 | /** The usual 32-bit size hack. */
|
---|
61 | RTTHREADSTATE_32BIT_HACK = 0x7fffffff
|
---|
62 | } RTTHREADSTATE;
|
---|
63 |
|
---|
64 |
|
---|
65 | /** Checks if a thread state indicates that the thread is sleeping. */
|
---|
66 | #define RTTHREAD_IS_SLEEPING(enmState) ( (enmState) == RTTHREADSTATE_CRITSECT \
|
---|
67 | || (enmState) == RTTHREADSTATE_MUTEX \
|
---|
68 | || (enmState) == RTTHREADSTATE_EVENT \
|
---|
69 | || (enmState) == RTTHREADSTATE_EVENTMULTI \
|
---|
70 | || (enmState) == RTTHREADSTATE_SLEEP \
|
---|
71 | )
|
---|
72 |
|
---|
73 | /** Max thread name length. */
|
---|
74 | #define RTTHREAD_NAME_LEN 16
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Internal represenation of a thread.
|
---|
78 | */
|
---|
79 | typedef struct RTTHREADINT
|
---|
80 | {
|
---|
81 | /** Avl node core - the key is the native thread id. */
|
---|
82 | AVLPVNODECORE Core;
|
---|
83 | /** Magic value (RTTHREADINT_MAGIC). */
|
---|
84 | uint32_t u32Magic;
|
---|
85 | /** Reference counter. */
|
---|
86 | uint32_t volatile cRefs;
|
---|
87 | /** The current thread state. */
|
---|
88 | RTTHREADSTATE volatile enmState;
|
---|
89 | #if defined(__WIN__) && defined(IN_RING3)
|
---|
90 | /** The thread handle.
|
---|
91 | * This is not valid until the create function has returned! */
|
---|
92 | uintptr_t hThread;
|
---|
93 | #endif
|
---|
94 | /** The user event semaphore. */
|
---|
95 | RTSEMEVENTMULTI EventUser;
|
---|
96 | /** The terminated event semaphore. */
|
---|
97 | RTSEMEVENTMULTI EventTerminated;
|
---|
98 | /** The thread type. */
|
---|
99 | RTTHREADTYPE enmType;
|
---|
100 | /** The thread creation flags. (RTTHREADFLAGS) */
|
---|
101 | unsigned fFlags;
|
---|
102 | /** Internal flags. (RTTHREADINT_FLAGS_ *) */
|
---|
103 | uint32_t fIntFlags;
|
---|
104 | /** The result code. */
|
---|
105 | int rc;
|
---|
106 | /** Thread function. */
|
---|
107 | PFNRTTHREAD pfnThread;
|
---|
108 | /** Thread function argument. */
|
---|
109 | void *pvUser;
|
---|
110 | /** Actual stack size. */
|
---|
111 | size_t cbStack;
|
---|
112 | #ifdef IN_RING3
|
---|
113 | /** What we're blocking on. */
|
---|
114 | union RTTHREADINTBLOCKID
|
---|
115 | {
|
---|
116 | uint64_t u64;
|
---|
117 | PRTCRITSECT pCritSect;
|
---|
118 | RTSEMEVENT Event;
|
---|
119 | RTSEMEVENTMULTI EventMulti;
|
---|
120 | RTSEMMUTEX Mutex;
|
---|
121 | } Block;
|
---|
122 | /** Where we're blocking. */
|
---|
123 | const char volatile *pszBlockFile;
|
---|
124 | /** Where we're blocking. */
|
---|
125 | unsigned volatile uBlockLine;
|
---|
126 | /** Where we're blocking. */
|
---|
127 | RTUINTPTR volatile uBlockId;
|
---|
128 | #endif /* IN_RING3 */
|
---|
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 (Ring-3).
|
---|
157 | *
|
---|
158 | * @returns iprt status code.
|
---|
159 | */
|
---|
160 | int rtThreadNativeInit(void);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Create a native thread.
|
---|
164 | * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
|
---|
165 | *
|
---|
166 | * @returns iprt status code.
|
---|
167 | * @param pThreadInt The thread data structure for the thread.
|
---|
168 | * @param pNativeThread Where to store the native thread identifier.
|
---|
169 | */
|
---|
170 | int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Adopts a thread, this is called immediately after allocating the
|
---|
174 | * thread structure.
|
---|
175 | *
|
---|
176 | * @param pThread Pointer to the thread structure.
|
---|
177 | */
|
---|
178 | int rtThreadNativeAdopt(PRTTHREADINT pThread);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Sets the priority of the thread according to the thread type
|
---|
182 | * and current process priority.
|
---|
183 | *
|
---|
184 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
185 | * the caller on a successful return.
|
---|
186 | *
|
---|
187 | * @returns iprt status code.
|
---|
188 | * @param Thread The thread in question.
|
---|
189 | * @param enmType The thread type.
|
---|
190 | * @remark Located in sched.
|
---|
191 | */
|
---|
192 | int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
|
---|
193 |
|
---|
194 | #ifdef IN_RING3
|
---|
195 | # ifdef __WIN__
|
---|
196 | /**
|
---|
197 | * Callback for when a native thread is detaching.
|
---|
198 | *
|
---|
199 | * It give the Win32/64 backend a chance to terminate alien
|
---|
200 | * threads properly.
|
---|
201 | */
|
---|
202 | void rtThreadNativeDetach(void);
|
---|
203 | # endif
|
---|
204 | #endif /* !IN_RING0 */
|
---|
205 |
|
---|
206 |
|
---|
207 | /* thread.cpp */
|
---|
208 | int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
209 | void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
|
---|
210 | const char *pszFile, unsigned uLine, RTUINTPTR uId);
|
---|
211 | void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
|
---|
212 | uint32_t rtThreadRelease(PRTTHREADINT pThread);
|
---|
213 | void rtThreadTerminate(PRTTHREADINT pThread, int rc);
|
---|
214 | PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
|
---|
215 | PRTTHREADINT rtThreadGet(RTTHREAD Thread);
|
---|
216 | int rtThreadInit(void);
|
---|
217 | void rtThreadTerm(void);
|
---|
218 | void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
219 | #ifdef IN_RING3
|
---|
220 | int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
|
---|
221 | #endif /* !IN_RING0 */
|
---|
222 |
|
---|
223 | __END_DECLS
|
---|
224 |
|
---|
225 | #endif
|
---|