From 0ce0096afa0295949f1288ea4dfe763d446a0c43 Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Wed, 25 Sep 2024 15:47:45 -0500 Subject: [PATCH 1/3] Split H5TS termination --- src/H5.c | 5 ++++- src/H5TSint.c | 38 ++++++++++++++++++++++++++++++-------- src/H5TSprivate.h | 1 + 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/H5.c b/src/H5.c index 9dc9fca1022..b1855c15889 100644 --- a/src/H5.c +++ b/src/H5.c @@ -433,11 +433,14 @@ H5_term_library(void) /* Shut down the following packages in strictly the order given * by the table. */ + , TERMINATOR(CX, true) +#ifdef H5_HAVE_THREADSAFE + , TERMINATOR(TS_top, true) +#endif , TERMINATOR(E, true) , TERMINATOR(I, true) , TERMINATOR(SL, true) , TERMINATOR(FL, true) - , TERMINATOR(CX, true) }; do { diff --git a/src/H5TSint.c b/src/H5TSint.c index a2761bb2ad3..4d3248667ab 100644 --- a/src/H5TSint.c +++ b/src/H5TSint.c @@ -133,10 +133,12 @@ H5TS__init(void) /*------------------------------------------------------------------------- * Function: H5TS_term_package * - * Purpose: Terminate this interface. + * Purpose: Terminate this interface. Clean up global resources shared by + * all threads. * * Note: This function is currently registered via atexit() and is called - * AFTER H5_term_library(). + * AFTER H5_term_library(). H5TS_top_term_package() is called at library + * termination to clean up per-thread resources. * * Return: void * @@ -151,8 +153,8 @@ H5TS_term_package(void) H5TS_mutex_destroy(&H5TS_api_info_p.api_mutex); H5TS_atomic_destroy_uint(&H5TS_api_info_p.attempt_lock_count); - /* Clean up per-thread library info */ - H5TS__tinfo_term(); + /* Release critical section / mutex for modifying the thread info globals */ + H5TS_mutex_destroy(&H5TS_tinfo_mtx_s); FUNC_LEAVE_NOAPI_VOID } /* end H5TS_term_package() */ @@ -535,6 +537,30 @@ H5TS__tinfo_destroy(void *_tinfo_node) FUNC_LEAVE_NOAPI_VOID_NAMECHECK_ONLY } +/*-------------------------------------------------------------------------- + * Function: H5TS_top_term_package + * + * Purpose: Terminate the threadlocal parts of the H5TS interface during library terminaton. + * + * Note: See H5TS_term_package for termination of the thread-global resources + * + * Return: Non-negative on success / Negative on failure + * + *-------------------------------------------------------------------------- + */ +int +H5TS_top_term_package(void) +{ + int n = 0; + + FUNC_ENTER_NOAPI_NOINIT_NOERR + + /* Clean up per-thread library info */ + H5TS__tinfo_term(); + + FUNC_LEAVE_NOAPI(n) +} + /*-------------------------------------------------------------------------- * Function: H5TS__tinfo_term * @@ -562,10 +588,6 @@ H5TS__tinfo_term(void) if (H5_UNLIKELY(H5TS_mutex_unlock(&H5TS_tinfo_mtx_s) < 0)) HGOTO_DONE(FAIL); - /* Release critical section / mutex for modifying the thread info globals */ - if (H5_UNLIKELY(H5TS_mutex_destroy(&H5TS_tinfo_mtx_s) < 0)) - HGOTO_DONE(FAIL); - /* Release key for thread-specific API contexts */ if (H5_UNLIKELY(H5TS_key_delete(H5TS_thrd_info_key_g) < 0)) HGOTO_DONE(FAIL); diff --git a/src/H5TSprivate.h b/src/H5TSprivate.h index d2be12414d6..64fd567a9bb 100644 --- a/src/H5TSprivate.h +++ b/src/H5TSprivate.h @@ -276,6 +276,7 @@ typedef atomic_flag H5TS_spinlock_t; #ifdef H5_HAVE_THREADSAFE /* Library/thread init/term operations */ H5_DLL void H5TS_term_package(void); +H5_DLL int H5TS_top_term_package(void); /* API locking */ H5_DLL herr_t H5TS_api_lock(void); From de4fcd42c0fc65d441589a028b65df491c79dd09 Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Fri, 27 Sep 2024 09:48:50 -0500 Subject: [PATCH 2/3] Restore original termination order for H5CX --- src/H5.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/H5.c b/src/H5.c index b1855c15889..9dc9fca1022 100644 --- a/src/H5.c +++ b/src/H5.c @@ -433,14 +433,11 @@ H5_term_library(void) /* Shut down the following packages in strictly the order given * by the table. */ - , TERMINATOR(CX, true) -#ifdef H5_HAVE_THREADSAFE - , TERMINATOR(TS_top, true) -#endif , TERMINATOR(E, true) , TERMINATOR(I, true) , TERMINATOR(SL, true) , TERMINATOR(FL, true) + , TERMINATOR(CX, true) }; do { From 5f178b4cc02b20c43d6589ade3e75ac7feac3ba5 Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Fri, 27 Sep 2024 12:27:18 -0500 Subject: [PATCH 3/3] Move mutex destroy back to tinfo_term --- src/H5TSint.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/H5TSint.c b/src/H5TSint.c index 4d3248667ab..322b7fe98c0 100644 --- a/src/H5TSint.c +++ b/src/H5TSint.c @@ -153,9 +153,6 @@ H5TS_term_package(void) H5TS_mutex_destroy(&H5TS_api_info_p.api_mutex); H5TS_atomic_destroy_uint(&H5TS_api_info_p.attempt_lock_count); - /* Release critical section / mutex for modifying the thread info globals */ - H5TS_mutex_destroy(&H5TS_tinfo_mtx_s); - FUNC_LEAVE_NOAPI_VOID } /* end H5TS_term_package() */ @@ -592,6 +589,9 @@ H5TS__tinfo_term(void) if (H5_UNLIKELY(H5TS_key_delete(H5TS_thrd_info_key_g) < 0)) HGOTO_DONE(FAIL); + /* Release critical section / mutex for modifying the thread info globals */ + if (H5_UNLIKELY(H5TS_mutex_destroy(&H5TS_tinfo_mtx_s) < 0)) + HGOTO_DONE(FAIL); done: FUNC_LEAVE_NOAPI_NAMECHECK_ONLY(ret_value) } /* end H5TS__tinfo_term() */