source: trunk/src/kernel32/thread.cpp@ 7509

Last change on this file since 7509 was 7509, checked in by phaller, 24 years ago

Performance shortcut for GetCurrentThreadId()

File size: 9.0 KB
Line 
1/* $Id: thread.cpp,v 1.36 2001-11-30 19:05:47 phaller Exp $ */
2
3/*
4 * Win32 Thread API functions
5 *
6 * TODO: Initialize threadInfo structure during thread creation
7 *
8 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
9 *
10 * Project Odin Software License can be found in LICENSE.TXT
11 *
12 */
13
14/*****************************************************************************
15 * Includes *
16 *****************************************************************************/
17
18#include <odin.h>
19#include <odinwrap.h>
20#include <os2sel.h>
21
22#include <os2win.h>
23#include <stdarg.h>
24#include <string.h>
25#include "thread.h"
26#include <misc.h>
27#include <cpuhlp.h>
28#include <wprocess.h>
29#include <windllbase.h>
30#include <winexebase.h>
31#include "exceptutil.h"
32#include "oslibmisc.h"
33#include "oslibdos.h"
34#include <handlemanager.h>
35
36#define DBG_LOCALLOG DBG_thread
37#include "dbglocal.h"
38
39ODINDEBUGCHANNEL(KERNEL32-THREAD)
40
41
42// The function GetThreadTEB() is defined in wprocess.cpp
43// This macro is for performance improvement only.
44// DWORD TIBFlatPtr is exported from wprocess.cpp
45#define GetThreadTEB() ((TEB*)(TIBFlatPtr))
46
47//******************************************************************************
48//******************************************************************************
49DWORD WIN32API GetCurrentThreadId()
50{
51 // check cached identifier
52 TEB *teb = GetThreadTEB();
53 if(teb != NULL)
54 {
55 // this is set in InitializeTIB() already.
56 return teb->o.odin.threadId;
57 }
58
59//// dprintf(("GetCurrentThreadId\n"));
60 return(O32_GetCurrentThreadId());
61}
62//******************************************************************************
63//******************************************************************************
64HANDLE WIN32API GetCurrentThread()
65{
66 TEB *teb;
67
68 teb = GetThreadTEB();
69 if(teb == 0) {
70 SetLastError(ERROR_INVALID_HANDLE); //todo
71 return 0;
72 }
73 return teb->o.odin.hThread;
74}
75
76// these two debugging functions allow access to a
77// calldepth counter inside the TEB block of each thread
78ULONG WIN32API dbg_GetThreadCallDepth()
79{
80#ifdef DEBUG
81 TEB *teb;
82
83 teb = GetThreadTEB();
84 if(teb == NULL)
85 return 0;
86 else
87 return teb->o.odin.dbgCallDepth;
88#else
89 return 0;
90#endif
91}
92
93
94void WIN32API dbg_IncThreadCallDepth()
95{
96#ifdef DEBUG
97 TEB *teb;
98
99 teb = GetThreadTEB();
100 if(teb != NULL)
101 teb->o.odin.dbgCallDepth++;
102#endif
103}
104
105
106#define MAX_CALLSTACK_SIZE 128
107void WIN32API dbg_ThreadPushCall(char *pszCaller)
108{
109#ifdef DEBUG
110 TEB *teb;
111
112 // embedded dbg_IncThreadCallDepth
113 teb = GetThreadTEB();
114 if(teb == NULL)
115 return;
116
117 teb->o.odin.dbgCallDepth++;
118
119 // add caller name to call stack trace
120 int iIndex = teb->o.odin.dbgCallDepth;
121
122 // allocate callstack on demand
123 if (teb->o.odin.arrstrCallStack == NULL)
124 teb->o.odin.arrstrCallStack = (PVOID*)malloc( sizeof(LPSTR) * MAX_CALLSTACK_SIZE);
125
126 // insert entry
127 if (iIndex < MAX_CALLSTACK_SIZE)
128 teb->o.odin.arrstrCallStack[iIndex] = (PVOID)pszCaller;
129#endif
130}
131
132
133void WIN32API dbg_DecThreadCallDepth()
134{
135#ifdef DEBUG
136 TEB *teb;
137
138 teb = GetThreadTEB();
139 if(teb != NULL)
140 --(teb->o.odin.dbgCallDepth);
141#endif
142}
143
144
145void WIN32API dbg_ThreadPopCall()
146{
147#ifdef DEBUG
148 TEB *teb;
149
150 // embedded dbg_DecThreadCallDepth
151 teb = GetThreadTEB();
152 if(teb == NULL)
153 return;
154
155 --(teb->o.odin.dbgCallDepth);
156
157 // add caller name to call stack trace
158 int iIndex = teb->o.odin.dbgCallDepth;
159
160 // insert entry
161 if (teb->o.odin.arrstrCallStack)
162 if (iIndex < MAX_CALLSTACK_SIZE)
163 teb->o.odin.arrstrCallStack[iIndex] = NULL;
164#endif
165}
166
167char* WIN32API dbg_GetLastCallerName()
168{
169#ifdef DEBUG
170 // retrieve last caller name from stack
171 TEB *teb;
172
173 // embedded dbg_DecThreadCallDepth
174 teb = GetThreadTEB();
175 if(teb != NULL)
176 {
177 int iIndex = teb->o.odin.dbgCallDepth - 1;
178 if ( (iIndex > 0) &&
179 (iIndex < MAX_CALLSTACK_SIZE) )
180 {
181 return (char*)teb->o.odin.arrstrCallStack[iIndex];
182 }
183 }
184#endif
185
186 return NULL;
187}
188
189
190//******************************************************************************
191//******************************************************************************
192VOID WIN32API ExitThread(DWORD exitcode)
193{
194 EXCEPTION_FRAME *exceptFrame;
195 TEB *teb;
196
197 dprintf(("ExitThread %x (%x)", GetCurrentThread(), exitcode));
198
199 teb = GetThreadTEB();
200 if(teb != 0) {
201 exceptFrame = (EXCEPTION_FRAME *)teb->o.odin.exceptFrame;
202 }
203 else DebugInt3();
204
205 HMSetThreadTerminated(GetCurrentThread());
206 Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
207 Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
208 WinExe->tlsDetachThread(); //destroy TLS structure of main exe
209 DestroyTIB();
210
211 if(exceptFrame) OS2UnsetExceptionHandler((void *)exceptFrame);
212
213 O32_ExitThread(exitcode);
214}
215/*****************************************************************************
216 * Name : DWORD SetThreadAffinityMask
217 * Purpose : The SetThreadAffinityMask function sets a processor affinity
218 * mask for a specified thread.
219 * A thread affinity mask is a bit vector in which each bit
220 * represents the processors that a thread is allowed to run on.
221 * A thread affinity mask must be a proper subset of the process
222 * affinity mask for the containing process of a thread. A thread
223 * is only allowed to run on the processors its process is allowed to run on.
224 * Parameters: HANDLE hThread handle to the thread of interest
225 * DWORD dwThreadAffinityMask a thread affinity mask
226 * Variables :
227 * Result : TRUE / FALSE
228 * Remark :
229 * Status : UNTESTED STUB
230 *
231 * Author : Patrick Haller [Mon, 1998/06/15 08:00]
232 *****************************************************************************/
233
234DWORD WIN32API SetThreadAffinityMask(HANDLE hThread,
235 DWORD dwThreadAffinityMask)
236{
237 dprintf(("KERNEL32: SetThreadAffinityMask(%08xh,%08xh)",
238 hThread, dwThreadAffinityMask));
239
240 if(hThread != GetCurrentThread()) {
241 dprintf(("WARNING: Setting the affinity mask for another thread than the current one is not supported!!"));
242 return FALSE;
243 }
244 return OSLibDosSetThreadAffinity(dwThreadAffinityMask);
245}
246//******************************************************************************
247//******************************************************************************
248Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags, HANDLE hThread)
249{
250 lpUserData = lpData;
251 pCallback = pUserCallback;
252 this->dwFlags = dwFlags;
253 this->hThread = hThread;
254}
255//******************************************************************************
256//******************************************************************************
257DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
258{
259 EXCEPTION_FRAME exceptFrame;
260 Win32Thread *me = (Win32Thread *)lpData;
261 ULONG winthread = (ULONG)me->pCallback;
262 LPVOID userdata = me->lpUserData;
263 HANDLE hThread = me->hThread;
264 DWORD rc;
265
266 delete(me); //only called once
267 dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
268
269 TEB *winteb = (TEB *)InitializeTIB();
270 if(winteb == NULL) {
271 dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
272 DebugInt3();
273 return 0;
274 }
275 winteb->flags = me->dwFlags;
276
277 winteb->entry_point = (void *)winthread;
278 winteb->entry_arg = (void *)userdata;
279 winteb->o.odin.hThread = hThread;
280
281 winteb->o.odin.hab = OSLibWinInitialize();
282 winteb->o.odin.hmq = OSLibWinQueryMsgQueue(winteb->o.odin.hab);
283 dprintf(("Win32ThreadProc: hab %x hmq %x", winteb->o.odin.hab, winteb->o.odin.hmq));
284
285 //Note: The Win32 exception structure referenced by FS:[0] is the same
286 // in OS/2
287 OS2SetExceptionHandler((void *)&exceptFrame);
288 winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
289
290 SetWin32TIB();
291
292 DWORD dwProcessAffinityMask, dwSystemAffinityMask;
293
294 //Change the affinity mask of this thread to the mask for the whole process
295 if(GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask) == TRUE) {
296 SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
297 }
298
299 WinExe->tlsAttachThread(); //setup TLS structure of main exe
300 Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
301 Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
302
303 //Set FPU control word to 0x27F (same as in NT)
304 CONTROL87(0x27F, 0xFFF);
305 rc = AsmCallThreadHandler(winthread, userdata);
306
307 HMSetThreadTerminated(GetCurrentThread());
308 winteb->o.odin.exceptFrame = 0;
309 Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
310 Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
311 WinExe->tlsDetachThread(); //destroy TLS structure of main exe
312 DestroyTIB();
313 OS2UnsetExceptionHandler((void *)&exceptFrame);
314
315 return rc;
316}
317//******************************************************************************
318//******************************************************************************
Note: See TracBrowser for help on using the repository browser.