1 | /* $Id: thread.cpp,v 1.33 2001-11-10 12:47:48 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 |
|
---|
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 | void WIN32API dbg_DecThreadCallDepth()
|
---|
93 | {
|
---|
94 | #ifdef DEBUG
|
---|
95 | TEB *teb;
|
---|
96 |
|
---|
97 | teb = GetThreadTEB();
|
---|
98 | if(teb != NULL)
|
---|
99 | --(teb->o.odin.dbgCallDepth);
|
---|
100 | #endif
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | //******************************************************************************
|
---|
105 | //******************************************************************************
|
---|
106 | VOID WIN32API ExitThread(DWORD exitcode)
|
---|
107 | {
|
---|
108 | EXCEPTION_FRAME *exceptFrame;
|
---|
109 | TEB *teb;
|
---|
110 |
|
---|
111 | dprintf(("ExitThread %x (%x)", GetCurrentThread(), exitcode));
|
---|
112 |
|
---|
113 | teb = GetThreadTEB();
|
---|
114 | if(teb != 0) {
|
---|
115 | exceptFrame = (EXCEPTION_FRAME *)teb->o.odin.exceptFrame;
|
---|
116 | }
|
---|
117 | else DebugInt3();
|
---|
118 |
|
---|
119 | HMSetThreadTerminated(GetCurrentThread());
|
---|
120 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
121 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
122 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
123 | DestroyTIB();
|
---|
124 |
|
---|
125 | if(exceptFrame) OS2UnsetExceptionHandler((void *)exceptFrame);
|
---|
126 |
|
---|
127 | O32_ExitThread(exitcode);
|
---|
128 | }
|
---|
129 | /*****************************************************************************
|
---|
130 | * Name : DWORD SetThreadAffinityMask
|
---|
131 | * Purpose : The SetThreadAffinityMask function sets a processor affinity
|
---|
132 | * mask for a specified thread.
|
---|
133 | * A thread affinity mask is a bit vector in which each bit
|
---|
134 | * represents the processors that a thread is allowed to run on.
|
---|
135 | * A thread affinity mask must be a proper subset of the process
|
---|
136 | * affinity mask for the containing process of a thread. A thread
|
---|
137 | * is only allowed to run on the processors its process is allowed to run on.
|
---|
138 | * Parameters: HANDLE hThread handle to the thread of interest
|
---|
139 | * DWORD dwThreadAffinityMask a thread affinity mask
|
---|
140 | * Variables :
|
---|
141 | * Result : TRUE / FALSE
|
---|
142 | * Remark :
|
---|
143 | * Status : UNTESTED STUB
|
---|
144 | *
|
---|
145 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
---|
146 | *****************************************************************************/
|
---|
147 |
|
---|
148 | DWORD WIN32API SetThreadAffinityMask(HANDLE hThread,
|
---|
149 | DWORD dwThreadAffinityMask)
|
---|
150 | {
|
---|
151 | dprintf(("KERNEL32: SetThreadAffinityMask(%08xh,%08xh)",
|
---|
152 | hThread, dwThreadAffinityMask));
|
---|
153 |
|
---|
154 | if(hThread != GetCurrentThread()) {
|
---|
155 | dprintf(("WARNING: Setting the affinity mask for another thread than the current one is not supported!!"));
|
---|
156 | return FALSE;
|
---|
157 | }
|
---|
158 | return OSLibDosSetThreadAffinity(dwThreadAffinityMask);
|
---|
159 | }
|
---|
160 | //******************************************************************************
|
---|
161 | //******************************************************************************
|
---|
162 | Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags, HANDLE hThread)
|
---|
163 | {
|
---|
164 | lpUserData = lpData;
|
---|
165 | pCallback = pUserCallback;
|
---|
166 | this->dwFlags = dwFlags;
|
---|
167 | this->hThread = hThread;
|
---|
168 | }
|
---|
169 | //******************************************************************************
|
---|
170 | //******************************************************************************
|
---|
171 | DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
---|
172 | {
|
---|
173 | EXCEPTION_FRAME exceptFrame;
|
---|
174 | Win32Thread *me = (Win32Thread *)lpData;
|
---|
175 | ULONG winthread = (ULONG)me->pCallback;
|
---|
176 | LPVOID userdata = me->lpUserData;
|
---|
177 | HANDLE hThread = me->hThread;
|
---|
178 | DWORD rc;
|
---|
179 |
|
---|
180 | delete(me); //only called once
|
---|
181 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
---|
182 |
|
---|
183 | TEB *winteb = (TEB *)InitializeTIB();
|
---|
184 | if(winteb == NULL) {
|
---|
185 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
---|
186 | DebugInt3();
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 | winteb->flags = me->dwFlags;
|
---|
190 |
|
---|
191 | winteb->entry_point = (void *)winthread;
|
---|
192 | winteb->entry_arg = (void *)userdata;
|
---|
193 | winteb->o.odin.hThread = hThread;
|
---|
194 |
|
---|
195 | winteb->o.odin.hab = OSLibWinInitialize();
|
---|
196 | winteb->o.odin.hmq = OSLibWinQueryMsgQueue(winteb->o.odin.hab);
|
---|
197 | dprintf(("Win32ThreadProc: hab %x hmq %x", winteb->o.odin.hab, winteb->o.odin.hmq));
|
---|
198 |
|
---|
199 | //Note: The Win32 exception structure referenced by FS:[0] is the same
|
---|
200 | // in OS/2
|
---|
201 | OS2SetExceptionHandler((void *)&exceptFrame);
|
---|
202 | winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
|
---|
203 |
|
---|
204 | SetWin32TIB();
|
---|
205 |
|
---|
206 | DWORD dwProcessAffinityMask, dwSystemAffinityMask;
|
---|
207 |
|
---|
208 | //Change the affinity mask of this thread to the mask for the whole process
|
---|
209 | if(GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask) == TRUE) {
|
---|
210 | SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
|
---|
211 | }
|
---|
212 |
|
---|
213 | WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
---|
214 | Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
---|
215 | Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
---|
216 |
|
---|
217 | //Set FPU control word to 0x27F (same as in NT)
|
---|
218 | CONTROL87(0x27F, 0xFFF);
|
---|
219 | rc = AsmCallThreadHandler(winthread, userdata);
|
---|
220 |
|
---|
221 | HMSetThreadTerminated(GetCurrentThread());
|
---|
222 | winteb->o.odin.exceptFrame = 0;
|
---|
223 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
224 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
225 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
226 | DestroyTIB();
|
---|
227 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
---|
228 |
|
---|
229 | return rc;
|
---|
230 | }
|
---|
231 | //******************************************************************************
|
---|
232 | //******************************************************************************
|
---|