| 1 | /* $Id: thread.cpp,v 1.42 2002-02-07 11:19:13 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 | static ULONG priorityclass = NORMAL_PRIORITY_CLASS;
|
|---|
| 42 |
|
|---|
| 43 | //******************************************************************************
|
|---|
| 44 | //******************************************************************************
|
|---|
| 45 | DWORD 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(O32_GetCurrentThreadId());
|
|---|
| 57 | }
|
|---|
| 58 | //******************************************************************************
|
|---|
| 59 | //******************************************************************************
|
|---|
| 60 | HANDLE 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 | //******************************************************************************
|
|---|
| 75 | ULONG 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 | //******************************************************************************
|
|---|
| 91 | void 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 | //******************************************************************************
|
|---|
| 104 | void 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 | //******************************************************************************
|
|---|
| 130 | void 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 | //******************************************************************************
|
|---|
| 142 | void 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 | //******************************************************************************
|
|---|
| 165 | char* 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 | //******************************************************************************
|
|---|
| 188 | VOID 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 | DestroyTIB();
|
|---|
| 206 |
|
|---|
| 207 | if(exceptFrame) OS2UnsetExceptionHandler((void *)exceptFrame);
|
|---|
| 208 |
|
|---|
| 209 | O32_ExitThread(exitcode);
|
|---|
| 210 | }
|
|---|
| 211 | /*****************************************************************************
|
|---|
| 212 | * Name : DWORD SetThreadAffinityMask
|
|---|
| 213 | * Purpose : The SetThreadAffinityMask function sets a processor affinity
|
|---|
| 214 | * mask for a specified thread.
|
|---|
| 215 | * A thread affinity mask is a bit vector in which each bit
|
|---|
| 216 | * represents the processors that a thread is allowed to run on.
|
|---|
| 217 | * A thread affinity mask must be a proper subset of the process
|
|---|
| 218 | * affinity mask for the containing process of a thread. A thread
|
|---|
| 219 | * is only allowed to run on the processors its process is allowed to run on.
|
|---|
| 220 | * Parameters: HANDLE hThread handle to the thread of interest
|
|---|
| 221 | * DWORD dwThreadAffinityMask a thread affinity mask
|
|---|
| 222 | * Variables :
|
|---|
| 223 | * Result : TRUE / FALSE
|
|---|
| 224 | * Remark :
|
|---|
| 225 | * Status : UNTESTED STUB
|
|---|
| 226 | *
|
|---|
| 227 | * Author : Patrick Haller [Mon, 1998/06/15 08:00]
|
|---|
| 228 | *****************************************************************************/
|
|---|
| 229 |
|
|---|
| 230 | DWORD WIN32API SetThreadAffinityMask(HANDLE hThread,
|
|---|
| 231 | DWORD dwThreadAffinityMask)
|
|---|
| 232 | {
|
|---|
| 233 | dprintf(("KERNEL32: SetThreadAffinityMask(%08xh,%08xh)",
|
|---|
| 234 | 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 | //******************************************************************************
|
|---|
| 244 | VOID WIN32API Sleep(DWORD mSecs)
|
|---|
| 245 | {
|
|---|
| 246 | dprintf2(("KERNEL32: Sleep %d", mSecs));
|
|---|
| 247 | OSLibDosSleep(mSecs);
|
|---|
| 248 | }
|
|---|
| 249 | //******************************************************************************
|
|---|
| 250 | //******************************************************************************
|
|---|
| 251 | DWORD WIN32API GetPriorityClass(HANDLE hProcess)
|
|---|
| 252 | {
|
|---|
| 253 | dprintf(("KERNEL32: GetPriorityClass %x", hProcess));
|
|---|
| 254 | return priorityclass;
|
|---|
| 255 | // return O32_GetPriorityClass(hProcess);
|
|---|
| 256 | }
|
|---|
| 257 | //******************************************************************************
|
|---|
| 258 | //******************************************************************************
|
|---|
| 259 | BOOL 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 | //******************************************************************************
|
|---|
| 268 | Win32Thread::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 | //******************************************************************************
|
|---|
| 276 | //******************************************************************************
|
|---|
| 277 | DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
|---|
| 278 | {
|
|---|
| 279 | EXCEPTION_FRAME exceptFrame;
|
|---|
| 280 | Win32Thread *me = (Win32Thread *)lpData;
|
|---|
| 281 | ULONG threadCallback = (ULONG)me->pCallback;
|
|---|
| 282 | LPVOID userdata = me->lpUserData;
|
|---|
| 283 | HANDLE hThread = me->hThread;
|
|---|
| 284 | DWORD rc;
|
|---|
| 285 |
|
|---|
| 286 | delete(me); //only called once
|
|---|
| 287 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
|---|
| 288 |
|
|---|
| 289 | TEB *winteb = (TEB *)InitializeTIB();
|
|---|
| 290 | if(winteb == NULL) {
|
|---|
| 291 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
|---|
| 292 | DebugInt3();
|
|---|
| 293 | return 0;
|
|---|
| 294 | }
|
|---|
| 295 | winteb->flags = me->dwFlags;
|
|---|
| 296 |
|
|---|
| 297 | winteb->entry_point = (void *)threadCallback;
|
|---|
| 298 | winteb->entry_arg = (void *)userdata;
|
|---|
| 299 | winteb->o.odin.hThread = hThread;
|
|---|
| 300 |
|
|---|
| 301 | winteb->o.odin.hab = OSLibWinInitialize();
|
|---|
| 302 | winteb->o.odin.hmq = OSLibWinQueryMsgQueue(winteb->o.odin.hab);
|
|---|
| 303 | dprintf(("Win32ThreadProc: hab %x hmq %x", winteb->o.odin.hab, winteb->o.odin.hmq));
|
|---|
| 304 |
|
|---|
| 305 | //Note: The Win32 exception structure referenced by FS:[0] is the same
|
|---|
| 306 | // in OS/2
|
|---|
| 307 | OS2SetExceptionHandler((void *)&exceptFrame);
|
|---|
| 308 | winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
|
|---|
| 309 |
|
|---|
| 310 | //Determine if thread callback is inside a PE dll; if true, then force
|
|---|
| 311 | //switch to win32 TIB (FS selector)
|
|---|
| 312 | //(necessary for Opera when loading win32 plugins that create threads)
|
|---|
| 313 | Win32DllBase *dll;
|
|---|
| 314 | dll = Win32DllBase::findModuleByAddr(threadCallback);
|
|---|
| 315 | if(dll && dll->isPEImage()) {
|
|---|
| 316 | dprintf(("Win32ThreadProc: Force win32 TIB switch"));
|
|---|
| 317 | SetWin32TIB(TIB_SWITCH_FORCE_WIN32);
|
|---|
| 318 | }
|
|---|
| 319 | else SetWin32TIB(TIB_SWITCH_DEFAULT); //executable type determines whether or not FS is changed
|
|---|
| 320 |
|
|---|
| 321 | DWORD dwProcessAffinityMask, dwSystemAffinityMask;
|
|---|
| 322 |
|
|---|
| 323 | //Change the affinity mask of this thread to the mask for the whole process
|
|---|
| 324 | if(GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask) == TRUE) {
|
|---|
| 325 | SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | if(WinExe) WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
|---|
| 329 | Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
|---|
| 330 | Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
|---|
| 331 |
|
|---|
| 332 | //Set FPU control word to 0x27F (same as in NT)
|
|---|
| 333 | CONTROL87(0x27F, 0xFFF);
|
|---|
| 334 | rc = AsmCallThreadHandler(threadCallback, userdata);
|
|---|
| 335 |
|
|---|
| 336 | if(fExitProcess) {
|
|---|
| 337 | OSLibDosExitThread(rc);
|
|---|
| 338 | }
|
|---|
| 339 | else {
|
|---|
| 340 | HMSetThreadTerminated(GetCurrentThread());
|
|---|
| 341 | winteb->o.odin.exceptFrame = 0;
|
|---|
| 342 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
|---|
| 343 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
|---|
| 344 | if(WinExe) WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
|---|
| 345 | DestroyTIB(); //destroys TIB and restores FS
|
|---|
| 346 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | return rc;
|
|---|
| 350 | }
|
|---|
| 351 | //******************************************************************************
|
|---|
| 352 | //******************************************************************************
|
|---|