mdbx-windows: remove MDBX_CONFIG_MANUAL_TLS_CALLBACK build option and add MDBX_MANUAL_MODULE_HANDLER macro.

Briefly:
 - Now constructor/destructor of "Thread Local Storage" handled automatically when possible.
 - Otherwise the MDBX_CONFIG_MANUAL_TLS_CALLBACK macro defined to 1 to indicate that mdbx_module_handle() should be called manually.
 - Corresponding build option MDBX_CONFIG_MANUAL_TLS_CALLBACK was removed.

Related to https://github.com/erthink/libmdbx/issues/155

Change-Id: Ic4e6a34b44f874676f0ab212ff473460e3d80559
This commit is contained in:
Leonid Yuriev 2021-05-08 17:44:21 +03:00
parent 3aa8701c6d
commit ecd3c06932
5 changed files with 34 additions and 41 deletions

View File

@ -445,9 +445,6 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
if(MDBX_NTDLL_EXTRA_IMPLIB)
add_mdbx_option(MDBX_WITHOUT_MSVC_CRT "Avoid dependence from MSVC CRT and use ntdll.dll instead" OFF)
endif()
add_mdbx_option(MDBX_CONFIG_MANUAL_TLS_CALLBACK
"Provide mdbx_dll_handler() for manual initialization" OFF)
mark_as_advanced(MDBX_CONFIG_MANUAL_TLS_CALLBACK)
else()
add_mdbx_option(MDBX_USE_OFDLOCKS "Use Open file description locks (aka OFD locks, non-POSIX)" AUTO)
mark_as_advanced(MDBX_USE_OFDLOCKS)

51
mdbx.h
View File

