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