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

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

CreateThread fix

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