| 1 | /* $Id: hmthread.cpp,v 1.4 2000-03-30 10:52:04 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 5 | * | 
|---|
| 6 | * Win32 thread handle class | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * TODO: Handle is not destroyed when thread terminates (or else GetExitCodeThread won't work) | 
|---|
| 10 | *       Create thread token during thread creation?? | 
|---|
| 11 | *       Fix for WaitForSingleObject when thread is already terminated, but | 
|---|
| 12 | *       WaitForMultipleObjects can still fail! | 
|---|
| 13 | *       WaitForSingle/MultipleObjects needs to be rewritten! (not using | 
|---|
| 14 | *       Open32) | 
|---|
| 15 | * | 
|---|
| 16 | * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 17 | * | 
|---|
| 18 | */ | 
|---|
| 19 | #include <os2win.h> | 
|---|
| 20 | #include <stdlib.h> | 
|---|
| 21 | #include <string.h> | 
|---|
| 22 | #include <misc.h> | 
|---|
| 23 |  | 
|---|
| 24 | #include <HandleManager.H> | 
|---|
| 25 | #include "HMThread.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include <win\thread.h> | 
|---|
| 28 | #include "thread.h" | 
|---|
| 29 | #include "exceptutil.h" | 
|---|
| 30 |  | 
|---|
| 31 | #define DBG_LOCALLOG    DBG_hmthread | 
|---|
| 32 | #include "dbglocal.h" | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | //****************************************************************************** | 
|---|
| 36 | //****************************************************************************** | 
|---|
| 37 | HANDLE HMDeviceThreadClass::CreateThread(PHMHANDLEDATA          pHMHandleData, | 
|---|
| 38 | LPSECURITY_ATTRIBUTES  lpsa, | 
|---|
| 39 | DWORD                  cbStack, | 
|---|
| 40 | LPTHREAD_START_ROUTINE lpStartAddr, | 
|---|
| 41 | LPVOID                 lpvThreadParm, | 
|---|
| 42 | DWORD                  fdwCreate, | 
|---|
| 43 | LPDWORD                lpIDThread, | 
|---|
| 44 | BOOL                   fFirstThread) | 
|---|
| 45 | { | 
|---|
| 46 | Win32Thread *winthread; | 
|---|
| 47 |  | 
|---|
| 48 | pHMHandleData->dwInternalType = HMTYPE_THREAD; | 
|---|
| 49 | pHMHandleData->dwUserData     = THREAD_ALIVE; | 
|---|
| 50 |  | 
|---|
| 51 | //SvL: This doesn't really create a thread, but only sets up the | 
|---|
| 52 | //     handle of thread 0 | 
|---|
| 53 | if(fFirstThread) { | 
|---|
| 54 | pHMHandleData->hHMHandle = O32_GetCurrentThread(); //return Open32 handle of thread | 
|---|
| 55 | return pHMHandleData->hHMHandle; | 
|---|
| 56 | } | 
|---|
| 57 | winthread = new Win32Thread(lpStartAddr, lpvThreadParm, fdwCreate, pHMHandleData->hHMHandle); | 
|---|
| 58 |  | 
|---|
| 59 | if(winthread == 0) | 
|---|
| 60 | return(0); | 
|---|
| 61 |  | 
|---|
| 62 | // @@@PH Note: with debug code enabled, ODIN might request more stack space! | 
|---|
| 63 | //SvL: Also need more stack in release build (RealPlayer 7 sometimes runs | 
|---|
| 64 | //     out of stack | 
|---|
| 65 | if (cbStack > 0) | 
|---|
| 66 | cbStack <<= 1;     // double stack | 
|---|
| 67 | else | 
|---|
| 68 | cbStack = 1048576; // per default 1MB stack per thread | 
|---|
| 69 |  | 
|---|
| 70 | pHMHandleData->hHMHandle = O32_CreateThread(lpsa, | 
|---|
| 71 | cbStack, | 
|---|
| 72 | winthread->GetOS2Callback(), | 
|---|
| 73 | (LPVOID)winthread, | 
|---|
| 74 | fdwCreate, | 
|---|
| 75 | lpIDThread); | 
|---|
| 76 | return pHMHandleData->hHMHandle; | 
|---|
| 77 | } | 
|---|
| 78 | //****************************************************************************** | 
|---|
| 79 | //****************************************************************************** | 
|---|
| 80 | INT HMDeviceThreadClass::GetThreadPriority(PHMHANDLEDATA pHMHandleData) | 
|---|
| 81 | { | 
|---|
| 82 | dprintf(("OS2GetThreadPriority(%08xh)\n", | 
|---|
| 83 | pHMHandleData->hHMHandle)); | 
|---|
| 84 |  | 
|---|
| 85 | return O32_GetThreadPriority(pHMHandleData->hHMHandle); | 
|---|
| 86 | } | 
|---|
| 87 | //****************************************************************************** | 
|---|
| 88 | //****************************************************************************** | 
|---|
| 89 | DWORD HMDeviceThreadClass::SuspendThread(PHMHANDLEDATA pHMHandleData) | 
|---|
| 90 | { | 
|---|
| 91 | dprintf(("OS2SuspendThread %08xh)\n", | 
|---|
| 92 | pHMHandleData->hHMHandle)); | 
|---|
| 93 |  | 
|---|
| 94 | return O32_SuspendThread(pHMHandleData->hHMHandle); | 
|---|
| 95 | } | 
|---|
| 96 | //****************************************************************************** | 
|---|
| 97 | //****************************************************************************** | 
|---|
| 98 | BOOL HMDeviceThreadClass::SetThreadPriority(PHMHANDLEDATA pHMHandleData, int priority) | 
|---|
| 99 | { | 
|---|
| 100 | dprintf(("OS2SetThreadPriority (%08xh,%08xh)\n", | 
|---|
| 101 | pHMHandleData->hHMHandle, | 
|---|
| 102 | priority)); | 
|---|
| 103 |  | 
|---|
| 104 | return O32_SetThreadPriority(pHMHandleData->hHMHandle, priority); | 
|---|
| 105 | } | 
|---|
| 106 | //****************************************************************************** | 
|---|
| 107 | //TODO: Implement this?? | 
|---|
| 108 | //****************************************************************************** | 
|---|
| 109 | BOOL HMDeviceThreadClass::GetThreadContext(PHMHANDLEDATA pHMHandleData, PCONTEXT lpContext) | 
|---|
| 110 | { | 
|---|
| 111 | dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n")); | 
|---|
| 112 | memset(lpContext, 0, sizeof(CONTEXT)); | 
|---|
| 113 |  | 
|---|
| 114 | /* make up some plausible values for segment registers */ | 
|---|
| 115 | lpContext->SegCs   = getCS(); | 
|---|
| 116 | lpContext->SegDs   = getDS(); | 
|---|
| 117 | lpContext->SegSs   = getSS(); | 
|---|
| 118 | lpContext->SegEs   = getES(); | 
|---|
| 119 | lpContext->SegGs   = getGS(); | 
|---|
| 120 | lpContext->SegFs   = GetFS(); | 
|---|
| 121 |  | 
|---|
| 122 | return TRUE; | 
|---|
| 123 | } | 
|---|
| 124 | //****************************************************************************** | 
|---|
| 125 | //TODO: Implement this?? | 
|---|
| 126 | //****************************************************************************** | 
|---|
| 127 | BOOL HMDeviceThreadClass::SetThreadContext(PHMHANDLEDATA pHMHandleData, const CONTEXT *lpContext) | 
|---|
| 128 | { | 
|---|
| 129 | dprintf(("SetThreadContext NOT IMPLEMENTED!!\n")); | 
|---|
| 130 |  | 
|---|
| 131 | return FALSE; | 
|---|
| 132 | } | 
|---|
| 133 | //****************************************************************************** | 
|---|
| 134 | //****************************************************************************** | 
|---|
| 135 | BOOL HMDeviceThreadClass::TerminateThread(PHMHANDLEDATA pHMHandleData, DWORD exitcode) | 
|---|
| 136 | { | 
|---|
| 137 | dprintf(("TerminateThread (%08xh,%08xh)\n", | 
|---|
| 138 | pHMHandleData->hHMHandle, | 
|---|
| 139 | exitcode)); | 
|---|
| 140 |  | 
|---|
| 141 | pHMHandleData->dwUserData = THREAD_TERMINATED; | 
|---|
| 142 | return O32_TerminateThread(pHMHandleData->hHMHandle, exitcode); | 
|---|
| 143 | } | 
|---|
| 144 | //****************************************************************************** | 
|---|
| 145 | //****************************************************************************** | 
|---|
| 146 | BOOL HMDeviceThreadClass::SetThreadTerminated(PHMHANDLEDATA pHMHandleData) | 
|---|
| 147 | { | 
|---|
| 148 | pHMHandleData->dwUserData = THREAD_TERMINATED; | 
|---|
| 149 | return TRUE; | 
|---|
| 150 | } | 
|---|
| 151 | //****************************************************************************** | 
|---|
| 152 | //****************************************************************************** | 
|---|
| 153 | DWORD HMDeviceThreadClass::ResumeThread(PHMHANDLEDATA pHMHandleData) | 
|---|
| 154 | { | 
|---|
| 155 | dprintf(("ResumeThread (%08xh)\n", | 
|---|
| 156 | pHMHandleData->hHMHandle)); | 
|---|
| 157 |  | 
|---|
| 158 | return O32_ResumeThread(pHMHandleData->hHMHandle); | 
|---|
| 159 | } | 
|---|
| 160 | //****************************************************************************** | 
|---|
| 161 | //****************************************************************************** | 
|---|
| 162 | BOOL HMDeviceThreadClass::GetExitCodeThread(PHMHANDLEDATA pHMHandleData, LPDWORD lpExitCode) | 
|---|
| 163 | { | 
|---|
| 164 | dprintf(("GetExitCodeThread (%08xh,%08xh)\n", | 
|---|
| 165 | pHMHandleData->hHMHandle, | 
|---|
| 166 | lpExitCode)); | 
|---|
| 167 |  | 
|---|
| 168 | #if 0 | 
|---|
| 169 | if(pHMHandleData->dwUserData == THREAD_ALIVE) { | 
|---|
| 170 | lpExitCode == STILL_ALIVE; | 
|---|
| 171 | return TRUE; | 
|---|
| 172 | } | 
|---|
| 173 | #endif | 
|---|
| 174 | return O32_GetExitCodeThread(pHMHandleData->hHMHandle, lpExitCode); | 
|---|
| 175 | } | 
|---|
| 176 | //****************************************************************************** | 
|---|
| 177 | //****************************************************************************** | 
|---|
| 178 | DWORD HMDeviceThreadClass::CloseHandle(PHMHANDLEDATA pHMHandleData) | 
|---|
| 179 | { | 
|---|
| 180 | return STATUS_SUCCESS; | 
|---|
| 181 | } | 
|---|
| 182 | //****************************************************************************** | 
|---|
| 183 | //****************************************************************************** | 
|---|
| 184 | DWORD HMDeviceThreadClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData, | 
|---|
| 185 | DWORD  dwTimeout) | 
|---|
| 186 | { | 
|---|
| 187 | //This doesn't work very well in Open32 (object's state never signaled) | 
|---|
| 188 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) { | 
|---|
| 189 | return WAIT_OBJECT_0; | 
|---|
| 190 | } | 
|---|
| 191 | return HMDeviceOpen32Class::WaitForSingleObject(pHMHandleData, dwTimeout); | 
|---|
| 192 | } | 
|---|
| 193 | //****************************************************************************** | 
|---|
| 194 | //****************************************************************************** | 
|---|
| 195 | DWORD HMDeviceThreadClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData, | 
|---|
| 196 | DWORD  dwTimeout, | 
|---|
| 197 | BOOL   fAlertable) | 
|---|
| 198 | { | 
|---|
| 199 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) { | 
|---|
| 200 | return WAIT_OBJECT_0; | 
|---|
| 201 | } | 
|---|
| 202 | return HMDeviceOpen32Class::WaitForSingleObjectEx(pHMHandleData, dwTimeout, fAlertable); | 
|---|
| 203 | } | 
|---|
| 204 | //****************************************************************************** | 
|---|
| 205 | //****************************************************************************** | 
|---|