1 | /* $Id: thread.cpp,v 1.34 2001-11-12 23:04:56 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 |
|
---|
39 | ODINDEBUGCHANNEL(KERNEL32-THREAD)
|
---|
40 |
|
---|
41 | //******************************************************************************
|
---|
42 | //******************************************************************************
|
---|
43 | DWORD WIN32API GetCurrentThreadId()
|
---|
44 | {
|
---|
45 | //// dprintf(("GetCurrentThreadId\n"));
|
---|
46 | return(O32_GetCurrentThreadId());
|
---|
47 | }
|
---|
48 | //******************************************************************************
|
---|
49 | //******************************************************************************
|
---|
50 | HANDLE 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
|
---|
64 | ULONG 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 |
|
---|
80 | void 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
|
---|
93 | void 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 |
|
---|
119 | void 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 |
|
---|
131 | void 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 |
|
---|
153 | char* WIN32API dbg_GetLastCallerName()
|
---|
154 | {
|
---|
155 | // retrieve last caller name from stack
|
---|
156 | TEB *teb;
|
---|
157 |
|
---|
158 | // embedded dbg_DecThreadCallDepth
|
---|
159 | teb = GetThreadTEB();
|
---|
160 | if(teb != NULL)
|
---|
161 | {
|
---|
162 | int iIndex = teb->o.odin.dbgCallDepth - 1;
|
---|
163 | if ( (iIndex > 0) &&
|
---|
164 | (iIndex < MAX_CALLSTACK_SIZE) )
|
---|
165 | {
|
---|
166 | return (char*)teb->o.odin.arrstrCallStack[iIndex];
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return NULL;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | //******************************************************************************
|
---|
175 | //******************************************************************************
|
---|
176 | VOID WIN32API ExitThread(DWORD exitcode)
|
---|
177 | {
|
---|
178 | EXCEPTION_FRAME *exceptFrame;
|
---|
179 | TEB *teb;
|
---|
180 |
|
---|
181 | dprintf(("ExitThread %x (%x)", GetCurrentThread(), exitcode));
|
---|
182 |
|
---|
183 | teb = GetThreadTEB();
|
---|
184 | if(teb != 0) {
|
---|
185 | exceptFrame = (EXCEPTION_FRAME *)teb->o.odin.exceptFrame;
|
---|
186 | }
|
---|
187 | else DebugInt3();
|
---|
188 |
|
---|
189 | HMSetThreadTerminated(GetCurrentThread());
|
---|
190 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
191 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
192 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
193 | DestroyTIB();
|
---|
194 |
|
---|
195 | if(exceptFrame) OS2UnsetExceptionHandler((void *)exceptFrame);
|
---|
196 |
|
---|
197 | O32_ExitThread(exitcode);
|
---|
198 | }
|
---|
199 | /*****************************************************************************
|
---|
200 | * Name : DWORD SetThreadAffinityMask
|
---|
201 | * Purpose : The SetThreadAffinityMask function sets a processor affinity
|
---|
202 | * mask for a specified thread.
|
---|
203 | * A thread affinity mask is a bit vector in which each bit
|
---|
204 | * represents the processors that a thread is allowed to run on.
|
---|
205 | * A thread affinity mask must be a proper subset of the process
|
---|
206 | * affinity mask for the containing process of a thread. A thread
|
---|
207 | * is only allowed to run on the processors its process is allowed to run on.
|
---|
208 | * Parameters: HANDLE hThread handle to the thread of interest
|
---|
209 | * DWORD dwThreadAffinityMask a thread affinity mask
|
---|
210 | * Variables :
|
---|
211 | * Result : TRUE / FALSE
|
---|
212 | * Remark :
|
---|
213 | * Status : UNTESTED STUB
|
---|
214 | *
|
---|
215 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
---|
216 | *****************************************************************************/
|
---|
217 |
|
---|
218 | DWORD WIN32API SetThreadAffinityMask(HANDLE hThread,
|
---|
219 | DWORD dwThreadAffinityMask)
|
---|
220 | {
|
---|
221 | dprintf(("KERNEL32: SetThreadAffinityMask(%08xh,%08xh)",
|
---|
222 | hThread, dwThreadAffinityMask));
|
---|
223 |
|
---|
224 | if(hThread != GetCurrentThread()) {
|
---|
225 | dprintf(("WARNING: Setting the affinity mask for another thread than the current one is not supported!!"));
|
---|
226 | return FALSE;
|
---|
227 | }
|
---|
228 | return OSLibDosSetThreadAffinity(dwThreadAffinityMask);
|
---|
229 | }
|
---|
230 | //******************************************************************************
|
---|
231 | //******************************************************************************
|
---|
232 | Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags, HANDLE hThread)
|
---|
233 | {
|
---|
234 | lpUserData = lpData;
|
---|
235 | pCallback = pUserCallback;
|
---|
236 | this->dwFlags = dwFlags;
|
---|
237 | this->hThread = hThread;
|
---|
238 | }
|
---|
239 | //******************************************************************************
|
---|
240 | //******************************************************************************
|
---|
241 | DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
---|
242 | {
|
---|
243 | EXCEPTION_FRAME exceptFrame;
|
---|
244 | Win32Thread *me = (Win32Thread *)lpData;
|
---|
245 | ULONG winthread = (ULONG)me->pCallback;
|
---|
246 | LPVOID userdata = me->lpUserData;
|
---|
247 | HANDLE hThread = me->hThread;
|
---|
248 | DWORD rc;
|
---|
249 |
|
---|
250 | delete(me); //only called once
|
---|
251 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
---|
252 |
|
---|
253 | TEB *winteb = (TEB *)InitializeTIB();
|
---|
254 | if(winteb == NULL) {
|
---|
255 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
---|
256 | DebugInt3();
|
---|
257 | return 0;
|
---|
258 | }
|
---|
259 | winteb->flags = me->dwFlags;
|
---|
260 |
|
---|
261 | winteb->entry_point = (void *)winthread;
|
---|
262 | winteb->entry_arg = (void *)userdata;
|
---|
263 | winteb->o.odin.hThread = hThread;
|
---|
264 |
|
---|
265 | winteb->o.odin.hab = OSLibWinInitialize();
|
---|
266 | winteb->o.odin.hmq = OSLibWinQueryMsgQueue(winteb->o.odin.hab);
|
---|
267 | dprintf(("Win32ThreadProc: hab %x hmq %x", winteb->o.odin.hab, winteb->o.odin.hmq));
|
---|
268 |
|
---|
269 | //Note: The Win32 exception structure referenced by FS:[0] is the same
|
---|
270 | // in OS/2
|
---|
271 | OS2SetExceptionHandler((void *)&exceptFrame);
|
---|
272 | winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
|
---|
273 |
|
---|
274 | SetWin32TIB();
|
---|
275 |
|
---|
276 | DWORD dwProcessAffinityMask, dwSystemAffinityMask;
|
---|
277 |
|
---|
278 | //Change the affinity mask of this thread to the mask for the whole process
|
---|
279 | if(GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask) == TRUE) {
|
---|
280 | SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
|
---|
281 | }
|
---|
282 |
|
---|
283 | WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
---|
284 | Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
---|
285 | Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
---|
286 |
|
---|
287 | //Set FPU control word to 0x27F (same as in NT)
|
---|
288 | CONTROL87(0x27F, 0xFFF);
|
---|
289 | rc = AsmCallThreadHandler(winthread, userdata);
|
---|
290 |
|
---|
291 | HMSetThreadTerminated(GetCurrentThread());
|
---|
292 | winteb->o.odin.exceptFrame = 0;
|
---|
293 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
294 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
295 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
296 | DestroyTIB();
|
---|
297 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
---|
298 |
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 | //******************************************************************************
|
---|
302 | //******************************************************************************
|
---|