| 1 | /* $Id: hmthread.cpp,v 1.8 2001-06-06 18:59:57 phaller 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 | DWORD threadid; | 
|---|
| 48 |  | 
|---|
| 49 | if(lpIDThread == NULL) { | 
|---|
| 50 | lpIDThread = &threadid; | 
|---|
| 51 | } | 
|---|
| 52 | pHMHandleData->dwInternalType = HMTYPE_THREAD; | 
|---|
| 53 | pHMHandleData->dwUserData     = THREAD_ALIVE; | 
|---|
| 54 |  | 
|---|
| 55 | //SvL: This doesn't really create a thread, but only sets up the | 
|---|
| 56 | //     handle of thread 0 | 
|---|
| 57 | if(fFirstThread) { | 
|---|
| 58 | pHMHandleData->hHMHandle = O32_GetCurrentThread(); //return Open32 handle of thread | 
|---|
| 59 | return pHMHandleData->hHMHandle; | 
|---|
| 60 | } | 
|---|
| 61 | winthread = new Win32Thread(lpStartAddr, lpvThreadParm, fdwCreate, pHMHandleData->hHMHandle); | 
|---|
| 62 |  | 
|---|
| 63 | if(winthread == 0) | 
|---|
| 64 | return(0); | 
|---|
| 65 |  | 
|---|
| 66 | // @@@PH Note: with debug code enabled, ODIN might request more stack space! | 
|---|
| 67 | //SvL: Also need more stack in release build (RealPlayer 7 sometimes runs | 
|---|
| 68 | //     out of stack | 
|---|
| 69 | if (cbStack > 0) | 
|---|
| 70 | cbStack <<= 1;     // double stack | 
|---|
| 71 | else | 
|---|
| 72 | cbStack = 1048576; // per default 1MB stack per thread | 
|---|
| 73 |  | 
|---|
| 74 | pHMHandleData->hHMHandle = O32_CreateThread(lpsa, | 
|---|
| 75 | cbStack, | 
|---|
| 76 | winthread->GetOS2Callback(), | 
|---|
| 77 | (LPVOID)winthread, | 
|---|
| 78 | fdwCreate, | 
|---|
| 79 | lpIDThread); | 
|---|
| 80 |  | 
|---|
| 81 | dprintf(("CreateThread created %08xh\n", pHMHandleData->hHMHandle)); | 
|---|
| 82 |  | 
|---|
| 83 | return pHMHandleData->hHMHandle; | 
|---|
| 84 | } | 
|---|
| 85 | //****************************************************************************** | 
|---|
| 86 | //****************************************************************************** | 
|---|
| 87 | INT HMDeviceThreadClass::GetThreadPriority(PHMHANDLEDATA pHMHandleData) | 
|---|
| 88 | { | 
|---|
| 89 | dprintf(("GetThreadPriority(%08xh)\n", pHMHandleData->hHMHandle)); | 
|---|
| 90 |  | 
|---|
| 91 | return O32_GetThreadPriority(pHMHandleData->hHMHandle); | 
|---|
| 92 | } | 
|---|
| 93 | //****************************************************************************** | 
|---|
| 94 | //****************************************************************************** | 
|---|
| 95 | DWORD HMDeviceThreadClass::SuspendThread(PHMHANDLEDATA pHMHandleData) | 
|---|
| 96 | { | 
|---|
| 97 | dprintf(("SuspendThread %08xh)\n", pHMHandleData->hHMHandle)); | 
|---|
| 98 |  | 
|---|
| 99 | return O32_SuspendThread(pHMHandleData->hHMHandle); | 
|---|
| 100 | } | 
|---|
| 101 | //****************************************************************************** | 
|---|
| 102 | //****************************************************************************** | 
|---|
| 103 | BOOL HMDeviceThreadClass::SetThreadPriority(PHMHANDLEDATA pHMHandleData, int priority) | 
|---|
| 104 | { | 
|---|
| 105 | dprintf(("SetThreadPriority (%08xh,%08xh)\n", | 
|---|
| 106 | pHMHandleData->hHMHandle, | 
|---|
| 107 | priority)); | 
|---|
| 108 |  | 
|---|
| 109 | return O32_SetThreadPriority(pHMHandleData->hHMHandle, priority); | 
|---|
| 110 | } | 
|---|
| 111 | //****************************************************************************** | 
|---|
| 112 | //TODO: Implement this?? | 
|---|
| 113 | //****************************************************************************** | 
|---|
| 114 | BOOL HMDeviceThreadClass::GetThreadContext(PHMHANDLEDATA pHMHandleData, PCONTEXT lpContext) | 
|---|
| 115 | { | 
|---|
| 116 | dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n")); | 
|---|
| 117 | memset(lpContext, 0, sizeof(CONTEXT)); | 
|---|
| 118 |  | 
|---|
| 119 | /* make up some plausible values for segment registers */ | 
|---|
| 120 | lpContext->SegCs   = getCS(); | 
|---|
| 121 | lpContext->SegDs   = getDS(); | 
|---|
| 122 | lpContext->SegSs   = getSS(); | 
|---|
| 123 | lpContext->SegEs   = getES(); | 
|---|
| 124 | lpContext->SegGs   = getGS(); | 
|---|
| 125 | lpContext->SegFs   = GetFS(); | 
|---|
| 126 |  | 
|---|
| 127 | return TRUE; | 
|---|
| 128 | } | 
|---|
| 129 | //****************************************************************************** | 
|---|
| 130 | //TODO: Implement this?? | 
|---|
| 131 | //****************************************************************************** | 
|---|
| 132 | BOOL HMDeviceThreadClass::SetThreadContext(PHMHANDLEDATA pHMHandleData, const CONTEXT *lpContext) | 
|---|
| 133 | { | 
|---|
| 134 | dprintf(("SetThreadContext NOT IMPLEMENTED!!\n")); | 
|---|
| 135 |  | 
|---|
| 136 | return FALSE; | 
|---|
| 137 | } | 
|---|
| 138 | //****************************************************************************** | 
|---|
| 139 | //****************************************************************************** | 
|---|
| 140 | BOOL HMDeviceThreadClass::TerminateThread(PHMHANDLEDATA pHMHandleData, DWORD exitcode) | 
|---|
| 141 | { | 
|---|
| 142 | dprintf(("TerminateThread (%08xh,%08xh)\n", | 
|---|
| 143 | pHMHandleData->hHMHandle, | 
|---|
| 144 | exitcode)); | 
|---|
| 145 |  | 
|---|
| 146 | pHMHandleData->dwUserData = THREAD_TERMINATED; | 
|---|
| 147 | return O32_TerminateThread(pHMHandleData->hHMHandle, exitcode); | 
|---|
| 148 | } | 
|---|
| 149 | //****************************************************************************** | 
|---|
| 150 | //****************************************************************************** | 
|---|
| 151 | BOOL HMDeviceThreadClass::SetThreadTerminated(PHMHANDLEDATA pHMHandleData) | 
|---|
| 152 | { | 
|---|
| 153 | pHMHandleData->dwUserData = THREAD_TERMINATED; | 
|---|
| 154 | return TRUE; | 
|---|
| 155 | } | 
|---|
| 156 | //****************************************************************************** | 
|---|
| 157 | //****************************************************************************** | 
|---|
| 158 | DWORD HMDeviceThreadClass::ResumeThread(PHMHANDLEDATA pHMHandleData) | 
|---|
| 159 | { | 
|---|
| 160 | dprintf(("ResumeThread (%08xh)\n", | 
|---|
| 161 | pHMHandleData->hHMHandle)); | 
|---|
| 162 |  | 
|---|
| 163 | return O32_ResumeThread(pHMHandleData->hHMHandle); | 
|---|
| 164 | } | 
|---|
| 165 | //****************************************************************************** | 
|---|
| 166 | //****************************************************************************** | 
|---|
| 167 | BOOL HMDeviceThreadClass::GetExitCodeThread(PHMHANDLEDATA pHMHandleData, LPDWORD lpExitCode) | 
|---|
| 168 | { | 
|---|
| 169 | dprintf(("GetExitCodeThread (%08xh,%08xh)\n", | 
|---|
| 170 | pHMHandleData->hHMHandle, | 
|---|
| 171 | lpExitCode)); | 
|---|
| 172 |  | 
|---|
| 173 | #if 0 | 
|---|
| 174 | if(pHMHandleData->dwUserData == THREAD_ALIVE) { | 
|---|
| 175 | lpExitCode == STILL_ALIVE; | 
|---|
| 176 | return TRUE; | 
|---|
| 177 | } | 
|---|
| 178 | #endif | 
|---|
| 179 | return O32_GetExitCodeThread(pHMHandleData->hHMHandle, lpExitCode); | 
|---|
| 180 | } | 
|---|
| 181 | //****************************************************************************** | 
|---|
| 182 | //****************************************************************************** | 
|---|
| 183 | BOOL HMDeviceThreadClass::CloseHandle(PHMHANDLEDATA pHMHandleData) | 
|---|
| 184 | { | 
|---|
| 185 | return TRUE; | 
|---|
| 186 | } | 
|---|
| 187 | //****************************************************************************** | 
|---|
| 188 | //****************************************************************************** | 
|---|
| 189 | DWORD HMDeviceThreadClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData, | 
|---|
| 190 | DWORD  dwTimeout) | 
|---|
| 191 | { | 
|---|
| 192 | dprintf(("HMThread::WaitForSingleObject (%08xh,%08xh)\n", | 
|---|
| 193 | pHMHandleData->hHMHandle, | 
|---|
| 194 | dwTimeout)); | 
|---|
| 195 |  | 
|---|
| 196 | //This doesn't work very well in Open32 (object's state never signaled) | 
|---|
| 197 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) { | 
|---|
| 198 | return WAIT_OBJECT_0; | 
|---|
| 199 | } | 
|---|
| 200 | return HMDeviceOpen32Class::WaitForSingleObject(pHMHandleData, dwTimeout); | 
|---|
| 201 | } | 
|---|
| 202 | //****************************************************************************** | 
|---|
| 203 | //****************************************************************************** | 
|---|
| 204 | DWORD HMDeviceThreadClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData, | 
|---|
| 205 | DWORD  dwTimeout, | 
|---|
| 206 | BOOL   fAlertable) | 
|---|
| 207 | { | 
|---|
| 208 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) { | 
|---|
| 209 | return WAIT_OBJECT_0; | 
|---|
| 210 | } | 
|---|
| 211 | return HMDeviceOpen32Class::WaitForSingleObjectEx(pHMHandleData, dwTimeout, fAlertable); | 
|---|
| 212 | } | 
|---|
| 213 | //****************************************************************************** | 
|---|
| 214 | //****************************************************************************** | 
|---|