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

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

thread fixes + heap wrappers

File size: 5.3 KB
Line 
1/* $Id: wintls.cpp,v 1.6 1999-11-09 19:22:33 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 TEB *winteb;
57 char *tibmem;
58
59 if(!tlsAddress)
60 return;
61
62 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
63 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
64 DebugInt3();
65 return;
66 }
67
68 dprintf(("Win32ImageBase::tlsAttachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
69 dprintf(("tlsAddress: %x", tlsAddress));
70 dprintf(("tlsInitSize: %x", tlsInitSize));
71 dprintf(("tlsTotalSize %x", tlsTotalSize));
72 dprintf(("tlsIndexAddr %x", tlsIndexAddr));
73 dprintf(("tlsCallbackAddr %x", tlsCallBackAddr));
74 dprintf(("*tlsCallbackAddr %x", *tlsCallBackAddr));
75 tibmem = (char *)VirtualAlloc(0, tlsTotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
76 if(tibmem == NULL) {
77 dprintf(("tlsAttachThread: tibmem == NULL!!!!"));
78 DebugInt3();
79 return;
80 }
81 memset(tibmem, 0, tlsTotalSize);
82 memcpy(tibmem, tlsAddress, tlsInitSize);
83
84 winteb = (TEB *)*TIBFlatPtr;
85 winteb->tls_ptr[tlsIndex] = tibmem;
86 *tlsIndexAddr = tlsIndex;
87
88 if((ULONG)*tlsCallBackAddr != 0) {
89 pCallback = tlsCallBackAddr;
90 while(*pCallback) {
91 dprintf(("tlsAttachThread: calling TLS Callback %x", *pCallback));
92
93 (*pCallback)((LPVOID)hinstance, DLL_THREAD_ATTACH, 0);
94
95 dprintf(("tlsAttachThread: finished calling TLS Callback %x", *pCallback));
96 *pCallback++;
97 }
98 }
99 return;
100}
101//******************************************************************************
102//******************************************************************************
103void Win32ImageBase::tlsDetachThread() //destroy TLS structures
104{
105 EXCEPTION_FRAME exceptFrame;
106 PIMAGE_TLS_CALLBACK *pCallback;
107 TEB *winteb;
108
109 if(!tlsAddress)
110 return;
111
112 dprintf(("Win32ImageBase::tlsDetachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
113
114 if((ULONG)*tlsCallBackAddr != 0) {
115 pCallback = tlsCallBackAddr;
116 while(*pCallback) {
117 dprintf(("tlsDetachThread: calling TLS Callback %x", *pCallback));
118
119 (*pCallback)((LPVOID)hinstance, DLL_THREAD_DETACH, 0);
120
121 dprintf(("tlsDetachThread: finished calling TLS Callback %x", *pCallback));
122 *pCallback++;
123 }
124 }
125 winteb = (TEB *)*TIBFlatPtr;
126 VirtualFree(winteb->tls_ptr[tlsIndex], tlsTotalSize, MEM_RELEASE);
127 winteb->tls_ptr[tlsIndex] = 0;
128}
129//******************************************************************************
130//******************************************************************************
131
132//******************************************************************************
133//******************************************************************************
134DWORD WIN32API TlsAlloc()
135{
136 dprintf(("KERNEL32: TlsAlloc\n"));
137 return(O32_TlsAlloc());
138}
139//******************************************************************************
140//******************************************************************************
141BOOL WIN32API TlsFree(DWORD index)
142{
143 dprintf(("KERNEL32: TlsFree\n"));
144 return(O32_TlsFree(index));
145}
146//******************************************************************************
147//******************************************************************************
148LPVOID WIN32API TlsGetValue(DWORD index)
149{
150 LPVOID rc;
151
152 rc = O32_TlsGetValue(index);
153// dprintf(("KERNEL32: TlsGetValue %d returned %X\n", index, rc));
154 return(rc);
155}
156//******************************************************************************
157//******************************************************************************
158BOOL WIN32API TlsSetValue(DWORD index, LPVOID val)
159{
160// dprintf(("KERNEL32: TlsSetValue\n"));
161 return(O32_TlsSetValue(index, val));
162}
163//******************************************************************************
164//******************************************************************************
Note: See TracBrowser for help on using the repository browser.