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

Last change on this file since 7519 was 7519, checked in by sandervl, 24 years ago

fixed GetCurrentThreadId

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