diff --git a/avs_core/core/avisynth.cpp b/avs_core/core/avisynth.cpp index ada90421b..320bc06a5 100644 --- a/avs_core/core/avisynth.cpp +++ b/avs_core/core/avisynth.cpp @@ -1167,7 +1167,7 @@ struct ScriptEnvironmentTLS // this is a work-around for that. #if defined(AVS_WINDOWS) && !defined(__GNUC__) # ifdef XP_TLS -extern DWORD dwTlsIndex; +extern _TLS _tls; # else // does not work on XP when DLL is dynamic loaded. see dwTlsIndex instead __declspec(thread) ScriptEnvironmentTLS* g_TLS = nullptr; @@ -1195,14 +1195,14 @@ class ThreadScriptEnvironment : public InternalEnvironment if (thread_id != 0) { // thread pool thread #ifdef XP_TLS - ScriptEnvironmentTLS* g_TLS = (ScriptEnvironmentTLS*)(TlsGetValue(dwTlsIndex)); + ScriptEnvironmentTLS* g_TLS = (ScriptEnvironmentTLS*)(TlsGetValue(_tls.dwTlsIndex)); #endif if (g_TLS != nullptr) { ThrowError("Detected multiple ScriptEnvironmentTLSs for a single thread"); } g_TLS = &myTLS; #ifdef XP_TLS - if (!TlsSetValue(dwTlsIndex, g_TLS)) { + if (!TlsSetValue(_tls.dwTlsIndex, g_TLS)) { ThrowError("Could not store thread local value for ScriptEnvironmentTLS"); } #endif @@ -1219,7 +1219,7 @@ class ThreadScriptEnvironment : public InternalEnvironment #ifdef XP_TLS // a ? : b, evaluate 'a' only once #define IFNULL(a, b) ([&](){ auto val = (a); return ((val) == nullptr ? (b) : (val)); }()) -#define DISPATCH(name) IFNULL((ScriptEnvironmentTLS*)(TlsGetValue(dwTlsIndex)), coreTLS)->name +#define DISPATCH(name) IFNULL((ScriptEnvironmentTLS*)(TlsGetValue(_tls.dwTlsIndex)), coreTLS)->name #else #define DISPATCH(name) (g_TLS ? g_TLS : coreTLS)->name #endif @@ -1616,7 +1616,7 @@ class ThreadScriptEnvironment : public InternalEnvironment bool is_runtime = true; #ifdef XP_TLS - ScriptEnvironmentTLS* g_TLS = (ScriptEnvironmentTLS*)(TlsGetValue(dwTlsIndex)); + ScriptEnvironmentTLS* g_TLS = (ScriptEnvironmentTLS*)(TlsGetValue(_tls.dwTlsIndex)); #endif if (g_TLS == nullptr) { // not called by thread if (GetFrameRecursiveCount() == 0) { // not called by GetFrame @@ -1778,7 +1778,7 @@ class ThreadScriptEnvironment : public InternalEnvironment void __stdcall DeleteScriptEnvironment() { #ifdef XP_TLS - ScriptEnvironmentTLS* g_TLS = (ScriptEnvironmentTLS*)(TlsGetValue(dwTlsIndex)); + ScriptEnvironmentTLS* g_TLS = (ScriptEnvironmentTLS*)(TlsGetValue(_tls.dwTlsIndex)); #endif if (g_TLS != nullptr) { ThrowError("Cannot delete environment from a TLS proxy."); @@ -2261,7 +2261,7 @@ ScriptEnvironment::ScriptEnvironment() cacheMode(CACHE_DEFAULT) { #ifdef XP_TLS - if(dwTlsIndex == 0) + if(_tls.dwTlsIndex == 0) throw("ScriptEnvironment: TlsAlloc failed on DLL load"); #endif diff --git a/avs_core/core/internal.h b/avs_core/core/internal.h index 59148637c..f3d8f1153 100644 --- a/avs_core/core/internal.h +++ b/avs_core/core/internal.h @@ -299,4 +299,22 @@ class GlobalVarFrame } }; +#ifdef XP_TLS +#include +struct _TLS { + DWORD dwTlsIndex; + _TLS() : dwTlsIndex(0) { + if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) + throw("TlsAlloc failed"); + _RPT1(0, "TlsAlloc: dwTlsIndex=0x%x\n", dwTlsIndex); + + } + ~_TLS() { + _RPT1(0, "TlsFree: dwTlsIndex=0x%x\n", dwTlsIndex); + TlsFree(dwTlsIndex); + dwTlsIndex = 0; + } +}; +#endif + #endif // __Internal_H__ diff --git a/avs_core/core/main.cpp b/avs_core/core/main.cpp index 18bcb02b7..6bc92bcee 100644 --- a/avs_core/core/main.cpp +++ b/avs_core/core/main.cpp @@ -130,7 +130,7 @@ void ReportMe(const char * msg, ...) { static long gRefCnt=0; #ifdef XP_TLS -DWORD dwTlsIndex = 0; +_TLS _tls; #endif extern "C" const GUID CLSID_CAVIFileSynth // {E6D6B700-124D-11D4-86F3-DB80AFD98778} @@ -288,19 +288,6 @@ BOOL APIENTRY DllMain(HANDLE hModule, ULONG ulReason, LPVOID lpReserved) { _RPT4(0,"DllMain: hModule=0x%08x, ulReason=%x, lpReserved=0x%08x, gRefCnt = %ld\n", hModule, ulReason, lpReserved, gRefCnt); -#ifdef XP_TLS - if (ulReason == DLL_PROCESS_ATTACH) { - if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) - throw("Avisynth DLL load: TlsAlloc failed"); - _RPT1(0, "DllMain: TlsAlloc: dwTlsIndex=0x%x\n", dwTlsIndex); - } - else if (ulReason == DLL_PROCESS_DETACH) { - _RPT1(0, "DllMain: TlsFree: dwTlsIndex=0x%x\n", dwTlsIndex); - TlsFree(dwTlsIndex); - dwTlsIndex = 0; - } -#endif - return TRUE; }