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

Last change on this file since 919 was 919, checked in by phaller, 26 years ago

To fix: mapping between thread priority models

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