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