| 1 | /* $Id: thread.cpp,v 1.8 1999-08-09 22:10:08 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 | #include <os2win.h>
|
|---|
| 13 | #include <stdarg.h>
|
|---|
| 14 | #include <odincrt.h>
|
|---|
| 15 | #include "thread.h"
|
|---|
| 16 | #include "except.h"
|
|---|
| 17 | #include <misc.h>
|
|---|
| 18 | #include <wprocess.h>
|
|---|
| 19 | #include <windll.h>
|
|---|
| 20 | #include <winexe.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 | ODIN_FS_BEGIN
|
|---|
| 35 | winthread = new Win32Thread(lpStartAddr, lpvThreadParm, fdwCreate);
|
|---|
| 36 | ODIN_FS_END
|
|---|
| 37 |
|
|---|
| 38 | if(winthread == 0)
|
|---|
| 39 | return(0);
|
|---|
| 40 |
|
|---|
| 41 | dprintf(("CreateThread (%08xh,%08xh,%08xh,%08xh,%08xh,%08xh)\n",
|
|---|
| 42 | lpsa,
|
|---|
| 43 | cbStack,
|
|---|
| 44 | lpStartAddr,
|
|---|
| 45 | lpvThreadParm,
|
|---|
| 46 | fdwCreate,
|
|---|
| 47 | lpIDThread));
|
|---|
| 48 |
|
|---|
| 49 | return(O32_CreateThread(lpsa, cbStack, winthread->GetOS2Callback(), (LPVOID)winthread, fdwCreate, lpIDThread));
|
|---|
| 50 | }
|
|---|
| 51 | //******************************************************************************
|
|---|
| 52 | //******************************************************************************
|
|---|
| 53 | DWORD WIN32API GetCurrentThreadId()
|
|---|
| 54 | {
|
|---|
| 55 | //// dprintf(("GetCurrentThreadId\n"));
|
|---|
| 56 | return(O32_GetCurrentThreadId());
|
|---|
| 57 | }
|
|---|
| 58 | //******************************************************************************
|
|---|
| 59 | //******************************************************************************
|
|---|
| 60 | HANDLE WIN32API GetCurrentThread()
|
|---|
| 61 | {
|
|---|
| 62 | //// dprintf(("GetCurrentThread\n"));
|
|---|
| 63 | return(O32_GetCurrentThread());
|
|---|
| 64 | }
|
|---|
| 65 | //******************************************************************************
|
|---|
| 66 | //******************************************************************************
|
|---|
| 67 | BOOL WIN32API GetExitCodeThread(HANDLE hThread, LPDWORD arg2)
|
|---|
| 68 | {
|
|---|
| 69 | dprintf(("GetExitCodeThread (%08xh,%08xh)\n",
|
|---|
| 70 | hThread,
|
|---|
| 71 | arg2));
|
|---|
| 72 |
|
|---|
| 73 | return O32_GetExitCodeThread(hThread, arg2);
|
|---|
| 74 | }
|
|---|
| 75 | //******************************************************************************
|
|---|
| 76 | //******************************************************************************
|
|---|
| 77 | BOOL WIN32API TerminateThread(HANDLE hThread, DWORD arg2)
|
|---|
| 78 | {
|
|---|
| 79 | dprintf(("TerminateThread (%08xh,%08xh)\n",
|
|---|
| 80 | hThread,
|
|---|
| 81 | arg2));
|
|---|
| 82 |
|
|---|
| 83 | return O32_TerminateThread(hThread, arg2);
|
|---|
| 84 | }
|
|---|
| 85 | //******************************************************************************
|
|---|
| 86 | //******************************************************************************
|
|---|
| 87 | DWORD WIN32API ResumeThread(HANDLE hThread)
|
|---|
| 88 | {
|
|---|
| 89 | dprintf(("ResumeThread (%08xh)\n",
|
|---|
| 90 | hThread));
|
|---|
| 91 |
|
|---|
| 92 | return O32_ResumeThread(hThread);
|
|---|
| 93 | }
|
|---|
| 94 | //******************************************************************************
|
|---|
| 95 | //******************************************************************************
|
|---|
| 96 | INT WIN32API GetThreadPriority(HANDLE hThread)
|
|---|
| 97 | {
|
|---|
| 98 | dprintf(("OS2GetThreadPriority(%08xh)\n",
|
|---|
| 99 | hThread));
|
|---|
| 100 |
|
|---|
| 101 | return O32_GetThreadPriority(hThread);
|
|---|
| 102 | }
|
|---|
| 103 | //******************************************************************************
|
|---|
| 104 | //******************************************************************************
|
|---|
| 105 | DWORD WIN32API SuspendThread(HANDLE hThread)
|
|---|
| 106 | {
|
|---|
| 107 | dprintf(("OS2SuspendThread %08xh)\n",
|
|---|
| 108 | hThread));
|
|---|
| 109 |
|
|---|
| 110 | return O32_SuspendThread(hThread);
|
|---|
| 111 | }
|
|---|
| 112 | //******************************************************************************
|
|---|
| 113 | //******************************************************************************
|
|---|
| 114 | BOOL WIN32API SetThreadPriority(HANDLE hThread, int priority)
|
|---|
| 115 | {
|
|---|
| 116 | dprintf(("OS2SetThreadPriority (%08xh,%08xh)\n",
|
|---|
| 117 | hThread,
|
|---|
| 118 | priority));
|
|---|
| 119 |
|
|---|
| 120 | return O32_SetThreadPriority(hThread, priority);
|
|---|
| 121 | }
|
|---|
| 122 | //******************************************************************************
|
|---|
| 123 | //******************************************************************************
|
|---|
| 124 | VOID WIN32API ExitThread(DWORD exitcode)
|
|---|
| 125 | {
|
|---|
| 126 | dprintf(("ExitThread (%08xu)\n",
|
|---|
| 127 | exitcode));
|
|---|
| 128 |
|
|---|
| 129 | Win32Dll::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
|---|
| 130 | Win32Dll::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
|---|
| 131 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
|---|
| 132 | DestroyTIB();
|
|---|
| 133 | O32_ExitThread(exitcode);
|
|---|
| 134 | }
|
|---|
| 135 | //******************************************************************************
|
|---|
| 136 | //******************************************************************************
|
|---|
| 137 | Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags)
|
|---|
| 138 | {
|
|---|
| 139 | lpUserData = lpData;
|
|---|
| 140 | pCallback = pUserCallback;
|
|---|
| 141 | this->dwFlags = dwFlags;
|
|---|
| 142 | }
|
|---|
| 143 | //******************************************************************************
|
|---|
| 144 | //******************************************************************************
|
|---|
| 145 | Win32Thread::~Win32Thread()
|
|---|
| 146 | {
|
|---|
| 147 |
|
|---|
| 148 | }
|
|---|
| 149 | //******************************************************************************
|
|---|
| 150 | //******************************************************************************
|
|---|
| 151 | PTHREAD_START_ROUTINE_O32 Win32Thread::GetOS2Callback()
|
|---|
| 152 | {
|
|---|
| 153 | return Win32ThreadProc;
|
|---|
| 154 | }
|
|---|
| 155 | //******************************************************************************
|
|---|
| 156 | //******************************************************************************
|
|---|
| 157 | static DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
|---|
| 158 | {
|
|---|
| 159 | Win32Thread *me = (Win32Thread *)lpData;
|
|---|
| 160 | WIN32THREADPROC winthread = me->pCallback;
|
|---|
| 161 | LPVOID userdata = me->lpUserData;
|
|---|
| 162 | DWORD rc;
|
|---|
| 163 |
|
|---|
| 164 | ODIN_delete(me) //only called once
|
|---|
| 165 |
|
|---|
| 166 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
|---|
| 167 |
|
|---|
| 168 | TEB *winteb = (TEB *)InitializeTIB();
|
|---|
| 169 | if(winteb == NULL) {
|
|---|
| 170 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
|---|
| 171 | DebugInt3();
|
|---|
| 172 | return 0;
|
|---|
| 173 | }
|
|---|
| 174 | winteb->flags = me->dwFlags;
|
|---|
| 175 |
|
|---|
| 176 | THDB *thdb = (THDB *)(winteb+1);
|
|---|
| 177 | thdb->entry_point = (void *)winthread;
|
|---|
| 178 | thdb->entry_arg = (void *)userdata;
|
|---|
| 179 |
|
|---|
| 180 | SetWin32TIB();
|
|---|
| 181 | WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
|---|
| 182 | Win32Dll::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
|---|
| 183 | Win32Dll::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
|---|
| 184 |
|
|---|
| 185 | rc = winthread(userdata);
|
|---|
| 186 |
|
|---|
| 187 | Win32Dll::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
|---|
| 188 | Win32Dll::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
|---|
| 189 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
|---|
| 190 | DestroyTIB();
|
|---|
| 191 | return rc;
|
|---|
| 192 | }
|
|---|
| 193 | //******************************************************************************
|
|---|
| 194 | //******************************************************************************
|
|---|