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

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

thread, SearchPath + handlemanager fixes

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