source: trunk/src/kernel32/thread.cpp@ 4658

Last change on this file since 4658 was 4658, checked in by sandervl, 25 years ago

Updates for TEB changes

File size: 4.8 KB
Line 
1/* $Id: thread.cpp,v 1.26 2000-11-21 11:35:09 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
37ODINDEBUGCHANNEL(KERNEL32-THREAD)
38
39//******************************************************************************
40//******************************************************************************
41DWORD WIN32API GetCurrentThreadId()
42{
43//// dprintf(("GetCurrentThreadId\n"));
44 return(O32_GetCurrentThreadId());
45}
46//******************************************************************************
47//******************************************************************************
48HANDLE WIN32API GetCurrentThread()
49{
50 TEB *teb;
51
52 teb = GetThreadTEB();
53 if(teb == 0) {
54 SetLastError(ERROR_INVALID_HANDLE); //todo
55 return 0;
56 }
57 return teb->o.odin.hThread;
58}
59//******************************************************************************
60//******************************************************************************
61VOID WIN32API ExitThread(DWORD exitcode)
62{
63 EXCEPTION_FRAME *exceptFrame;
64 TEB *teb;
65
66 dprintf(("ExitThread %x (%x)", GetCurrentThread(), exitcode));
67
68 teb = GetThreadTEB();
69 if(teb != 0) {
70 exceptFrame = (EXCEPTION_FRAME *)teb->o.odin.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//******************************************************************************
86Win32Thread::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//******************************************************************************
95DWORD 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 winteb->entry_point = (void *)winthread;
116 winteb->entry_arg = (void *)userdata;
117 winteb->o.odin.hThread = hThread;
118
119 winteb->o.odin.hab = OSLibWinInitialize();
120 winteb->o.odin.hmq = OSLibWinQueryMsgQueue(winteb->o.odin.hab);
121 dprintf(("Win32ThreadProc: hab %x hmq %x", winteb->o.odin.hab, winteb->o.odin.hmq));
122
123 //Note: The Win32 exception structure referenced by FS:[0] is the same
124 // in OS/2
125 OS2SetExceptionHandler((void *)&exceptFrame);
126 winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
127
128 SetWin32TIB();
129 WinExe->tlsAttachThread(); //setup TLS structure of main exe
130 Win32DllBase::tlsAttachThreadToAllDlls(); //setup TLS structures of all dlls
131 Win32DllBase::attachThreadToAllDlls(); //send DLL_THREAD_ATTACH message to all dlls
132
133 //Set default FPU control word (no exceptions); same as in NT
134 _control87(0x27F, 0xFFF);
135 rc = winthread(userdata);
136
137 HMSetThreadTerminated(GetCurrentThread());
138 winteb->o.odin.exceptFrame = 0;
139 Win32DllBase::detachThreadFromAllDlls(); //send DLL_THREAD_DETACH message to all dlls
140 Win32DllBase::tlsDetachThreadFromAllDlls(); //destroy TLS structures of all dlls
141 WinExe->tlsDetachThread(); //destroy TLS structure of main exe
142 DestroyTIB();
143 OS2UnsetExceptionHandler((void *)&exceptFrame);
144
145 return rc;
146}
147//******************************************************************************
148//******************************************************************************
Note: See TracBrowser for help on using the repository browser.