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