From 4e156a162f6cfd3ed9243d4bdc4328b4fa13a023 Mon Sep 17 00:00:00 2001 From: yuriyluxriot Date: Fri, 1 Nov 2019 21:33:12 +0200 Subject: [PATCH] Merge pull request #15812 from yuriyluxriot:fls_replaces_tls * Use FlsAlloc/FlsFree/FlsGetValue/FlsSetValue instead of TlsAlloc/TlsFree/TlsGetValue/TlsSetValue to implment TLS value cleanup when thread has been terminated on Windows Vista and above * Fix 32-bit build * Fixed calling convention of cleanup callback * WINAPI changed to NTAPI * Use proper guard macro --- modules/core/src/system.cpp | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index b39173de0d..47cb63ee87 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -131,6 +131,10 @@ void* allocSingletonNewBuffer(size_t size) { return malloc(size); } #if (_WIN32_WINNT >= 0x0602) #include #endif +#if ((_WIN32_WINNT >= 0x0600) && !defined(CV_DISABLE_FLS)) || defined(CV_FORCE_FLS) + #include + #define CV_USE_FLS +#endif #undef small #undef min #undef max @@ -142,7 +146,7 @@ void* allocSingletonNewBuffer(size_t size) { return malloc(size); } #ifndef __cplusplus_winrt #include #pragma comment(lib, "runtimeobject.lib") -#endif +#endif // WINRT std::wstring GetTempPathWinRT() { @@ -1422,24 +1426,43 @@ void TlsAbstraction::SetData(void *pData) tlsData = pData; } #else //WINRT +#ifdef CV_USE_FLS +static void NTAPI opencv_fls_destructor(void* pData); +#endif // CV_USE_FLS TlsAbstraction::TlsAbstraction() { +#ifndef CV_USE_FLS tlsKey = TlsAlloc(); +#else // CV_USE_FLS + tlsKey = FlsAlloc(opencv_fls_destructor); +#endif // CV_USE_FLS CV_Assert(tlsKey != TLS_OUT_OF_INDEXES); } TlsAbstraction::~TlsAbstraction() { +#ifndef CV_USE_FLS TlsFree(tlsKey); +#else // CV_USE_FLS + FlsFree(tlsKey); +#endif // CV_USE_FLS } void* TlsAbstraction::GetData() const { +#ifndef CV_USE_FLS return TlsGetValue(tlsKey); +#else // CV_USE_FLS + return FlsGetValue(tlsKey); +#endif // CV_USE_FLS } void TlsAbstraction::SetData(void *pData) { +#ifndef CV_USE_FLS CV_Assert(TlsSetValue(tlsKey, pData) == TRUE); +#else // CV_USE_FLS + CV_Assert(FlsSetValue(tlsKey, pData) == TRUE); +#endif // CV_USE_FLS } -#endif +#endif // WINRT #else // _WIN32 static void opencv_tls_destructor(void* pData); TlsAbstraction::TlsAbstraction() @@ -1674,7 +1697,14 @@ static void opencv_tls_destructor(void* pData) { getTlsStorage().releaseThread(pData); } -#endif +#else // _WIN32 +#ifdef CV_USE_FLS +static void WINAPI opencv_fls_destructor(void* pData) +{ + getTlsStorage().releaseThread(pData); +} +#endif // CV_USE_FLS +#endif // _WIN32 } // namespace details using namespace details;