| 1 | /* $Id: hmthread.cpp,v 1.9 2001-12-03 12:13:08 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 | #include <wprocess.h>
|
|---|
| 24 |
|
|---|
| 25 | #include <HandleManager.H>
|
|---|
| 26 | #include "HMThread.h"
|
|---|
| 27 | #include "oslibdos.h"
|
|---|
| 28 |
|
|---|
| 29 | #include <win\thread.h>
|
|---|
| 30 | #include "thread.h"
|
|---|
| 31 | #include "exceptutil.h"
|
|---|
| 32 |
|
|---|
| 33 | #define DBG_LOCALLOG DBG_hmthread
|
|---|
| 34 | #include "dbglocal.h"
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | //******************************************************************************
|
|---|
| 38 | //******************************************************************************
|
|---|
| 39 | HANDLE HMDeviceThreadClass::CreateThread(PHMHANDLEDATA pHMHandleData,
|
|---|
| 40 | LPSECURITY_ATTRIBUTES lpsa,
|
|---|
| 41 | DWORD cbStack,
|
|---|
| 42 | LPTHREAD_START_ROUTINE lpStartAddr,
|
|---|
| 43 | LPVOID lpvThreadParm,
|
|---|
| 44 | DWORD fdwCreate,
|
|---|
| 45 | LPDWORD lpIDThread,
|
|---|
| 46 | BOOL fFirstThread)
|
|---|
| 47 | {
|
|---|
| 48 | Win32Thread *winthread;
|
|---|
| 49 | DWORD threadid;
|
|---|
| 50 |
|
|---|
| 51 | if(lpIDThread == NULL) {
|
|---|
| 52 | lpIDThread = &threadid;
|
|---|
| 53 | }
|
|---|
| 54 | pHMHandleData->dwInternalType = HMTYPE_THREAD;
|
|---|
| 55 | pHMHandleData->dwUserData = THREAD_ALIVE;
|
|---|
| 56 |
|
|---|
| 57 | //SvL: This doesn't really create a thread, but only sets up the
|
|---|
| 58 | // handle of thread 0
|
|---|
| 59 | if(fFirstThread) {
|
|---|
| 60 | pHMHandleData->hHMHandle = O32_GetCurrentThread(); //return Open32 handle of thread
|
|---|
| 61 | return pHMHandleData->hHMHandle;
|
|---|
| 62 | }
|
|---|
| 63 | winthread = new Win32Thread(lpStartAddr, lpvThreadParm, fdwCreate, pHMHandleData->hHMHandle);
|
|---|
| 64 |
|
|---|
| 65 | if(winthread == 0) {
|
|---|
| 66 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 67 | return(0);
|
|---|
| 68 | }
|
|---|
| 69 | // @@@PH Note: with debug code enabled, ODIN might request more stack space!
|
|---|
| 70 | //SvL: Also need more stack in release build (RealPlayer 7 sometimes runs
|
|---|
| 71 | // out of stack
|
|---|
| 72 | if (cbStack > 0)
|
|---|
| 73 | cbStack <<= 1; // double stack
|
|---|
| 74 | else
|
|---|
| 75 | cbStack = 1048576; // per default 1MB stack per thread
|
|---|
| 76 |
|
|---|
| 77 | pHMHandleData->hHMHandle = O32_CreateThread(lpsa,
|
|---|
| 78 | cbStack,
|
|---|
| 79 | winthread->GetOS2Callback(),
|
|---|
| 80 | (LPVOID)winthread,
|
|---|
| 81 | fdwCreate,
|
|---|
| 82 | lpIDThread);
|
|---|
| 83 |
|
|---|
| 84 | dprintf(("CreateThread created %08xh\n", pHMHandleData->hHMHandle));
|
|---|
| 85 |
|
|---|
| 86 | return pHMHandleData->hHMHandle;
|
|---|
| 87 | }
|
|---|
| 88 | //******************************************************************************
|
|---|
| 89 | //******************************************************************************
|
|---|
| 90 | DWORD HMDeviceThreadClass::SuspendThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
|---|
| 91 | {
|
|---|
| 92 | dprintf(("SuspendThread %08xh)\n", pHMHandleData->hHMHandle));
|
|---|
| 93 |
|
|---|
| 94 | return O32_SuspendThread(pHMHandleData->hHMHandle);
|
|---|
| 95 | }
|
|---|
| 96 | //******************************************************************************
|
|---|
| 97 | //******************************************************************************
|
|---|
| 98 | INT HMDeviceThreadClass::GetThreadPriority(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
|---|
| 99 | {
|
|---|
| 100 | TEB *teb;
|
|---|
| 101 |
|
|---|
| 102 | dprintf(("GetThreadPriority(%08xh)\n", pHMHandleData->hHMHandle));
|
|---|
| 103 |
|
|---|
| 104 | teb = GetTEBFromThreadHandle(hThread);
|
|---|
| 105 | if(teb == NULL) {
|
|---|
| 106 | dprintf(("!WARNING!: TEB not found!!"));
|
|---|
| 107 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 108 | return THREAD_PRIORITY_ERROR_RETURN;
|
|---|
| 109 | }
|
|---|
| 110 | return teb->delta_priority;
|
|---|
| 111 | }
|
|---|
| 112 | //******************************************************************************
|
|---|
| 113 | //******************************************************************************
|
|---|
| 114 | BOOL HMDeviceThreadClass::SetThreadPriority(HANDLE hThread, PHMHANDLEDATA pHMHandleData, int priority)
|
|---|
| 115 | {
|
|---|
| 116 | TEB *teb;
|
|---|
| 117 |
|
|---|
| 118 | dprintf(("SetThreadPriority (%08xh,%08xh)", pHMHandleData->hHMHandle, priority));
|
|---|
| 119 |
|
|---|
| 120 | teb = GetTEBFromThreadHandle(hThread);
|
|---|
| 121 | if(teb == NULL) {
|
|---|
| 122 | dprintf(("!WARNING!: TEB not found!!"));
|
|---|
| 123 | SetLastError(ERROR_INVALID_HANDLE);
|
|---|
| 124 | return THREAD_PRIORITY_ERROR_RETURN;
|
|---|
| 125 | }
|
|---|
| 126 | DWORD ret = OSLibDosSetPriority(teb->o.odin.threadId, priority);
|
|---|
| 127 | if(ret == ERROR_SUCCESS) {
|
|---|
| 128 | teb->delta_priority = priority;
|
|---|
| 129 | return TRUE;
|
|---|
| 130 | }
|
|---|
| 131 | else {
|
|---|
| 132 | dprintf(("DosSetPriority failed with rc %d", ret));
|
|---|
| 133 | return FALSE;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | //******************************************************************************
|
|---|
| 137 | //TODO: Implement this??
|
|---|
| 138 | //******************************************************************************
|
|---|
| 139 | BOOL HMDeviceThreadClass::GetThreadContext(HANDLE hThread, PHMHANDLEDATA pHMHandleData, PCONTEXT lpContext)
|
|---|
| 140 | {
|
|---|
| 141 | dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n"));
|
|---|
| 142 | memset(lpContext, 0, sizeof(CONTEXT));
|
|---|
| 143 |
|
|---|
| 144 | /* make up some plausible values for segment registers */
|
|---|
| 145 | lpContext->SegCs = getCS();
|
|---|
| 146 | lpContext->SegDs = getDS();
|
|---|
| 147 | lpContext->SegSs = getSS();
|
|---|
| 148 | lpContext->SegEs = getES();
|
|---|
| 149 | lpContext->SegGs = getGS();
|
|---|
| 150 | lpContext->SegFs = GetFS();
|
|---|
| 151 |
|
|---|
| 152 | return TRUE;
|
|---|
| 153 | }
|
|---|
| 154 | //******************************************************************************
|
|---|
| 155 | //TODO: Implement this??
|
|---|
| 156 | //******************************************************************************
|
|---|
| 157 | BOOL HMDeviceThreadClass::SetThreadContext(HANDLE hThread, PHMHANDLEDATA pHMHandleData, const CONTEXT *lpContext)
|
|---|
| 158 | {
|
|---|
| 159 | dprintf(("SetThreadContext NOT IMPLEMENTED!!\n"));
|
|---|
| 160 |
|
|---|
| 161 | return FALSE;
|
|---|
| 162 | }
|
|---|
| 163 | //******************************************************************************
|
|---|
| 164 | //******************************************************************************
|
|---|
| 165 | BOOL HMDeviceThreadClass::TerminateThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData, DWORD exitcode)
|
|---|
| 166 | {
|
|---|
| 167 | dprintf(("TerminateThread (%08xh,%08xh)\n",
|
|---|
| 168 | pHMHandleData->hHMHandle,
|
|---|
| 169 | exitcode));
|
|---|
| 170 |
|
|---|
| 171 | pHMHandleData->dwUserData = THREAD_TERMINATED;
|
|---|
| 172 | return O32_TerminateThread(pHMHandleData->hHMHandle, exitcode);
|
|---|
| 173 | }
|
|---|
| 174 | //******************************************************************************
|
|---|
| 175 | //******************************************************************************
|
|---|
| 176 | BOOL HMDeviceThreadClass::SetThreadTerminated(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
|---|
| 177 | {
|
|---|
| 178 | pHMHandleData->dwUserData = THREAD_TERMINATED;
|
|---|
| 179 | return TRUE;
|
|---|
| 180 | }
|
|---|
| 181 | //******************************************************************************
|
|---|
| 182 | //******************************************************************************
|
|---|
| 183 | DWORD HMDeviceThreadClass::ResumeThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
|---|
| 184 | {
|
|---|
| 185 | dprintf(("ResumeThread (%08xh)\n",
|
|---|
| 186 | pHMHandleData->hHMHandle));
|
|---|
| 187 |
|
|---|
| 188 | return O32_ResumeThread(pHMHandleData->hHMHandle);
|
|---|
| 189 | }
|
|---|
| 190 | //******************************************************************************
|
|---|
| 191 | //******************************************************************************
|
|---|
| 192 | BOOL HMDeviceThreadClass::GetExitCodeThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData, LPDWORD lpExitCode)
|
|---|
| 193 | {
|
|---|
| 194 | dprintf(("GetExitCodeThread (%08xh,%08xh)\n",
|
|---|
| 195 | pHMHandleData->hHMHandle,
|
|---|
| 196 | lpExitCode));
|
|---|
| 197 |
|
|---|
| 198 | #if 0
|
|---|
| 199 | if(pHMHandleData->dwUserData == THREAD_ALIVE) {
|
|---|
| 200 | lpExitCode == STILL_ALIVE;
|
|---|
| 201 | return TRUE;
|
|---|
| 202 | }
|
|---|
| 203 | #endif
|
|---|
| 204 | return O32_GetExitCodeThread(pHMHandleData->hHMHandle, lpExitCode);
|
|---|
| 205 | }
|
|---|
| 206 | //******************************************************************************
|
|---|
| 207 | //******************************************************************************
|
|---|
| 208 | BOOL HMDeviceThreadClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
|
|---|
| 209 | {
|
|---|
| 210 | return TRUE;
|
|---|
| 211 | }
|
|---|
| 212 | //******************************************************************************
|
|---|
| 213 | //******************************************************************************
|
|---|
| 214 | DWORD HMDeviceThreadClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData,
|
|---|
| 215 | DWORD dwTimeout)
|
|---|
| 216 | {
|
|---|
| 217 | dprintf(("HMThread::WaitForSingleObject (%08xh,%08xh)\n",
|
|---|
| 218 | pHMHandleData->hHMHandle,
|
|---|
| 219 | dwTimeout));
|
|---|
| 220 |
|
|---|
| 221 | //This doesn't work very well in Open32 (object's state never signaled)
|
|---|
| 222 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) {
|
|---|
| 223 | return WAIT_OBJECT_0;
|
|---|
| 224 | }
|
|---|
| 225 | return HMDeviceOpen32Class::WaitForSingleObject(pHMHandleData, dwTimeout);
|
|---|
| 226 | }
|
|---|
| 227 | //******************************************************************************
|
|---|
| 228 | //******************************************************************************
|
|---|
| 229 | DWORD HMDeviceThreadClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData,
|
|---|
| 230 | DWORD dwTimeout,
|
|---|
| 231 | BOOL fAlertable)
|
|---|
| 232 | {
|
|---|
| 233 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) {
|
|---|
| 234 | return WAIT_OBJECT_0;
|
|---|
| 235 | }
|
|---|
| 236 | return HMDeviceOpen32Class::WaitForSingleObjectEx(pHMHandleData, dwTimeout, fAlertable);
|
|---|
| 237 | }
|
|---|
| 238 | //******************************************************************************
|
|---|
| 239 | //******************************************************************************
|
|---|