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
pull/15838/head
yuriyluxriot 5 years ago committed by Alexander Alekhin
parent ed7e4273cd
commit 4e156a162f
  1. 36
      modules/core/src/system.cpp

@ -131,6 +131,10 @@ void* allocSingletonNewBuffer(size_t size) { return malloc(size); }
#if (_WIN32_WINNT >= 0x0602) #if (_WIN32_WINNT >= 0x0602)
#include <synchapi.h> #include <synchapi.h>
#endif #endif
#if ((_WIN32_WINNT >= 0x0600) && !defined(CV_DISABLE_FLS)) || defined(CV_FORCE_FLS)
#include <fibersapi.h>
#define CV_USE_FLS
#endif
#undef small #undef small
#undef min #undef min
#undef max #undef max
@ -142,7 +146,7 @@ void* allocSingletonNewBuffer(size_t size) { return malloc(size); }
#ifndef __cplusplus_winrt #ifndef __cplusplus_winrt
#include <windows.storage.h> #include <windows.storage.h>
#pragma comment(lib, "runtimeobject.lib") #pragma comment(lib, "runtimeobject.lib")
#endif #endif // WINRT
std::wstring GetTempPathWinRT() std::wstring GetTempPathWinRT()
{ {
@ -1422,24 +1426,43 @@ void TlsAbstraction::SetData(void *pData)
tlsData = pData; tlsData = pData;
} }
#else //WINRT #else //WINRT
#ifdef CV_USE_FLS
static void NTAPI opencv_fls_destructor(void* pData);
#endif // CV_USE_FLS
TlsAbstraction::TlsAbstraction() TlsAbstraction::TlsAbstraction()
{ {
#ifndef CV_USE_FLS
tlsKey = TlsAlloc(); tlsKey = TlsAlloc();
#else // CV_USE_FLS
tlsKey = FlsAlloc(opencv_fls_destructor);
#endif // CV_USE_FLS
CV_Assert(tlsKey != TLS_OUT_OF_INDEXES); CV_Assert(tlsKey != TLS_OUT_OF_INDEXES);
} }
TlsAbstraction::~TlsAbstraction() TlsAbstraction::~TlsAbstraction()
{ {
#ifndef CV_USE_FLS
TlsFree(tlsKey); TlsFree(tlsKey);
#else // CV_USE_FLS
FlsFree(tlsKey);
#endif // CV_USE_FLS
} }
void* TlsAbstraction::GetData() const void* TlsAbstraction::GetData() const
{ {
#ifndef CV_USE_FLS
return TlsGetValue(tlsKey); return TlsGetValue(tlsKey);
#else // CV_USE_FLS
return FlsGetValue(tlsKey);
#endif // CV_USE_FLS
} }
void TlsAbstraction::SetData(void *pData) void TlsAbstraction::SetData(void *pData)
{ {
#ifndef CV_USE_FLS
CV_Assert(TlsSetValue(tlsKey, pData) == TRUE); 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 #else // _WIN32
static void opencv_tls_destructor(void* pData); static void opencv_tls_destructor(void* pData);
TlsAbstraction::TlsAbstraction() TlsAbstraction::TlsAbstraction()
@ -1674,7 +1697,14 @@ static void opencv_tls_destructor(void* pData)
{ {
getTlsStorage().releaseThread(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 } // namespace details
using namespace details; using namespace details;

Loading…
Cancel
Save