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

Last change on this file since 8648 was 8648, checked in by sandervl, 23 years ago

thread linking + create TEB before thread creation

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