1 | /* $Id: thread.cpp,v 1.25 2000-04-18 20:06:38 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 <misc.h>
|
---|
27 | #include <wprocess.h>
|
---|
28 | #include <windllbase.h>
|
---|
29 | #include <winexebase.h>
|
---|
30 | #include "exceptutil.h"
|
---|
31 | #include "oslibmisc.h"
|
---|
32 | #include <handlemanager.h>
|
---|
33 |
|
---|
34 | #define DBG_LOCALLOG DBG_thread
|
---|
35 | #include "dbglocal.h"
|
---|
36 |
|
---|
37 | ODINDEBUGCHANNEL(KERNEL32-THREAD)
|
---|
38 |
|
---|
39 | //******************************************************************************
|
---|
40 | //******************************************************************************
|
---|
41 | DWORD WIN32API GetCurrentThreadId()
|
---|
42 | {
|
---|
43 | //// dprintf(("GetCurrentThreadId\n"));
|
---|
44 | return(O32_GetCurrentThreadId());
|
---|
45 | }
|
---|
46 | //******************************************************************************
|
---|
47 | //******************************************************************************
|
---|
48 | HANDLE WIN32API GetCurrentThread()
|
---|
49 | {
|
---|
50 | THDB *thdb;
|
---|
51 |
|
---|
52 | thdb = GetThreadTHDB();
|
---|
53 | if(thdb == 0) {
|
---|
54 | SetLastError(ERROR_INVALID_HANDLE); //todo
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 | return thdb->hThread;
|
---|
58 | }
|
---|
59 | //******************************************************************************
|
---|
60 | //******************************************************************************
|
---|
61 | VOID WIN32API ExitThread(DWORD exitcode)
|
---|
62 | {
|
---|
63 | EXCEPTION_FRAME *exceptFrame;
|
---|
64 | THDB *thdb;
|
---|
65 |
|
---|
66 | dprintf(("ExitThread %x (%x)", GetCurrentThread(), exitcode));
|
---|
67 |
|
---|
68 | thdb = GetThreadTHDB();
|
---|
69 | if(thdb != 0) {
|
---|
70 | exceptFrame = (EXCEPTION_FRAME *)thdb->exceptFrame;
|
---|
71 | }
|
---|
72 | else DebugInt3();
|
---|
73 |
|
---|
74 | HMSetThreadTerminated(GetCurrentThread());
|
---|
75 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
76 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
77 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
78 | DestroyTIB();
|
---|
79 |
|
---|
80 | if(exceptFrame) OS2UnsetExceptionHandler((void *)exceptFrame);
|
---|
81 |
|
---|
82 | O32_ExitThread(exitcode);
|
---|
83 | }
|
---|
84 | //******************************************************************************
|
---|
85 | //******************************************************************************
|
---|
86 | Win32Thread::Win32Thread(LPTHREAD_START_ROUTINE pUserCallback, LPVOID lpData, DWORD dwFlags, HANDLE hThread)
|
---|
87 | {
|
---|
88 | lpUserData = lpData;
|
---|
89 | pCallback = pUserCallback;
|
---|
90 | this->dwFlags = dwFlags;
|
---|
91 | this->hThread = hThread;
|
---|
92 | }
|
---|
93 | //******************************************************************************
|
---|
94 | //******************************************************************************
|
---|
95 | DWORD OPEN32API Win32ThreadProc(LPVOID lpData)
|
---|
96 | {
|
---|
97 | EXCEPTION_FRAME exceptFrame;
|
---|
98 | Win32Thread *me = (Win32Thread *)lpData;
|
---|
99 | WIN32THREADPROC winthread = me->pCallback;
|
---|
100 | LPVOID userdata = me->lpUserData;
|
---|
101 | HANDLE hThread = me->hThread;
|
---|
102 | DWORD rc;
|
---|
103 |
|
---|
104 | delete(me); //only called once
|
---|
105 | dprintf(("Win32ThreadProc %d\n", GetCurrentThreadId()));
|
---|
106 |
|
---|
107 | TEB *winteb = (TEB *)InitializeTIB();
|
---|
108 | if(winteb == NULL) {
|
---|
109 | dprintf(("Win32ThreadProc: InitializeTIB failed!!"));
|
---|
110 | DebugInt3();
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 | winteb->flags = me->dwFlags;
|
---|
114 |
|
---|
115 | THDB *thdb = (THDB *)(winteb+1);
|
---|
116 | thdb->entry_point = (void *)winthread;
|
---|
117 | thdb->entry_arg = (void *)userdata;
|
---|
118 | thdb->hThread = hThread;
|
---|
119 |
|
---|
120 | thdb->hab = OSLibWinInitialize();
|
---|
121 | thdb->hmq = OSLibWinQueryMsgQueue(thdb->hab);
|
---|
122 | dprintf(("Win32ThreadProc: hab %x hmq %x", thdb->hab, thdb->hmq));
|
---|
123 |
|
---|
124 | //Note: The Win32 exception structure referenced by FS:[0] is the same
|
---|
125 | // in OS/2
|
---|
126 | OS2SetExceptionHandler((void *)&exceptFrame);
|
---|
127 | thdb->exceptFrame = (ULONG)&exceptFrame;
|
---|
128 |
|
---|
129 | SetWin32TIB();
|
---|
130 | WinExe->tlsAttachThread(); //setup TLS structure of main exe
|
---|
131 | Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
|
---|
132 | Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
|
---|
133 |
|
---|
134 | //Set default FPU control word (no exceptions); same as in NT
|
---|
135 | _control87(0x27F, 0xFFF);
|
---|
136 | rc = winthread(userdata);
|
---|
137 |
|
---|
138 | HMSetThreadTerminated(GetCurrentThread());
|
---|
139 | thdb->exceptFrame = 0;
|
---|
140 | Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
|
---|
141 | Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
|
---|
142 | WinExe->tlsDetachThread(); //destroy TLS structure of main exe
|
---|
143 | DestroyTIB();
|
---|
144 | OS2UnsetExceptionHandler((void *)&exceptFrame);
|
---|
145 |
|
---|
146 | return rc;
|
---|
147 | }
|
---|
148 | //******************************************************************************
|
---|
149 | //******************************************************************************
|
---|