@ -587,9 +587,7 @@ extern LIBMDBX_VERINFO_API const struct MDBX_build_info {
const char *flags; /**< CFLAGS and CXXFLAGS */
} /** \brief libmdbx build information */ mdbx_build;
#if defined(_WIN32) || defined(_WIN64)
#if !MDBX_BUILD_SHARED_LIBRARY
#if (defined(_WIN32) || defined(_WIN64)) && !MDBX_BUILD_SHARED_LIBRARY
/* MDBX internally uses global and thread local storage destructors to
* automatically (de)initialization, releasing reader lock table slots
* and so on.
@ -600,44 +598,41 @@ extern LIBMDBX_VERINFO_API const struct MDBX_build_info {
*
* Otherwise, if MDBX was builded not as a DLL, some black magic
* may be required depending of Windows version:
*
* - Modern Windows versions, including Windows Vista and later, provides
* support for "TLS Directory" (e.g .CRT$XL[A-Z] sections in executable
* or dll file). In this case, MDBX capable of doing all automatically,
* and you do not need to call mdbx_dll_handler().
* therefore you DON'T NEED to call mdbx_module_handler()
* so the MDBX_MANUAL_MODULE_HANDLER defined as 0.
*
* - Obsolete versions of Windows, prior to Windows Vista, REQUIRES calling
* mdbx_dll_handler() manually from corresponding DllMain() or WinMain()
* of your DLL or application.
* - This behavior is under control of the MODX_CONFIG_MANUAL_TLS_CALLBACK
* option, which is determined by default according to the target version
* of Windows at build time.
* But you may override MODX_CONFIG_MANUAL_TLS_CALLBACK in special cases.
* mdbx_module_handler() manually from corresponding DllMain() or WinMain()
* of your DLL or application,
* so the MDBX_MANUAL_MODULE_HANDLER defined as 1.
*
* Therefore, building MDBX as a DLL is recommended for all version of Windows.
* So, if you doubt, just build MDBX as the separate DLL and don't worry. */
* So, if you doubt, just build MDBX as the separate DLL and don't care about
* the MDBX_MANUAL_MODULE_HANDLER. */
#ifndef MDBX_CONFIG_MANUAL_TLS_CALLBACK
#ifndef _WIN32_WINNT
#error Non-dll build libmdbx requires target Windows version \
to be explicitly defined via _WIN32_WINNT for properly \
handling thread local storage destructors.
#endif
#if _WIN32_WINNT >= 0x0600 /* Windows Vista */
/* As described above mdbx_dll_handler() is NOT needed forWindows Vista
* and later. */
#define MDBX_CONFIG_MANUAL_TLS_CALLBACK 0
#else
/* As described above mdbx_dll_handler() IS REQUIRED for Windows versions
* prior to Windows Vista. */
#define MDBX_CONFIG_MANUAL_TLS_CALLBACK 1
#endif
#endif /* MDBX_CONFIG_MANUAL_TLS_CALLBACK */
#endif /* _WIN32_WINNT */
#if MDBX_CONFIG_MANUAL_TLS_CALLBACK
void LIBMDBX_API NTAPI mdbx_dll_handler(PVOID module, DWORD reason,
#if _WIN32_WINNT >= 0x0600 /* Windows Vista */
/* As described above mdbx_module_handler() is NOT needed for Windows Vista
* and later. */
#define MDBX_MANUAL_MODULE_HANDLER 0
#else
/* As described above mdbx_module_handler() IS REQUIRED for Windows versions
* prior to Windows Vista. */
#define MDBX_MANUAL_MODULE_HANDLER 1
void LIBMDBX_API NTAPI mdbx_module_handler(PVOID module, DWORD reason,
PVOID reserved);
#endif /* MDBX_CONFIG_MANUAL_TLS_CALLBACK */
#endif /* !MDBX_BUILD_SHARED_LIBRARY */
#endif /* Windows */
#endif
#endif /* Windows && !DLL && MDBX_MANUAL_MODULE_HANDLER */
/* OPACITY STRUCTURES *********************************************************/

View File

@ -29,7 +29,6 @@
#cmakedefine01 MDBX_DISABLE_PAGECHECKS
/* Windows */
#cmakedefine01 MDBX_CONFIG_MANUAL_TLS_CALLBACK
#cmakedefine01 MDBX_WITHOUT_MSVC_CRT
/* MacOS & iOS */

View File

@ -22068,8 +22068,10 @@ __dll_export
#endif /* MacOS */
#if defined(_WIN32) || defined(_WIN64)
" MDBX_WITHOUT_MSVC_CRT=" STRINGIFY(MDBX_AVOID_CRT)
" MDBX_CONFIG_MANUAL_TLS_CALLBACK=" STRINGIFY(MDBX_CONFIG_MANUAL_TLS_CALLBACK)
" MDBX_BUILD_SHARED_LIBRARY=" STRINGIFY(MDBX_BUILD_SHARED_LIBRARY)
#if !MDBX_BUILD_SHARED_LIBRARY
" MDBX_MANUAL_MODULE_HANDLER=" STRINGIFY(MDBX_MANUAL_MODULE_HANDLER)
#endif
" WINVER=" STRINGIFY(WINVER)
#else /* Windows */
" MDBX_LOCKING=" MDBX_LOCKING_CONFIG

View File

@ -37,11 +37,11 @@ static void mdbx_winnt_import(void);
BOOL APIENTRY DllMain(HANDLE module, DWORD reason, LPVOID reserved)
#else
#if !MDBX_CONFIG_MANUAL_TLS_CALLBACK
#if !MDBX_MANUAL_MODULE_HANDLER
static
#endif /* !MDBX_CONFIG_MANUAL_TLS_CALLBACK */
#endif /* !MDBX_MANUAL_MODULE_HANDLER */
void NTAPI
mdbx_dll_handler(PVOID module, DWORD reason, PVOID reserved)
mdbx_module_handler(PVOID module, DWORD reason, PVOID reserved)
#endif /* MDBX_BUILD_SHARED_LIBRARY */
{
(void)reserved;
@ -65,7 +65,7 @@ static
#endif
}
#if !MDBX_BUILD_SHARED_LIBRARY && !MDBX_CONFIG_MANUAL_TLS_CALLBACK
#if !MDBX_BUILD_SHARED_LIBRARY && !MDBX_MANUAL_MODULE_HANDLER
/* *INDENT-OFF* */
/* clang-format off */
#if defined(_MSC_VER)
@ -89,7 +89,7 @@ static
# pragma data_seg(".CRT$XLB")
# endif
__declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK mdbx_tls_anchor = mdbx_dll_handler;
__declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK mdbx_tls_anchor = mdbx_module_handler;
# pragma data_seg(pop)
# pragma const_seg(pop)
@ -97,13 +97,13 @@ static
# ifdef _WIN64
const
# endif
PIMAGE_TLS_CALLBACK mdbx_tls_anchor __attribute__((__section__(".CRT$XLB"), used)) = mdbx_dll_handler;
PIMAGE_TLS_CALLBACK mdbx_tls_anchor __attribute__((__section__(".CRT$XLB"), used)) = mdbx_module_handler;
#else
# error FIXME
#endif
/* *INDENT-ON* */
/* clang-format on */
#endif /* !MDBX_BUILD_SHARED_LIBRARY && !MDBX_CONFIG_MANUAL_TLS_CALLBACK */
#endif /* !MDBX_BUILD_SHARED_LIBRARY && !MDBX_MANUAL_MODULE_HANDLER */
/*----------------------------------------------------------------------------*/