source: trunk/src/kernel32/hmthread.cpp@ 3140

Last change on this file since 3140 was 3140, checked in by sandervl, 25 years ago

fix for handle of thread 0

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