1 | /* $Id: thread.h 5999 2007-12-07 15:05:06Z 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 | */
|
---|
46 | typedef 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 |
|
---|
82 | /**
|
---|
83 | * Internal represenation of a thread.
|
---|
84 | */
|
---|
85 | typedef struct RTTHREADINT
|
---|
86 | {
|
---|
87 | /** Avl node core - the key is the native thread id. */
|
---|
88 | AVLPVNODECORE Core;
|
---|
89 | /** Magic value (RTTHREADINT_MAGIC). */
|
---|
90 | uint32_t u32Magic;
|
---|
91 | /** Reference counter. */
|
---|
92 | uint32_t volatile cRefs;
|
---|
93 | /** The current thread state. */
|
---|
94 | RTTHREADSTATE volatile enmState;
|
---|
95 | #if defined(RT_OS_WINDOWS) && defined(IN_RING3)
|
---|
96 | /** The thread handle
|
---|
97 | * This is not valid until the create function has returned! */
|
---|
98 | uintptr_t hThread;
|
---|
99 | #endif
|
---|
100 | /** The user event semaphore. */
|
---|
101 | RTSEMEVENTMULTI EventUser;
|
---|
102 | /** The terminated event semaphore. */
|
---|
103 | RTSEMEVENTMULTI EventTerminated;
|
---|
104 | /** The thread type. */
|
---|
105 | RTTHREADTYPE enmType;
|
---|
106 | /** The thread creation flags. (RTTHREADFLAGS) */
|
---|
107 | unsigned fFlags;
|
---|
108 | /** Internal flags. (RTTHREADINT_FLAGS_ *) */
|
---|
109 | uint32_t fIntFlags;
|
---|
110 | /** The result code. */
|
---|
111 | int rc;
|
---|
112 | /** Thread function. */
|
---|
113 | PFNRTTHREAD pfnThread;
|
---|
114 | /** Thread function argument. */
|
---|
115 | void *pvUser;
|
---|
116 | /** Actual stack size. */
|
---|
117 | size_t cbStack;
|
---|
118 | #ifdef IN_RING3
|
---|
119 | /** What we're blocking on. */
|
---|
120 | union RTTHREADINTBLOCKID
|
---|
121 | {
|
---|
122 | uint64_t u64;
|
---|
123 | PRTCRITSECT pCritSect;
|
---|
124 | RTSEMEVENT Event;
|
---|
125 | RTSEMEVENTMULTI EventMulti;
|
---|
126 | RTSEMMUTEX Mutex;
|
---|
127 | } Block;
|
---|
128 | /** Where we're blocking. */
|
---|
129 | const char volatile *pszBlockFile;
|
---|
130 | /** Where we're blocking. */
|
---|
131 | unsigned volatile uBlockLine;
|
---|
132 | /** Where we're blocking. */
|
---|
133 | RTUINTPTR volatile uBlockId;
|
---|
134 | #endif /* IN_RING3 */
|
---|
135 | /** Thread name. */
|
---|
136 | char szName[RTTHREAD_NAME_LEN];
|
---|
137 | } RTTHREADINT, *PRTTHREADINT;
|
---|
138 |
|
---|
139 |
|
---|
140 | /** @name RTTHREADINT::fIntFlags Masks and Bits.
|
---|
141 | * @{ */
|
---|
142 | /** Set if the thread is an alien thread.
|
---|
143 | * Clear if the thread was created by IPRT. */
|
---|
144 | #define RTTHREADINT_FLAGS_ALIEN RT_BIT(0)
|
---|
145 | /** Set if the thread has terminated.
|
---|
146 | * Clear if the thread is running. */
|
---|
147 | #define RTTHREADINT_FLAGS_TERMINATED RT_BIT(1)
|
---|
148 | /** This bit is set if the thread is in the AVL tree. */
|
---|
149 | #define RTTHREADINT_FLAG_IN_TREE_BIT 2
|
---|
150 | /** @copydoc RTTHREADINT_FLAG_IN_TREE_BIT */
|
---|
151 | #define RTTHREADINT_FLAG_IN_TREE RT_BIT(RTTHREADINT_FLAG_IN_TREE_BIT)
|
---|
152 | /** @} */
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Initialize the native part of the thread management.
|
---|
157 | *
|
---|
158 | * Generally a TLS entry will be allocated at this point (Ring-3).
|
---|
159 | *
|
---|
160 | * @returns iprt status code.
|
---|
161 | */
|
---|
162 | int rtThreadNativeInit(void);
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Create a native thread.
|
---|
166 | * This creates the thread as described in pThreadInt and stores the thread id in *pThread.
|
---|
167 | *
|
---|
168 | * @returns iprt status code.
|
---|
169 | * @param pThreadInt The thread data structure for the thread.
|
---|
170 | * @param pNativeThread Where to store the native thread identifier.
|
---|
171 | */
|
---|
172 | int rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread);
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Adopts a thread, this is called immediately after allocating the
|
---|
176 | * thread structure.
|
---|
177 | *
|
---|
178 | * @param pThread Pointer to the thread structure.
|
---|
179 | */
|
---|
180 | int rtThreadNativeAdopt(PRTTHREADINT pThread);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Sets the priority of the thread according to the thread type
|
---|
184 | * and current process priority.
|
---|
185 | *
|
---|
186 | * The RTTHREADINT::enmType member has not yet been updated and will be updated by
|
---|
187 | * the caller on a successful return.
|
---|
188 | *
|
---|
189 | * @returns iprt status code.
|
---|
190 | * @param Thread The thread in question.
|
---|
191 | * @param enmType The thread type.
|
---|
192 | * @remark Located in sched.
|
---|
193 | */
|
---|
194 | int rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType);
|
---|
195 |
|
---|
196 | #ifdef IN_RING3
|
---|
197 | # ifdef RT_OS_WINDOWS
|
---|
198 | /**
|
---|
199 | * Callback for when a native thread is detaching.
|
---|
200 | *
|
---|
201 | * It give the Win32/64 backend a chance to terminate alien
|
---|
202 | * threads properly.
|
---|
203 | */
|
---|
204 | void rtThreadNativeDetach(void);
|
---|
205 | # endif
|
---|
206 | #endif /* !IN_RING0 */
|
---|
207 |
|
---|
208 |
|
---|
209 | /* thread.cpp */
|
---|
210 | int rtThreadMain(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread, const char *pszThreadName);
|
---|
211 | void rtThreadBlocking(PRTTHREADINT pThread, RTTHREADSTATE enmState, uint64_t u64Block,
|
---|
212 | const char *pszFile, unsigned uLine, RTUINTPTR uId);
|
---|
213 | void rtThreadUnblocked(PRTTHREADINT pThread, RTTHREADSTATE enmCurState);
|
---|
214 | uint32_t rtThreadRelease(PRTTHREADINT pThread);
|
---|
215 | void rtThreadTerminate(PRTTHREADINT pThread, int rc);
|
---|
216 | PRTTHREADINT rtThreadGetByNative(RTNATIVETHREAD NativeThread);
|
---|
217 | PRTTHREADINT rtThreadGet(RTTHREAD Thread);
|
---|
218 | int rtThreadInit(void);
|
---|
219 | void rtThreadTerm(void);
|
---|
220 | void rtThreadInsert(PRTTHREADINT pThread, RTNATIVETHREAD NativeThread);
|
---|
221 | #ifdef IN_RING3
|
---|
222 | int rtThreadDoSetProcPriority(RTPROCPRIORITY enmPriority);
|
---|
223 | #endif /* !IN_RING0 */
|
---|
224 |
|
---|
225 | __END_DECLS
|
---|
226 |
|
---|
227 | #endif
|
---|