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

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

replaced heap alloc by stack alloc

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