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