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