diff options
author | Dana Robinson <derobins@hdfgroup.org> | 2014-04-04 20:51:30 (GMT) |
---|---|---|
committer | Dana Robinson <derobins@hdfgroup.org> | 2014-04-04 20:51:30 (GMT) |
commit | b17ef126a75779f00ee4fef70a2b590b2c19fb4e (patch) | |
tree | 846887128b92979fd6e1c3cd84bb6a5c445e1068 /src/H5.c | |
parent | 79a891c2417859de411a36e25203499f1f0d4280 (diff) | |
download | hdf5-b17ef126a75779f00ee4fef70a2b590b2c19fb4e.zip hdf5-b17ef126a75779f00ee4fef70a2b590b2c19fb4e.tar.gz hdf5-b17ef126a75779f00ee4fef70a2b590b2c19fb4e.tar.bz2 |
[svn-r24961] Updates to Win32 thread-local storage cleanup when the thread-safe library is built on Windows. Previously, thread-local storage was not cleaned up, causing resource leaks.
Fixes HDFFV-8518, HDFFV-8699
As a part of these changes, the thread-safe + static library options are declared unsupported since the solution relies on DllMain. A solution for the static library is probably doable, but requires much more complicated surgery and has been deferred to HDF5 1.8.14.
Tested on:
64-bit Windows 7 using VS 2012 (changes only affect Windows)
Diffstat (limited to 'src/H5.c')
-rw-r--r-- | src/H5.c | 73 |
1 files changed, 70 insertions, 3 deletions
@@ -151,13 +151,23 @@ H5_init_library(void) #endif /* - * Install atexit() library cleanup routine unless the H5dont_atexit() + * Install atexit() library cleanup routines unless the H5dont_atexit() * has been called. Once we add something to the atexit() list it stays * there permanently, so we set H5_dont_atexit_g after we add it to prevent * adding it again later if the library is cosed and reopened. */ if (!H5_dont_atexit_g) { - (void)HDatexit(H5_term_library); + +#if defined(H5_HAVE_THREADSAFE) && defined(H5_HAVE_WIN_THREADS) + /* Clean up Win32 thread resources. Pthreads automatically cleans up. + * This must be entered before the library cleanup code so it's + * executed in LIFO order (i.e., last). + */ + (void)HDatexit(H5TS_win32_process_exit); +#endif /* H5_HAVE_THREADSAFE && H5_HAVE_WIN_THREADS */ + + /* Normal library termination code */ + (void)HDatexit(H5_term_library); H5_dont_atexit_g = TRUE; } /* end if */ @@ -330,7 +340,7 @@ H5_term_library(void) done: #ifdef H5_HAVE_THREADSAFE H5_API_UNLOCK -#endif +#endif /* H5_HAVE_THREADSAFE */ return; } /* end H5_term_library() */ @@ -831,3 +841,60 @@ H5free_memory(void *mem) FUNC_LEAVE_API(SUCCEED) } /* end H5free_memory() */ + +#ifdef H5_HAVE_WIN32_API +/*------------------------------------------------------------------------- + * Function: DllMain + * + * Purpose: Handles various conditions in the library on Windows. + * + * NOTE: The main purpose of this is for handling Win32 thread cleanup + * on thread/process detach. + * + * Return: TRUE on success, FALSE on failure + * + *------------------------------------------------------------------------- + */ +BOOL WINAPI +DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) +{ + /* Don't add our function enter/leave macros since this function will be + * called before the library is initialized. + * + * NOTE: Do NOT call any CRT functions in DllMain! + * This includes any functions that are called by from here! + */ + + BOOL fOkay = TRUE; + + switch(fdwReason) + { + case DLL_PROCESS_ATTACH: + break; + + case DLL_PROCESS_DETACH: + break; + + case DLL_THREAD_ATTACH: +#ifdef H5_HAVE_WIN_THREADS + if(H5TS_win32_thread_enter() < 0) + fOkay = FALSE; +#endif /* H5_HAVE_WIN_THREADS */ + break; + + case DLL_THREAD_DETACH: +#ifdef H5_HAVE_WIN_THREADS + if(H5TS_win32_thread_exit() < 0) + fOkay = FALSE; +#endif /* H5_HAVE_WIN_THREADS */ + break; + + default: + /* Shouldn't get here */ + fOkay = FALSE; + break; + } + + return fOkay; +} +#endif /* H5_HAVE_WIN32_API */ |