source: trunk/src/kernel32/wintls.cpp@ 1036

Last change on this file since 1036 was 956, checked in by sandervl, 26 years ago

Rewrite for new win32 image classes

File size: 5.6 KB
Line 
1/* $Id: wintls.cpp,v 1.5 1999-09-15 23:38:02 sandervl Exp $ */
2/*
3 * Win32 TLS API functions
4 *
5 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
6 *
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 *
10 */
11#include <os2win.h>
12#include <string.h>
13#include <winimagebase.h>
14#include <thread.h>
15#include <wprocess.h>
16#include <except.h>
17#include "exceptutil.h"
18
19//******************************************************************************
20//******************************************************************************
21void Win32ImageBase::tlsAlloc() //Allocate TLS index for this module
22{
23 if(!tlsAddress)
24 return;
25
26 tlsIndex = TlsAlloc();
27 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
28 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
29 DebugInt3();
30 return;
31 }
32 dprintf(("Win32ImageBase::tlsAlloc (%d) for module %x", tlsIndex, hinstance));
33}
34//******************************************************************************
35//******************************************************************************
36void Win32ImageBase::tlsDelete() //Free TLS index for this module
37{
38 if(!tlsAddress)
39 return;
40
41 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
42 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
43 DebugInt3();
44 return;
45 }
46 dprintf(("Win32ImageBase::tlsDestroy (%d) for module %x", tlsIndex, hinstance));
47 TlsFree(tlsIndex);
48 tlsIndex = -1;
49}
50//******************************************************************************
51//******************************************************************************
52void Win32ImageBase::tlsAttachThread() //setup TLS structures for new thread
53{
54 EXCEPTION_FRAME exceptFrame;
55 PIMAGE_TLS_CALLBACK *pCallback;
56 USHORT sel;
57 TEB *winteb;
58 char *tibmem;
59
60 if(!tlsAddress)
61 return;
62
63 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
64 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
65 DebugInt3();
66 return;
67 }
68
69 dprintf(("Win32ImageBase::tlsAttachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
70 dprintf(("tlsAddress: %x", tlsAddress));
71 dprintf(("tlsInitSize: %x", tlsInitSize));
72 dprintf(("tlsTotalSize %x", tlsTotalSize));
73 dprintf(("tlsIndexAddr %x", tlsIndexAddr));
74 dprintf(("tlsCallbackAddr %x", tlsCallBackAddr));
75 dprintf(("*tlsCallbackAddr %x", *tlsCallBackAddr));
76 tibmem = (char *)VirtualAlloc(0, tlsTotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
77 if(tibmem == NULL) {
78 dprintf(("tlsAttachThread: tibmem == NULL!!!!"));
79 DebugInt3();
80 return;
81 }
82 memset(tibmem, 0, tlsTotalSize);
83 memcpy(tibmem, tlsAddress, tlsInitSize);
84
85 winteb = (TEB *)*TIBFlatPtr;
86 winteb->tls_ptr[tlsIndex] = tibmem;
87 *tlsIndexAddr = tlsIndex;
88
89 if((ULONG)*tlsCallBackAddr != 0) {
90 pCallback = tlsCallBackAddr;
91 while(*pCallback) {
92 dprintf(("tlsAttachThread: calling TLS Callback %x", *pCallback));
93
94 OS2SetExceptionHandler((void *)&exceptFrame);
95 sel = SetWin32TIB();
96 (*pCallback)((LPVOID)hinstance, DLL_THREAD_ATTACH, 0);
97 SetFS(sel);
98 OS2UnsetExceptionHandler((void *)&exceptFrame);
99
100 dprintf(("tlsAttachThread: finished calling TLS Callback %x", *pCallback));
101 *pCallback++;
102 }
103 }
104 return;
105}
106//******************************************************************************
107//******************************************************************************
108void Win32ImageBase::tlsDetachThread() //destroy TLS structures
109{
110 EXCEPTION_FRAME exceptFrame;
111 PIMAGE_TLS_CALLBACK *pCallback;
112 USHORT sel;
113 TEB *winteb;
114
115 if(!tlsAddress)
116 return;
117
118 dprintf(("Win32ImageBase::tlsDetachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
119
120 if((ULONG)*tlsCallBackAddr != 0) {
121 pCallback = tlsCallBackAddr;
122 while(*pCallback) {
123 dprintf(("tlsDetachThread: calling TLS Callback %x", *pCallback));
124
125 OS2SetExceptionHandler((void *)&exceptFrame);
126 sel = SetWin32TIB();
127 (*pCallback)((LPVOID)hinstance, DLL_THREAD_DETACH, 0);
128 SetFS(sel);
129 OS2UnsetExceptionHandler((void *)&exceptFrame);
130
131 dprintf(("tlsDetachThread: finished calling TLS Callback %x", *pCallback));
132 *pCallback++;
133 }
134 }
135 winteb = (TEB *)*TIBFlatPtr;
136 VirtualFree(winteb->tls_ptr[tlsIndex], tlsTotalSize, MEM_RELEASE);
137 winteb->tls_ptr[tlsIndex] = 0;
138}
139//******************************************************************************
140//******************************************************************************
141
142//******************************************************************************
143//******************************************************************************
144DWORD WIN32API TlsAlloc()
145{
146 dprintf(("KERNEL32: TlsAlloc\n"));
147 return(O32_TlsAlloc());
148}
149//******************************************************************************
150//******************************************************************************
151BOOL WIN32API TlsFree(DWORD index)
152{
153 dprintf(("KERNEL32: TlsFree\n"));
154 return(O32_TlsFree(index));
155}
156//******************************************************************************
157//******************************************************************************
158LPVOID WIN32API TlsGetValue(DWORD index)
159{
160 LPVOID rc;
161
162 rc = O32_TlsGetValue(index);
163// dprintf(("KERNEL32: TlsGetValue %d returned %X\n", index, rc));
164 return(rc);
165}
166//******************************************************************************
167//******************************************************************************
168BOOL WIN32API TlsSetValue(DWORD index, LPVOID val)
169{
170// dprintf(("KERNEL32: TlsSetValue\n"));
171 return(O32_TlsSetValue(index, val));
172}
173//******************************************************************************
174//******************************************************************************
Note: See TracBrowser for help on using the repository browser.