| 1 | /* $Id: thread.cpp,v 1.31 2001-06-27 19:09:35 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 <handlemanager.h>
|
|---|
| 34 |
|
|---|
| 35 | #define DBG_LOCALLOG DBG_thread
|
|---|
| 36 | #include "dbglocal.h"
|
|---|
| 37 |
|
|---|
| 38 | ODINDEBUGCHANNEL(KERNEL32-THREAD)
|
|---|
| 39 |
|
|---|
| 40 | //******************************************************************************
|
|---|
| 41 | //******************************************************************************
|
|---|
| 42 | DWORD WIN32API GetCurrentThreadId()
|
|---|
| 43 | {
|
|---|
| 44 | //// dprintf(("GetCurrentThreadId\n"));
|
|---|
| 45 | return(O32_GetCurrentThreadId());
|
|---|
| 46 | }
|
|---|
| 47 | //******************************************************************************
|
|---|
| 48 | //******************************************************************************
|
|---|
| 49 | HANDLE WIN32API GetCurrentThread()
|
|---|
| 50 | {
|
|---|
| 51 | TEB *teb;
|
|---|
| 52 |
|
|---|
| 53 | teb = GetThreadTEB();
|
|---|
| 54 | if(teb == 0) {
|
|---|
| 55 | SetLastError(ERROR_INVALID_HANDLE); //todo
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 | return teb->o.odin.hThread;
|
|---|
| 59 | }
|
|---|
| 60 | //******************************************************************************
|
|---|
| 61 | //******************************************************************************
|
|---|
| 62 | VOID WIN32API ExitThread(DWORD exitcode)
|
|---|
| 63 | {
|
|---|
| 64 | EXCEPTION_FRAME *exceptFrame;
|
|---|
| 65 | TEB *teb;
|
|---|
| 66 |
|
|---|
| 67 | dprintf(("ExitThread %x (%x)", GetCurrentThread(), exitcode));
|
|---|
| 68 |
|
|---|
| 69 | teb = GetThreadTEB();
|
|---|
| 70 | if(teb != 0) {
|
|---|
| 71 | exceptFrame = (EXCEPTION_FRAME *)teb->o.odin.exceptFrame;
|
|---|
| 72 | }
|
|---|
| 73 | else DebugInt3();
|
|---|
| 74 |
|
|---|
| 75 | HMSetThreadTerminated(GetCurrentThread());
|
|---|
| 76 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
|---|
| 77 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
|---|
| 78 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
|---|
| 79 | DestroyTIB();
|
|---|
| 80 |
|
|---|
| 81 | if(exceptFrame) OS2UnsetExceptionHandler((void *)exceptFrame);
|
|---|
| 82 |
|
|---|
| 83 | O32_ExitThread(exitcode);
|
|---|
| 84 | }
|
|---|
| 85 | //******************************************************************************
|
|---|
| 86 | //******************************************************************************
|
|---|
| 87 | Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags, HANDLE hThread)
|
|---|
| 88 | {
|
|---|
| 89 | lpUserData = lpData;
|
|---|
| 90 | pCallback = pUserCallback;
|
|---|
| 91 | this->dwFlags = dwFlags;
|
|---|
| 92 | this->hThread = hThread;
|
|---|
| 93 | }
|
|---|
| 94 | //******************************************************************************
|
|---|
| 95 | //******************************************************************************
|
|---|
| 96 | DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
|---|
| 97 | {
|
|---|
| 98 | EXCEPTION_FRAME exceptFrame;
|
|---|
| 99 | Win32Thread *me = (Win32Thread *)lpData;
|
|---|
| 100 | ULONG winthread = (ULONG)me->pCallback;
|
|---|
| 101 | LPVOID userdata = me->lpUserData;
|
|---|
| 102 | HANDLE hThread = me->hThread;
|
|---|
| 103 | DWORD rc;
|
|---|
| 104 |
|
|---|
| 105 | delete(me); //only called once
|
|---|
| 106 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
|---|
| 107 |
|
|---|
| 108 | TEB *winteb = (TEB *)InitializeTIB();
|
|---|
| 109 | if(winteb == NULL) {
|
|---|
| 110 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
|---|
| 111 | DebugInt3();
|
|---|
| 112 | return 0;
|
|---|
| 113 | }
|
|---|
| 114 | winteb->flags = me->dwFlags;
|
|---|
| 115 |
|
|---|
| 116 | winteb->entry_point = (void *)winthread;
|
|---|
| 117 | winteb->entry_arg = (void *)userdata;
|
|---|
| 118 | winteb->o.odin.hThread = hThread;
|
|---|
| 119 |
|
|---|
| 120 | winteb->o.odin.hab = OSLibWinInitialize();
|
|---|
| 121 | winteb->o.odin.hmq = OSLibWinQueryMsgQueue(winteb->o.odin.hab);
|
|---|
| 122 | dprintf(("Win32ThreadProc: hab %x hmq %x", winteb->o.odin.hab, winteb->o.odin.hmq));
|
|---|
| 123 |
|
|---|
| 124 | //Note: The Win32 exception structure referenced by FS:[0] is the same
|
|---|
| 125 | // in OS/2
|
|---|
| 126 | OS2SetExceptionHandler((void *)&exceptFrame);
|
|---|
| 127 | winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
|
|---|
| 128 |
|
|---|
| 129 | SetWin32TIB();
|
|---|
| 130 | WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
|---|
| 131 | Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
|---|
| 132 | Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
|---|
| 133 |
|
|---|
| 134 | //Set FPU control word to 0x27F (same as in NT)
|
|---|
| 135 | CONTROL87(0x27F, 0xFFF);
|
|---|
| 136 | rc = AsmCallThreadHandler(winthread, userdata);
|
|---|
| 137 |
|
|---|
| 138 | HMSetThreadTerminated(GetCurrentThread());
|
|---|
| 139 | winteb->o.odin.exceptFrame = 0;
|
|---|
| 140 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
|---|
| 141 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
|---|
| 142 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
|---|
| 143 | DestroyTIB();
|
|---|
| 144 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
|---|
| 145 |
|
|---|
| 146 | return rc;
|
|---|
| 147 | }
|
|---|
| 148 | //******************************************************************************
|
|---|
| 149 | //******************************************************************************
|
|---|