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