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