From 3f05a466efa04821e41cb85828c1bd868e74e78b Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Mon, 18 Nov 2024 11:02:20 -0600 Subject: [PATCH] Move atomic type specification into macros --- src/H5VLdyn_ops.c | 34 ++-------------------------------- src/H5VLint.c | 10 +--------- src/H5VLpassthru.c | 14 +++++--------- src/H5VLprivate.h | 12 ++---------- src/H5public.h | 15 +++++++++++++++ test/h5test.c | 15 ++++----------- test/h5test.h | 15 ++++----------- 7 files changed, 33 insertions(+), 82 deletions(-) diff --git a/src/H5VLdyn_ops.c b/src/H5VLdyn_ops.c index 1ef33d3b11d..f7e24be1e1b 100644 --- a/src/H5VLdyn_ops.c +++ b/src/H5VLdyn_ops.c @@ -67,11 +67,7 @@ static void H5VL__release_dyn_op(H5VL_dyn_op_t *dyn_op); /*******************/ /* The current optional operation values */ -#ifdef H5_HAVE_MULTITHREAD -/* Initialize in H5VL_init_opt_operation_table() */ -static _Atomic(int) H5VL_opt_vals_g[H5VL_SUBCLS_TOKEN + 1]; -#else -static int H5VL_opt_vals_g[H5VL_SUBCLS_TOKEN + 1] = +static H5_ATOMIC_SPECIFIER(int) H5VL_opt_vals_g[H5VL_SUBCLS_TOKEN + 1] = { H5VL_RESERVED_NATIVE_OPTIONAL, /* H5VL_SUBCLS_NONE */ H5VL_RESERVED_NATIVE_OPTIONAL, /* H5VL_SUBCLS_INFO */ @@ -87,14 +83,9 @@ static int H5VL_opt_vals_g[H5VL_SUBCLS_TOKEN + 1] = H5VL_RESERVED_NATIVE_OPTIONAL, /* H5VL_SUBCLS_BLOB */ H5VL_RESERVED_NATIVE_OPTIONAL /* H5VL_SUBCLS_TOKEN */ }; -#endif /* The current optional operations' info */ -#ifdef H5_HAVE_MULTITHREAD -static _Atomic(H5SL_t *) H5VL_opt_ops_g[H5VL_SUBCLS_TOKEN + 1] = -#else -static H5SL_t *H5VL_opt_ops_g[H5VL_SUBCLS_TOKEN + 1] = -#endif +static H5_ATOMIC_SPECIFIER(H5SL_t *) H5VL_opt_ops_g[H5VL_SUBCLS_TOKEN + 1] = { NULL, /* H5VL_SUBCLS_NONE */ NULL, /* H5VL_SUBCLS_INFO */ @@ -189,27 +180,6 @@ H5VL__term_opt_operation(void) FUNC_LEAVE_NOAPI(SUCCEED) } /* H5VL__term_opt_operation() */ -#ifdef H5_HAVE_MULTITHREAD -/*--------------------------------------------------------------------------- - * Function: H5VL__init_opt_operation_table - * - * Purpose: Initialize the atomic optional operation table for VOL objects. - * - *--------------------------------------------------------------------------- - */ -void -H5VL__init_opt_operation_table(void) { - size_t subcls = 0; /* Index over the elements of operation array */ - - FUNC_ENTER_PACKAGE_NOERR - - /* Iterate over the VOL subclasses */ - for (subcls = 0; subcls < NELMTS(H5VL_opt_vals_g); subcls++) - atomic_init(&H5VL_opt_vals_g[subcls], H5VL_RESERVED_NATIVE_OPTIONAL); - - FUNC_LEAVE_NOAPI_VOID -} /* H5VL__init_opt_operation_table() */ -#endif /*--------------------------------------------------------------------------- * Function: H5VL__register_opt_operation * diff --git a/src/H5VLint.c b/src/H5VLint.c index 2a36872a8be..4ef2dc84423 100644 --- a/src/H5VLint.c +++ b/src/H5VLint.c @@ -57,11 +57,7 @@ /* Object wrapping context info */ typedef struct H5VL_wrap_ctx_t { -#ifdef H5_HAVE_MULTITHREAD - _Atomic unsigned rc; -#else - unsigned rc; /* Ref. count for the # of times the context was set / reset */ -#endif + H5_ATOMIC(unsigned) rc; /* Ref. count for the # of times the context was set / reset */ H5VL_t *connector; /* VOL connector for "outermost" class to start wrap */ void *obj_wrap_ctx; /* "wrap context" for outermost connector */ } H5VL_wrap_ctx_t; @@ -239,10 +235,6 @@ H5VL_init_phase2(void) if (H5VL__set_def_conn() < 0) HGOTO_ERROR(H5E_VOL, H5E_CANTSET, FAIL, "unable to set default VOL connector"); -#ifdef H5_HAVE_MULTITHREAD - /* Initialize the atomic dynamic optional operation table */ - H5VL__init_opt_operation_table(); -#endif done: FUNC_LEAVE_NOAPI(ret_value) } /* end H5VL_init_phase2() */ diff --git a/src/H5VLpassthru.c b/src/H5VLpassthru.c index 0a032a647aa..e2a92c88104 100644 --- a/src/H5VLpassthru.c +++ b/src/H5VLpassthru.c @@ -35,12 +35,13 @@ #include #include -/* TODO: H5_HAVE_MULTITHREAD seems not to be exposed to this module, always import */ -#include - /* Public HDF5 file */ #include "hdf5.h" +#ifdef H5_HAVE_MULTITHREAD +#include +#endif + /* This connector's header */ #include "H5VLpassthru.h" @@ -368,12 +369,7 @@ static const H5VL_class_t H5VL_pass_through_g = { }; /* The connector identification number, initialized at runtime */ -#ifdef H5_HAVE_MULTITHREAD -static _Atomic(hid_t) H5VL_PASSTHRU_ID_g = ATOMIC_VAR_INIT(H5I_INVALID_HID); -#else -static hid_t H5VL_PASSTHRU_ID_g = H5I_INVALID_HID; -#endif - +static H5_ATOMIC_SPECIFIER(hid_t) H5VL_PASSTHRU_ID_g = H5_ATOMIC_VAR_INIT(H5I_INVALID_HID); /*------------------------------------------------------------------------- * Function: H5VL__pass_through_new_obj * diff --git a/src/H5VLprivate.h b/src/H5VLprivate.h index e5ab7aacfd7..56cee886409 100644 --- a/src/H5VLprivate.h +++ b/src/H5VLprivate.h @@ -34,11 +34,7 @@ /* Internal struct to track VOL connector information for objects */ typedef struct H5VL_t { const H5VL_class_t *cls; /* Pointer to connector class struct */ -#ifdef H5_HAVE_MULTITHREAD - _Atomic int64_t nrefs; /* Number of references by objects using this struct */ -#else - int64_t nrefs; /* Number of references by objects using this struct */ -#endif + H5_ATOMIC(int64_t) nrefs; /* Number of references by objects using this struct */ hid_t id; /* Identifier for the VOL connector */ } H5VL_t; @@ -46,11 +42,7 @@ typedef struct H5VL_t { typedef struct H5VL_object_t { void *data; /* Pointer to connector-managed data for this object */ H5VL_t *connector; /* Pointer to VOL connector struct */ -#ifdef H5_HAVE_MULTITHREAD - _Atomic size_t rc; /* Reference count */ -#else - size_t rc; /* Reference count */ -#endif + H5_ATOMIC(size_t) rc; /* Reference count */ } H5VL_object_t; diff --git a/src/H5public.h b/src/H5public.h index a95a7f6e3b2..35a4ecc5a4b 100644 --- a/src/H5public.h +++ b/src/H5public.h @@ -408,6 +408,21 @@ typedef void (*H5_atclose_func_t)(void *ctx); extern "C" { #endif +/** + * Macros that paper over differences between single-threaded and + * multi-threaded builds of the library. + */ +#ifdef H5_HAVE_MULTITHREAD +#define H5_ATOMIC(type) _Atomic type +#define H5_ATOMIC_SPECIFIER(type) _Atomic(type) +#define H5_ATOMIC_VAR_INIT(value) ATOMIC_VAR_INIT(value) +#else +#define H5_ATOMIC(type) type +#define H5_ATOMIC_SPECIFIER(type) type +#define H5_ATOMIC_VAR_INIT(value) value +#endif + + /* Functions in H5.c */ /** * \ingroup H5 diff --git a/test/h5test.c b/test/h5test.c index 8d8acdff36e..278a4fb4938 100644 --- a/test/h5test.c +++ b/test/h5test.c @@ -110,17 +110,10 @@ const char *LIBVER_NAMES[] = {"earliest", /* H5F_LIBVER_EARLIEST = 0 */ static H5E_auto2_t err_func = NULL; /* Global variables for testing */ -#ifdef H5_HAVE_MULTITHREAD -_Atomic size_t n_tests_run_g = 0; -_Atomic size_t n_tests_passed_g = 0; -_Atomic size_t n_tests_failed_g = 0; -_Atomic size_t n_tests_skipped_g = 0; -#else -size_t n_tests_run_g = 0; -size_t n_tests_passed_g = 0; -size_t n_tests_failed_g = 0; -size_t n_tests_skipped_g = 0; -#endif /* H5_HAVE_MULTITHREAD */ +H5_ATOMIC(size_t) n_tests_run_g = 0; +H5_ATOMIC(size_t) n_tests_passed_g = 0; +H5_ATOMIC(size_t) n_tests_failed_g = 0; +H5_ATOMIC(size_t) n_tests_skipped_g = 0; /* Value of currently registered optional dynamic VOL operation */ int reg_opt_curr_op_val = 0; diff --git a/test/h5test.h b/test/h5test.h index 0e292dbbb8e..2e726092806 100644 --- a/test/h5test.h +++ b/test/h5test.h @@ -413,17 +413,10 @@ H5TEST_DLL char *getenv_all(MPI_Comm comm, int root, const char *name); /* Extern global variables */ H5TEST_DLLVAR int TestVerbosity; /* Global variables for testing */ -#ifdef H5_HAVE_MULTITHREAD -H5TEST_DLLVAR _Atomic size_t n_tests_run_g; -H5TEST_DLLVAR _Atomic size_t n_tests_passed_g; -H5TEST_DLLVAR _Atomic size_t n_tests_failed_g; -H5TEST_DLLVAR _Atomic size_t n_tests_skipped_g; -#else -H5TEST_DLLVAR size_t n_tests_run_g; -H5TEST_DLLVAR size_t n_tests_passed_g; -H5TEST_DLLVAR size_t n_tests_failed_g; -H5TEST_DLLVAR size_t n_tests_skipped_g; -#endif +H5TEST_DLLVAR H5_ATOMIC(size_t) n_tests_run_g; +H5TEST_DLLVAR H5_ATOMIC(size_t) n_tests_passed_g; +H5TEST_DLLVAR H5_ATOMIC(size_t) n_tests_failed_g; +H5TEST_DLLVAR H5_ATOMIC(size_t) n_tests_skipped_g; H5TEST_DLLVAR uint64_t vol_cap_flags_g;