xref: /reactos/sdk/lib/crt/misc/tls.c (revision 845faec4)
1 #include <precomp.h>
2 #include <stdlib.h>
3 #include <internal/tls.h>
4 #include <internal/rterror.h>
5 
6 /* Index to TLS */
7 static DWORD msvcrt_tls_index;
8 
9 BOOL msvcrt_init_tls(void)
10 {
11   msvcrt_tls_index = TlsAlloc();
12 
13   if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
14   {
15     ERR("TlsAlloc() failed!\n");
16     return FALSE;
17   }
18   return TRUE;
19 }
20 
21 BOOL msvcrt_free_tls(void)
22 {
23   if (!TlsFree(msvcrt_tls_index))
24   {
25     ERR("TlsFree() failed!\n");
26     return FALSE;
27   }
28   return TRUE;
29 }
30 
31 thread_data_t *msvcrt_get_thread_data(void)
32 {
33     thread_data_t *ptr;
34     DWORD err = GetLastError();  /* need to preserve last error */
35 
36     if (!(ptr = TlsGetValue( msvcrt_tls_index )))
37     {
38         if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) )))
39             _amsg_exit( _RT_THREAD );
40         if (!TlsSetValue( msvcrt_tls_index, ptr )) _amsg_exit( _RT_THREAD );
41         ptr->tid = GetCurrentThreadId();
42         ptr->handle = INVALID_HANDLE_VALUE;
43         ptr->random_seed = 1;
44         ptr->locinfo = MSVCRT_locale->locinfo;
45         ptr->mbcinfo = MSVCRT_locale->mbcinfo;
46     }
47     SetLastError( err );
48     return ptr;
49 }
50 
51 void msvcrt_free_tls_mem(void)
52 {
53   thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
54 
55   if (tls)
56   {
57     CloseHandle(tls->handle);
58     HeapFree(GetProcessHeap(),0,tls->efcvt_buffer);
59     HeapFree(GetProcessHeap(),0,tls->asctime_buffer);
60     HeapFree(GetProcessHeap(),0,tls->wasctime_buffer);
61     HeapFree(GetProcessHeap(),0,tls->strerror_buffer);
62     HeapFree(GetProcessHeap(),0,tls->wcserror_buffer);
63     HeapFree(GetProcessHeap(),0,tls->time_buffer);
64     //if(tls->have_locale) {
65     //    free_locinfo(tls->locinfo);
66     //    free_mbcinfo(tls->mbcinfo);
67     //}
68   }
69   HeapFree(GetProcessHeap(), 0, tls);
70 }
71 
72 /* EOF */
73 
74