1 | /* $Id: thread.cpp,v 1.11 1999-08-22 11:11:10 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 | #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 |
|
---|
22 | static DWORD OPEN32API Win32ThreadProc(LPVOID lpData);
|
---|
23 |
|
---|
24 |
|
---|
25 | //******************************************************************************
|
---|
26 | //******************************************************************************
|
---|
27 | HANDLE 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 | //******************************************************************************
|
---|
51 | DWORD WIN32API GetCurrentThreadId()
|
---|
52 | {
|
---|
53 | //// dprintf(("GetCurrentThreadId\n"));
|
---|
54 | return(O32_GetCurrentThreadId());
|
---|
55 | }
|
---|
56 | //******************************************************************************
|
---|
57 | //******************************************************************************
|
---|
58 | HANDLE WIN32API GetCurrentThread()
|
---|
59 | {
|
---|
60 | //// dprintf(("GetCurrentThread\n"));
|
---|
61 | return(O32_GetCurrentThread());
|
---|
62 | }
|
---|
63 | //******************************************************************************
|
---|
64 | //******************************************************************************
|
---|
65 | BOOL 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 | //******************************************************************************
|
---|
75 | BOOL 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 | //******************************************************************************
|
---|
85 | DWORD WIN32API ResumeThread(HANDLE hThread)
|
---|
86 | {
|
---|
87 | dprintf(("ResumeThread (%08xh)\n",
|
---|
88 | hThread));
|
---|
89 |
|
---|
90 | return O32_ResumeThread(hThread);
|
---|
91 | }
|
---|
92 | //******************************************************************************
|
---|
93 | //******************************************************************************
|
---|
94 | INT WIN32API GetThreadPriority(HANDLE hThread)
|
---|
95 | {
|
---|
96 | dprintf(("OS2GetThreadPriority(%08xh)\n",
|
---|
97 | hThread));
|
---|
98 |
|
---|
99 | return O32_GetThreadPriority(hThread);
|
---|
100 | }
|
---|
101 | //******************************************************************************
|
---|
102 | //******************************************************************************
|
---|
103 | DWORD WIN32API SuspendThread(HANDLE hThread)
|
---|
104 | {
|
---|
105 | dprintf(("OS2SuspendThread %08xh)\n",
|
---|
106 | hThread));
|
---|
107 |
|
---|
108 | return O32_SuspendThread(hThread);
|
---|
109 | }
|
---|
110 | //******************************************************************************
|
---|
111 | //******************************************************************************
|
---|
112 | BOOL WIN32API SetThreadPriority(HANDLE hThread, int priority)
|
---|
113 | {
|
---|
114 | dprintf(("OS2SetThreadPriority (%08xh,%08xh)\n",
|
---|
115 | hThread,
|
---|
116 | priority));
|
---|
117 |
|
---|
118 | return O32_SetThreadPriority(hThread, priority);
|
---|
119 | }
|
---|
120 | //******************************************************************************
|
---|
121 | //******************************************************************************
|
---|
122 | VOID WIN32API ExitThread(DWORD exitcode)
|
---|
123 | {
|
---|
124 | dprintf(("ExitThread (%08xu)\n",
|
---|
125 | exitcode));
|
---|
126 |
|
---|
127 | Win32Dll::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
128 | Win32Dll::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
129 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
130 | DestroyTIB();
|
---|
131 | O32_ExitThread(exitcode);
|
---|
132 | }
|
---|
133 | //******************************************************************************
|
---|
134 | //******************************************************************************
|
---|
135 | Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags)
|
---|
136 | {
|
---|
137 | lpUserData = lpData;
|
---|
138 | pCallback = pUserCallback;
|
---|
139 | this->dwFlags = dwFlags;
|
---|
140 | }
|
---|
141 | //******************************************************************************
|
---|
142 | //******************************************************************************
|
---|
143 | Win32Thread::~Win32Thread()
|
---|
144 | {
|
---|
145 |
|
---|
146 | }
|
---|
147 | //******************************************************************************
|
---|
148 | //******************************************************************************
|
---|
149 | PTHREAD_START_ROUTINE_O32 Win32Thread::GetOS2Callback()
|
---|
150 | {
|
---|
151 | return Win32ThreadProc;
|
---|
152 | }
|
---|
153 | //******************************************************************************
|
---|
154 | //******************************************************************************
|
---|
155 | static DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
---|
156 | {
|
---|
157 | EXCEPTION_FRAME exceptFrame;
|
---|
158 | Win32Thread *me = (Win32Thread *)lpData;
|
---|
159 | WIN32THREADPROC winthread = me->pCallback;
|
---|
160 | LPVOID userdata = me->lpUserData;
|
---|
161 | DWORD rc;
|
---|
162 |
|
---|
163 | delete(me); //only called once
|
---|
164 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
---|
165 |
|
---|
166 | TEB *winteb = (TEB *)InitializeTIB();
|
---|
167 | if(winteb == NULL) {
|
---|
168 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
---|
169 | DebugInt3();
|
---|
170 | return 0;
|
---|
171 | }
|
---|
172 | winteb->flags = me->dwFlags;
|
---|
173 |
|
---|
174 | THDB *thdb = (THDB *)(winteb+1);
|
---|
175 | thdb->entry_point = (void *)winthread;
|
---|
176 | thdb->entry_arg = (void *)userdata;
|
---|
177 |
|
---|
178 | SetWin32TIB();
|
---|
179 | WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
---|
180 | Win32Dll::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
---|
181 | Win32Dll::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
---|
182 |
|
---|
183 | //Note: The Win32 exception structure references by FS:[0] is the same
|
---|
184 | // in OS/2
|
---|
185 | OS2SetExceptionHandler((void *)&exceptFrame);
|
---|
186 | rc = winthread(userdata);
|
---|
187 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
---|
188 |
|
---|
189 | Win32Dll::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
190 | Win32Dll::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
191 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
192 | DestroyTIB();
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 | //******************************************************************************
|
---|
196 | //******************************************************************************
|
---|