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