source: trunk/src/kernel32/thread.cpp@ 1898

Last change on this file since 1898 was 1898, checked in by sandervl, 26 years ago

compilation fixes

File size: 8.2 KB
Line 
1/* $Id: thread.cpp,v 1.18 1999-11-30 20:19:34 sandervl Exp $ */
2
3/*
4 * Win32 Thread API functions
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 */
12
13/*****************************************************************************
14 * Includes *
15 *****************************************************************************/
16
17#include <odin.h>
18#include <odinwrap.h>
19#include <os2sel.h>
20
21#include <os2win.h>
22#include <stdarg.h>
23#include <string.h>
24#include "thread.h"
25#include "exceptutil.h"
26#include <misc.h>
27#include <wprocess.h>
28#include <windllbase.h>
29#include <winexebase.h>
30
31ODINDEBUGCHANNEL(KERNEL32-THREAD)
32
33
34static DWORD OPEN32API Win32ThreadProc(LPVOID lpData);
35
36
37//******************************************************************************
38//******************************************************************************
39ODINFUNCTION6(HANDLE,CreateThread,LPSECURITY_ATTRIBUTES, lpsa,
40 DWORD, cbStack,
41 LPTHREAD_START_ROUTINE, lpStartAddr,
42 LPVOID, lpvThreadParm,
43 DWORD, fdwCreate,
44 LPDWORD, lpIDThread)
45{
46 Win32Thread *winthread;
47
48 winthread = new Win32Thread(lpStartAddr, lpvThreadParm, fdwCreate);
49
50 if(winthread == 0)
51 return(0);
52
53#ifdef DEBUG
54 // @@@PH Note: with debug code enabled, ODIN might request more stack space!
55 if (cbStack > 0)
56 cbStack <<= 1; // double stack
57 else
58 cbStack = 1048576; // per default 1MB stack per thread
59#endif
60
61 return(O32_CreateThread(lpsa,
62 cbStack,
63 winthread->GetOS2Callback(),
64 (LPVOID)winthread,
65 fdwCreate,
66 lpIDThread));
67}
68//******************************************************************************
69//******************************************************************************
70DWORD WIN32API GetCurrentThreadId()
71{
72//// dprintf(("GetCurrentThreadId\n"));
73 return(O32_GetCurrentThreadId());
74}
75//******************************************************************************
76//******************************************************************************
77HANDLE WIN32API GetCurrentThread()
78{
79//// dprintf(("GetCurrentThread\n"));
80 return(O32_GetCurrentThread());
81}
82//******************************************************************************
83//******************************************************************************
84BOOL WIN32API GetExitCodeThread(HANDLE hThread, LPDWORD arg2)
85{
86 dprintf(("GetExitCodeThread (%08xh,%08xh)\n",
87 hThread,
88 arg2));
89
90 return O32_GetExitCodeThread(hThread, arg2);
91}
92//******************************************************************************
93//******************************************************************************
94BOOL WIN32API TerminateThread(HANDLE hThread, DWORD arg2)
95{
96 dprintf(("TerminateThread (%08xh,%08xh)\n",
97 hThread,
98 arg2));
99
100 return O32_TerminateThread(hThread, arg2);
101}
102//******************************************************************************
103//******************************************************************************
104DWORD WIN32API ResumeThread(HANDLE hThread)
105{
106 dprintf(("ResumeThread (%08xh)\n",
107 hThread));
108
109 return O32_ResumeThread(hThread);
110}
111//******************************************************************************
112//******************************************************************************
113INT WIN32API GetThreadPriority(HANDLE hThread)
114{
115 dprintf(("OS2GetThreadPriority(%08xh)\n",
116 hThread));
117
118 return O32_GetThreadPriority(hThread);
119}
120//******************************************************************************
121//******************************************************************************
122DWORD WIN32API SuspendThread(HANDLE hThread)
123{
124 dprintf(("OS2SuspendThread %08xh)\n",
125 hThread));
126
127 return O32_SuspendThread(hThread);
128}
129//******************************************************************************
130//******************************************************************************
131BOOL WIN32API SetThreadPriority(HANDLE hThread, int priority)
132{
133 dprintf(("OS2SetThreadPriority (%08xh,%08xh)\n",
134 hThread,
135 priority));
136
137 return O32_SetThreadPriority(hThread, priority);
138}
139//******************************************************************************
140//TODO: Implement this??
141//******************************************************************************
142BOOL WIN32API GetThreadContext(HANDLE hThread, PCONTEXT lpContext)
143{
144 dprintf(("GetThreadContext NOT IMPLEMENTED!! (TRUE)\n"));
145 memset(lpContext, 0, sizeof(CONTEXT));
146 return TRUE;
147}
148//******************************************************************************
149//TODO: Implement this??
150//******************************************************************************
151BOOL WIN32API SetThreadContext(HANDLE hThread, const CONTEXT *lpContext)
152{
153 dprintf(("SetThreadContext NOT IMPLEMENTED!!\n"));
154
155 return FALSE;
156}
157//******************************************************************************
158//******************************************************************************
159VOID WIN32API ExitThread(DWORD exitcode)
160{
161 dprintf(("ExitThread (%08xu)\n",
162 exitcode));
163
164 Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
165 Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
166 WinExe->tlsDetachThread(); //destroy TLS structure of main exe
167 DestroyTIB();
168 O32_ExitThread(exitcode);
169}
170//******************************************************************************
171//******************************************************************************
172Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags)
173{
174 lpUserData = lpData;
175 pCallback = pUserCallback;
176 this->dwFlags = dwFlags;
177}
178//******************************************************************************
179//******************************************************************************
180Win32Thread::~Win32Thread()
181{
182
183}
184//******************************************************************************
185//******************************************************************************
186PTHREAD_START_ROUTINE_O32 Win32Thread::GetOS2Callback()
187{
188 return Win32ThreadProc;
189}
190//******************************************************************************
191//******************************************************************************
192static DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
193{
194 EXCEPTION_FRAME exceptFrame;
195 Win32Thread *me = (Win32Thread *)lpData;
196 WIN32THREADPROC winthread = me->pCallback;
197 LPVOID userdata = me->lpUserData;
198 DWORD rc;
199
200 delete(me); //only called once
201 dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
202
203 TEB *winteb = (TEB *)InitializeTIB();
204 if(winteb == NULL) {
205 dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
206 DebugInt3();
207 return 0;
208 }
209 winteb->flags = me->dwFlags;
210
211 THDB *thdb = (THDB *)(winteb+1);
212 thdb->entry_point = (void *)winthread;
213 thdb->entry_arg = (void *)userdata;
214
215 //Note: The Win32 exception structure referenced by FS:[0] is the same
216 // in OS/2
217 OS2SetExceptionHandler((void *)&exceptFrame);
218
219 SetWin32TIB();
220 WinExe->tlsAttachThread(); //setup TLS structure of main exe
221 Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
222 Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
223
224 rc = winthread(userdata);
225
226 Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
227 Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
228 WinExe->tlsDetachThread(); //destroy TLS structure of main exe
229 DestroyTIB();
230 OS2UnsetExceptionHandler((void *)&exceptFrame);
231
232 return rc;
233}
234//******************************************************************************
235//******************************************************************************
Note: See TracBrowser for help on using the repository browser.