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

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

Thread bugfix

File size: 5.2 KB
Line 
1/* $Id: wintls.cpp,v 1.3 1999-07-09 15:58:54 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 <winimage.h>
13#include <thread.h>
14#include <wprocess.h>
15
16//******************************************************************************
17//******************************************************************************
18void Win32Image::tlsAlloc() //Allocate TLS index for this module
19{
20 if(!tlsAddress)
21 return;
22
23 tlsIndex = TlsAlloc();
24 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
25 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
26 DebugInt3();
27 return;
28 }
29 dprintf(("Win32Image::tlsAlloc (%d) for module %x", tlsIndex, hinstance));
30}
31//******************************************************************************
32//******************************************************************************
33void Win32Image::tlsDelete() //Free TLS index for this module
34{
35 if(!tlsAddress)
36 return;
37
38 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
39 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
40 DebugInt3();
41 return;
42 }
43 dprintf(("Win32Image::tlsDestroy (%d) for module %x", tlsIndex, hinstance));
44 TlsFree(tlsIndex);
45 tlsIndex = -1;
46}
47//******************************************************************************
48//******************************************************************************
49void Win32Image::tlsAttachThread() //setup TLS structures for new thread
50{
51 PIMAGE_TLS_CALLBACK *pCallback;
52 TEB *winteb;
53 char *tibmem;
54
55 if(!tlsAddress)
56 return;
57
58 if(tlsIndex >= TLS_MINIMUM_AVAILABLE) {
59 dprintf(("tlsAttachThread: invalid tlsIndex %x!!!!", tlsIndex));
60 DebugInt3();
61 return;
62 }
63
64 dprintf(("Win32Image::tlsAttachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
65 dprintf(("tlsAddress: %x", tlsAddress));
66 dprintf(("tlsInitSize: %x", tlsInitSize));
67 dprintf(("tlsTotalSize %x", tlsTotalSize));
68 dprintf(("tlsIndexAddr %x", tlsIndexAddr));
69 dprintf(("tlsCallbackAddr %x", tlsCallBackAddr));
70 dprintf(("*tlsCallbackAddr %x", *tlsCallBackAddr));
71 tibmem = (char *)VirtualAlloc(0, tlsTotalSize, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
72 if(tibmem == NULL) {
73 dprintf(("tlsAttachThread: tibmem == NULL!!!!"));
74 DebugInt3();
75 return;
76 }
77 memset(tibmem, 0, tlsTotalSize);
78 memcpy(tibmem, tlsAddress, tlsInitSize);
79
80 winteb = (TEB *)*TIBFlatPtr;
81 winteb->tls_ptr[tlsIndex] = tibmem;
82 *tlsIndexAddr = tlsIndex;
83
84 if((ULONG)*tlsCallBackAddr != 0) {
85 pCallback = tlsCallBackAddr;
86 while(*pCallback) {
87 dprintf(("tlsAttachThread: calling TLS Callback %x", *pCallback));
88 (*pCallback)((LPVOID)hinstance, DLL_THREAD_ATTACH, 0);
89 dprintf(("tlsAttachThread: finished calling TLS Callback %x", *pCallback));
90 *pCallback++;
91 }
92 }
93 return;
94}
95//******************************************************************************
96//******************************************************************************
97void Win32Image::tlsDetachThread() //destroy TLS structures
98{
99 PIMAGE_TLS_CALLBACK *pCallback;
100 TEB *winteb;
101
102 if(!tlsAddress)
103 return;
104
105 dprintf(("Win32Image::tlsDetachThread for module %x, thread id %x", hinstance, GetCurrentThreadId()));
106
107 if((ULONG)*tlsCallBackAddr != 0) {
108 pCallback = tlsCallBackAddr;
109 while(*pCallback) {
110 dprintf(("tlsDetachThread: calling TLS Callback %x", *pCallback));
111 (*pCallback)((LPVOID)hinstance, DLL_THREAD_DETACH, 0);
112 dprintf(("tlsDetachThread: finished calling TLS Callback %x", *pCallback));
113 *pCallback++;
114 }
115 }
116 winteb = (TEB *)*TIBFlatPtr;
117 VirtualFree(winteb->tls_ptr[tlsIndex], tlsTotalSize, MEM_RELEASE);
118 winteb->tls_ptr[tlsIndex] = 0;
119}
120//******************************************************************************
121//******************************************************************************
122
123//******************************************************************************
124//******************************************************************************
125DWORD WIN32API TlsAlloc()
126{
127 dprintf(("KERNEL32: TlsAlloc\n"));
128 return(O32_TlsAlloc());
129}
130//******************************************************************************
131//******************************************************************************
132BOOL WIN32API TlsFree(DWORD index)
133{
134 dprintf(("KERNEL32: TlsFree\n"));
135 return(O32_TlsFree(index));
136}
137//******************************************************************************
138//******************************************************************************
139LPVOID WIN32API TlsGetValue(DWORD index)
140{
141 LPVOID rc;
142
143 rc = O32_TlsGetValue(index);
144// dprintf(("KERNEL32: TlsGetValue %d returned %X\n", index, rc));
145 return(rc);
146}
147//******************************************************************************
148//******************************************************************************
149BOOL WIN32API TlsSetValue(DWORD index, LPVOID val)
150{
151// dprintf(("KERNEL32: TlsSetValue\n"));
152 return(O32_TlsSetValue(index, val));
153}
154//******************************************************************************
155//******************************************************************************
Note: See TracBrowser for help on using the repository browser.