1 | /* $Id: hmthread.cpp,v 1.12 2002-05-22 12:57:42 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 "asmutil.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 | if(lpIDThread) {
|
---|
85 | *lpIDThread = MAKE_THREADID(O32_GetCurrentProcessId(), *lpIDThread);
|
---|
86 | }
|
---|
87 | dprintf(("CreateThread created %08xh\n", pHMHandleData->hHMHandle));
|
---|
88 |
|
---|
89 | return pHMHandleData->hHMHandle;
|
---|
90 | }
|
---|
91 | //******************************************************************************
|
---|
92 | //******************************************************************************
|
---|
93 | DWORD HMDeviceThreadClass::SuspendThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
---|
94 | {
|
---|
95 | dprintf(("SuspendThread %08xh)\n", pHMHandleData->hHMHandle));
|
---|
96 |
|
---|
97 | return O32_SuspendThread(pHMHandleData->hHMHandle);
|
---|
98 | }
|
---|
99 | //******************************************************************************
|
---|
100 | //******************************************************************************
|
---|
101 | INT HMDeviceThreadClass::GetThreadPriority(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
---|
102 | {
|
---|
103 | TEB *teb;
|
---|
104 |
|
---|
105 | dprintf(("GetThreadPriority(%08xh)\n", pHMHandleData->hHMHandle));
|
---|
106 |
|
---|
107 | teb = GetTEBFromThreadHandle(hThread);
|
---|
108 | if(teb == NULL) {
|
---|
109 | dprintf(("!WARNING!: TEB not found!!"));
|
---|
110 | SetLastError(ERROR_INVALID_HANDLE);
|
---|
111 | return THREAD_PRIORITY_ERROR_RETURN;
|
---|
112 | }
|
---|
113 | return teb->delta_priority;
|
---|
114 | }
|
---|
115 | //******************************************************************************
|
---|
116 | //******************************************************************************
|
---|
117 | BOOL HMDeviceThreadClass::SetThreadPriority(HANDLE hThread, PHMHANDLEDATA pHMHandleData, int priority)
|
---|
118 | {
|
---|
119 | TEB *teb;
|
---|
120 |
|
---|
121 | dprintf(("SetThreadPriority (%08xh,%08xh)", pHMHandleData->hHMHandle, priority));
|
---|
122 |
|
---|
123 | teb = GetTEBFromThreadHandle(hThread);
|
---|
124 | if(teb == NULL) {
|
---|
125 | dprintf(("!WARNING!: TEB not found!!"));
|
---|
126 | SetLastError(ERROR_INVALID_HANDLE);
|
---|
127 | return THREAD_PRIORITY_ERROR_RETURN;
|
---|
128 | }
|
---|
129 | DWORD ret = OSLibDosSetPriority(ODIN_TO_OS2_THREADID(teb->o.odin.threadId), priority);
|
---|
130 | if(ret == ERROR_SUCCESS) {
|
---|
131 | teb->delta_priority = priority;
|
---|
132 | return TRUE;
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | dprintf(("DosSetPriority failed with rc %d", ret));
|
---|
136 | return FALSE;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | //******************************************************************************
|
---|
140 | //TODO: Implement this??
|
---|
141 | //******************************************************************************
|
---|
142 | BOOL HMDeviceThreadClass::GetThreadContext(HANDLE hThread, PHMHANDLEDATA pHMHandleData, PCONTEXT lpContext)
|
---|
143 | {
|
---|
144 | dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n"));
|
---|
145 | memset(lpContext, 0, sizeof(CONTEXT));
|
---|
146 |
|
---|
147 | /* make up some plausible values for segment registers */
|
---|
148 | lpContext->SegCs = getCS();
|
---|
149 | lpContext->SegDs = getDS();
|
---|
150 | lpContext->SegSs = getSS();
|
---|
151 | lpContext->SegEs = getES();
|
---|
152 | lpContext->SegGs = getGS();
|
---|
153 | lpContext->SegFs = GetFS();
|
---|
154 |
|
---|
155 | return TRUE;
|
---|
156 | }
|
---|
157 | //******************************************************************************
|
---|
158 | //TODO: Implement this??
|
---|
159 | //******************************************************************************
|
---|
160 | BOOL HMDeviceThreadClass::SetThreadContext(HANDLE hThread, PHMHANDLEDATA pHMHandleData, const CONTEXT *lpContext)
|
---|
161 | {
|
---|
162 | dprintf(("SetThreadContext NOT IMPLEMENTED!!\n"));
|
---|
163 |
|
---|
164 | return FALSE;
|
---|
165 | }
|
---|
166 | //******************************************************************************
|
---|
167 | //******************************************************************************
|
---|
168 | BOOL HMDeviceThreadClass::TerminateThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData, DWORD exitcode)
|
---|
169 | {
|
---|
170 | dprintf(("TerminateThread (%08xh,%08xh)\n",
|
---|
171 | pHMHandleData->hHMHandle,
|
---|
172 | exitcode));
|
---|
173 |
|
---|
174 | pHMHandleData->dwUserData = THREAD_TERMINATED;
|
---|
175 | return O32_TerminateThread(pHMHandleData->hHMHandle, exitcode);
|
---|
176 | }
|
---|
177 | //******************************************************************************
|
---|
178 | //******************************************************************************
|
---|
179 | BOOL HMDeviceThreadClass::SetThreadTerminated(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
---|
180 | {
|
---|
181 | pHMHandleData->dwUserData = THREAD_TERMINATED;
|
---|
182 | return TRUE;
|
---|
183 | }
|
---|
184 | //******************************************************************************
|
---|
185 | //******************************************************************************
|
---|
186 | DWORD HMDeviceThreadClass::ResumeThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData)
|
---|
187 | {
|
---|
188 | dprintf(("ResumeThread (%08xh)\n",
|
---|
189 | pHMHandleData->hHMHandle));
|
---|
190 |
|
---|
191 | return O32_ResumeThread(pHMHandleData->hHMHandle);
|
---|
192 | }
|
---|
193 | //******************************************************************************
|
---|
194 | //******************************************************************************
|
---|
195 | BOOL HMDeviceThreadClass::GetExitCodeThread(HANDLE hThread, PHMHANDLEDATA pHMHandleData, LPDWORD lpExitCode)
|
---|
196 | {
|
---|
197 | dprintf(("GetExitCodeThread (%08xh,%08xh)\n",
|
---|
198 | pHMHandleData->hHMHandle,
|
---|
199 | lpExitCode));
|
---|
200 |
|
---|
201 | #if 0
|
---|
202 | if(pHMHandleData->dwUserData == THREAD_ALIVE) {
|
---|
203 | lpExitCode == STILL_ALIVE;
|
---|
204 | return TRUE;
|
---|
205 | }
|
---|
206 | #endif
|
---|
207 | return O32_GetExitCodeThread(pHMHandleData->hHMHandle, lpExitCode);
|
---|
208 | }
|
---|
209 | //******************************************************************************
|
---|
210 | //******************************************************************************
|
---|
211 | BOOL HMDeviceThreadClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
|
---|
212 | {
|
---|
213 | return TRUE;
|
---|
214 | }
|
---|
215 | //******************************************************************************
|
---|
216 | //******************************************************************************
|
---|
217 | DWORD HMDeviceThreadClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData,
|
---|
218 | DWORD dwTimeout)
|
---|
219 | {
|
---|
220 | dprintf(("HMThread::WaitForSingleObject (%08xh,%08xh)\n",
|
---|
221 | pHMHandleData->hHMHandle,
|
---|
222 | dwTimeout));
|
---|
223 |
|
---|
224 | //This doesn't work very well in Open32 (object's state never signaled)
|
---|
225 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) {
|
---|
226 | return WAIT_OBJECT_0;
|
---|
227 | }
|
---|
228 | return HMDeviceOpen32Class::WaitForSingleObject(pHMHandleData, dwTimeout);
|
---|
229 | }
|
---|
230 | //******************************************************************************
|
---|
231 | //******************************************************************************
|
---|
232 | DWORD HMDeviceThreadClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData,
|
---|
233 | DWORD dwTimeout,
|
---|
234 | BOOL fAlertable)
|
---|
235 | {
|
---|
236 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) {
|
---|
237 | return WAIT_OBJECT_0;
|
---|
238 | }
|
---|
239 | return HMDeviceOpen32Class::WaitForSingleObjectEx(pHMHandleData, dwTimeout, fAlertable);
|
---|
240 | }
|
---|
241 | //******************************************************************************
|
---|
242 | //******************************************************************************
|
---|