@@ -141,14 +141,21 @@ static void shutdown_common(void)
141141 */
142142#if defined(GIT_THREADS ) && defined(GIT_WIN32 )
143143
144- static DWORD _tls_index ;
144+ static DWORD _fls_index ;
145145static volatile LONG _mutex = 0 ;
146146
147+ static void WINAPI fls_free (void * st )
148+ {
149+ git__global_state_cleanup (st );
150+ git__free (st );
151+ }
152+
147153static int synchronized_threads_init (void )
148154{
149155 int error ;
150156
151- _tls_index = TlsAlloc ();
157+ if ((_fls_index = FlsAlloc (fls_free )) == FLS_OUT_OF_INDEXES )
158+ return -1 ;
152159
153160 git_threads_init ();
154161
@@ -190,9 +197,7 @@ int git_libgit2_shutdown(void)
190197 if ((ret = git_atomic_dec (& git__n_inits )) == 0 ) {
191198 shutdown_common ();
192199
193- git__free_tls_data ();
194-
195- TlsFree (_tls_index );
200+ FlsFree (_fls_index );
196201 git_mutex_free (& git__mwindow_mutex );
197202
198203#if defined(GIT_MSVC_CRTDBG )
@@ -213,7 +218,7 @@ git_global_st *git__global_state(void)
213218
214219 assert (git_atomic_get (& git__n_inits ) > 0 );
215220
216- if ((ptr = TlsGetValue ( _tls_index )) != NULL )
221+ if ((ptr = FlsGetValue ( _fls_index )) != NULL )
217222 return ptr ;
218223
219224 ptr = git__calloc (1 , sizeof (git_global_st ));
@@ -222,43 +227,10 @@ git_global_st *git__global_state(void)
222227
223228 git_buf_init (& ptr -> error_buf , 0 );
224229
225- TlsSetValue ( _tls_index , ptr );
230+ FlsSetValue ( _fls_index , ptr );
226231 return ptr ;
227232}
228233
229- /**
230- * Free the TLS data associated with this thread.
231- * This should only be used by the thread as it
232- * is exiting.
233- */
234- void git__free_tls_data (void )
235- {
236- void * ptr = TlsGetValue (_tls_index );
237- if (!ptr )
238- return ;
239-
240- git__global_state_cleanup (ptr );
241- git__free (ptr );
242- TlsSetValue (_tls_index , NULL );
243- }
244-
245- BOOL WINAPI DllMain (HINSTANCE hInstDll , DWORD fdwReason , LPVOID lpvReserved )
246- {
247- GIT_UNUSED (hInstDll );
248- GIT_UNUSED (lpvReserved );
249-
250- /* This is how Windows lets us know our thread is being shut down */
251- if (fdwReason == DLL_THREAD_DETACH ) {
252- git__free_tls_data ();
253- }
254-
255- /*
256- * Windows pays attention to this during library loading. We don't do anything
257- * so we trivially succeed.
258- */
259- return TRUE;
260- }
261-
262234#elif defined(GIT_THREADS ) && defined(_POSIX_THREADS )
263235
264236static pthread_key_t _tls_key ;
0 commit comments