diff --git a/include/wx/tls.h b/include/wx/tls.h index 6a4ee03786..f05a3f42d5 100644 --- a/include/wx/tls.h +++ b/include/wx/tls.h @@ -44,6 +44,7 @@ #ifdef wxHAS_COMPILER_TLS #define wxTLS_TYPE(T) wxTHREAD_SPECIFIC_DECL T + #define wxTLS_TYPE_REF(T) T& #define wxTLS_PTR(var) (&(var)) #define wxTLS_VALUE(var) (var) #else // !wxHAS_COMPILER_TLS @@ -135,6 +136,7 @@ }; #define wxTLS_TYPE(T) wxTlsValue + #define wxTLS_TYPE_REF(T) wxTLS_TYPE(T)& #define wxTLS_PTR(var) ((var).Get()) #define wxTLS_VALUE(var) (*(var)) #endif // wxHAS_COMPILER_TLS/!wxHAS_COMPILER_TLS diff --git a/src/common/threadinfo.cpp b/src/common/threadinfo.cpp index be07847b70..e77682db17 100644 --- a/src/common/threadinfo.cpp +++ b/src/common/threadinfo.cpp @@ -48,26 +48,33 @@ inline wxAllThreadInfos& GetAllThreadInfos() } // Pointer to the current thread's instance -wxTLS_TYPE(wxThreadSpecificInfo*) g_thisThreadInfo; +inline wxTLS_TYPE_REF(wxThreadSpecificInfo*) GetThisThreadInfo() +{ + static wxTLS_TYPE(wxThreadSpecificInfo*) s_thisThreadInfo; + + return s_thisThreadInfo; +} + +#define wxTHIS_THREAD_INFO wxTLS_VALUE(GetThisThreadInfo()) } // anonymous namespace wxThreadSpecificInfo& wxThreadSpecificInfo::Get() { - if ( !wxTLS_VALUE(g_thisThreadInfo) ) + if ( !wxTHIS_THREAD_INFO ) { - wxTLS_VALUE(g_thisThreadInfo) = new wxThreadSpecificInfo; + wxTHIS_THREAD_INFO = new wxThreadSpecificInfo; wxCriticalSectionLocker lock(GetAllThreadInfosCS()); GetAllThreadInfos().push_back( - wxSharedPtr(wxTLS_VALUE(g_thisThreadInfo))); + wxSharedPtr(wxTHIS_THREAD_INFO)); } - return *wxTLS_VALUE(g_thisThreadInfo); + return *wxTHIS_THREAD_INFO; } void wxThreadSpecificInfo::ThreadCleanUp() { - if ( !wxTLS_VALUE(g_thisThreadInfo) ) + if ( !wxTHIS_THREAD_INFO ) return; // nothing to do, not used by this thread at all // find this thread's instance in GetAllThreadInfos() and destroy it @@ -76,10 +83,10 @@ void wxThreadSpecificInfo::ThreadCleanUp() i != GetAllThreadInfos().end(); ++i ) { - if ( i->get() == wxTLS_VALUE(g_thisThreadInfo) ) + if ( i->get() == wxTHIS_THREAD_INFO ) { GetAllThreadInfos().erase(i); - wxTLS_VALUE(g_thisThreadInfo) = NULL; + wxTHIS_THREAD_INFO = NULL; break; } }