1 | /* $Id: hmthread.cpp,v 1.6 2001-03-19 19:27:13 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 | 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 | return pHMHandleData->hHMHandle;
|
---|
81 | }
|
---|
82 | //******************************************************************************
|
---|
83 | //******************************************************************************
|
---|
84 | INT HMDeviceThreadClass::GetThreadPriority(PHMHANDLEDATA pHMHandleData)
|
---|
85 | {
|
---|
86 | dprintf(("GetThreadPriority(%08xh)\n", pHMHandleData->hHMHandle));
|
---|
87 |
|
---|
88 | return O32_GetThreadPriority(pHMHandleData->hHMHandle);
|
---|
89 | }
|
---|
90 | //******************************************************************************
|
---|
91 | //******************************************************************************
|
---|
92 | DWORD HMDeviceThreadClass::SuspendThread(PHMHANDLEDATA pHMHandleData)
|
---|
93 | {
|
---|
94 | dprintf(("SuspendThread %08xh)\n", pHMHandleData->hHMHandle));
|
---|
95 |
|
---|
96 | return O32_SuspendThread(pHMHandleData->hHMHandle);
|
---|
97 | }
|
---|
98 | //******************************************************************************
|
---|
99 | //******************************************************************************
|
---|
100 | BOOL HMDeviceThreadClass::SetThreadPriority(PHMHANDLEDATA pHMHandleData, int priority)
|
---|
101 | {
|
---|
102 | dprintf(("SetThreadPriority (%08xh,%08xh)\n",
|
---|
103 | pHMHandleData->hHMHandle,
|
---|
104 | priority));
|
---|
105 |
|
---|
106 | return O32_SetThreadPriority(pHMHandleData->hHMHandle, priority);
|
---|
107 | }
|
---|
108 | //******************************************************************************
|
---|
109 | //TODO: Implement this??
|
---|
110 | //******************************************************************************
|
---|
111 | BOOL HMDeviceThreadClass::GetThreadContext(PHMHANDLEDATA pHMHandleData, PCONTEXT lpContext)
|
---|
112 | {
|
---|
113 | dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n"));
|
---|
114 | memset(lpContext, 0, sizeof(CONTEXT));
|
---|
115 |
|
---|
116 | /* make up some plausible values for segment registers */
|
---|
117 | lpContext->SegCs = getCS();
|
---|
118 | lpContext->SegDs = getDS();
|
---|
119 | lpContext->SegSs = getSS();
|
---|
120 | lpContext->SegEs = getES();
|
---|
121 | lpContext->SegGs = getGS();
|
---|
122 | lpContext->SegFs = GetFS();
|
---|
123 |
|
---|
124 | return TRUE;
|
---|
125 | }
|
---|
126 | //******************************************************************************
|
---|
127 | //TODO: Implement this??
|
---|
128 | //******************************************************************************
|
---|
129 | BOOL HMDeviceThreadClass::SetThreadContext(PHMHANDLEDATA pHMHandleData, const CONTEXT *lpContext)
|
---|
130 | {
|
---|
131 | dprintf(("SetThreadContext NOT IMPLEMENTED!!\n"));
|
---|
132 |
|
---|
133 | return FALSE;
|
---|
134 | }
|
---|
135 | //******************************************************************************
|
---|
136 | //******************************************************************************
|
---|
137 | BOOL HMDeviceThreadClass::TerminateThread(PHMHANDLEDATA pHMHandleData, DWORD exitcode)
|
---|
138 | {
|
---|
139 | dprintf(("TerminateThread (%08xh,%08xh)\n",
|
---|
140 | pHMHandleData->hHMHandle,
|
---|
141 | exitcode));
|
---|
142 |
|
---|
143 | pHMHandleData->dwUserData = THREAD_TERMINATED;
|
---|
144 | return O32_TerminateThread(pHMHandleData->hHMHandle, exitcode);
|
---|
145 | }
|
---|
146 | //******************************************************************************
|
---|
147 | //******************************************************************************
|
---|
148 | BOOL HMDeviceThreadClass::SetThreadTerminated(PHMHANDLEDATA pHMHandleData)
|
---|
149 | {
|
---|
150 | pHMHandleData->dwUserData = THREAD_TERMINATED;
|
---|
151 | return TRUE;
|
---|
152 | }
|
---|
153 | //******************************************************************************
|
---|
154 | //******************************************************************************
|
---|
155 | DWORD HMDeviceThreadClass::ResumeThread(PHMHANDLEDATA pHMHandleData)
|
---|
156 | {
|
---|
157 | dprintf(("ResumeThread (%08xh)\n",
|
---|
158 | pHMHandleData->hHMHandle));
|
---|
159 |
|
---|
160 | return O32_ResumeThread(pHMHandleData->hHMHandle);
|
---|
161 | }
|
---|
162 | //******************************************************************************
|
---|
163 | //******************************************************************************
|
---|
164 | BOOL HMDeviceThreadClass::GetExitCodeThread(PHMHANDLEDATA pHMHandleData, LPDWORD lpExitCode)
|
---|
165 | {
|
---|
166 | dprintf(("GetExitCodeThread (%08xh,%08xh)\n",
|
---|
167 | pHMHandleData->hHMHandle,
|
---|
168 | lpExitCode));
|
---|
169 |
|
---|
170 | #if 0
|
---|
171 | if(pHMHandleData->dwUserData == THREAD_ALIVE) {
|
---|
172 | lpExitCode == STILL_ALIVE;
|
---|
173 | return TRUE;
|
---|
174 | }
|
---|
175 | #endif
|
---|
176 | return O32_GetExitCodeThread(pHMHandleData->hHMHandle, lpExitCode);
|
---|
177 | }
|
---|
178 | //******************************************************************************
|
---|
179 | //******************************************************************************
|
---|
180 | DWORD HMDeviceThreadClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
|
---|
181 | {
|
---|
182 | return STATUS_SUCCESS;
|
---|
183 | }
|
---|
184 | //******************************************************************************
|
---|
185 | //******************************************************************************
|
---|
186 | DWORD HMDeviceThreadClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData,
|
---|
187 | DWORD dwTimeout)
|
---|
188 | {
|
---|
189 | //This doesn't work very well in Open32 (object's state never signaled)
|
---|
190 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) {
|
---|
191 | return WAIT_OBJECT_0;
|
---|
192 | }
|
---|
193 | return HMDeviceOpen32Class::WaitForSingleObject(pHMHandleData, dwTimeout);
|
---|
194 | }
|
---|
195 | //******************************************************************************
|
---|
196 | //******************************************************************************
|
---|
197 | DWORD HMDeviceThreadClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData,
|
---|
198 | DWORD dwTimeout,
|
---|
199 | BOOL fAlertable)
|
---|
200 | {
|
---|
201 | if(pHMHandleData->dwUserData == THREAD_TERMINATED) {
|
---|
202 | return WAIT_OBJECT_0;
|
---|
203 | }
|
---|
204 | return HMDeviceOpen32Class::WaitForSingleObjectEx(pHMHandleData, dwTimeout, fAlertable);
|
---|
205 | }
|
---|
206 | //******************************************************************************
|
---|
207 | //******************************************************************************
|
---|