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