Index: libcxx/include/__threading_support =================================================================== --- libcxx/include/__threading_support +++ libcxx/include/__threading_support @@ -240,6 +240,9 @@ int __libcpp_tls_create(__libcpp_tls_key* __key, void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*)); +_LIBCPP_THREAD_ABI_VISIBILITY +void __libcpp_tls_destroy(__libcpp_tls_key __key); + _LIBCPP_THREAD_ABI_VISIBILITY void *__libcpp_tls_get(__libcpp_tls_key __key); Index: libcxx/src/support/win32/thread_win32.cpp =================================================================== --- libcxx/src/support/win32/thread_win32.cpp +++ libcxx/src/support/win32/thread_win32.cpp @@ -259,6 +259,11 @@ return 0; } +void __libcpp_tls_destroy(__libcpp_tls_key __key) +{ + FlsFree(__key); +} + void *__libcpp_tls_get(__libcpp_tls_key __key) { return FlsGetValue(__key); Index: libcxxabi/src/cxa_exception_storage.cpp =================================================================== --- libcxxabi/src/cxa_exception_storage.cpp +++ libcxxabi/src/cxa_exception_storage.cpp @@ -64,9 +64,18 @@ abort_message("cannot zero out thread value for __cxa_get_globals()"); } +#if defined(_LIBCPP_HAS_THREAD_API_WIN32) + void destroy_() { + std::__libcpp_tls_destroy(key_); + } +#endif + void construct_() { if (0 != std::__libcpp_tls_create(&key_, destruct_)) abort_message("cannot create thread specific key for __cxa_get_globals()"); +#if defined(_LIBCPP_HAS_THREAD_API_WIN32) + atexit(destroy_); +#endif } } // namespace