From: Arran Cudbard-Bell Date: Thu, 12 Jan 2023 20:17:47 +0000 (-0600) Subject: Just create two fr_atexit_global_once macros X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a542bc2f010999001fbfd71795617d2f0760f92c;p=thirdparty%2Ffreeradius-server.git Just create two fr_atexit_global_once macros One that returns values, one that doesn't --- diff --git a/src/lib/server/module.c b/src/lib/server/module.c index c0bf9391247..85632c45b9d 100644 --- a/src/lib/server/module.c +++ b/src/lib/server/module.c @@ -1135,11 +1135,10 @@ static int _module_global_list_free(UNUSED void *uctx) */ void modules_init(char const *lib_dir) { - int ret; /* * Create the global module heap we use for * common indexes in the thread-specific * heaps. */ - fr_atexit_global_once(NULL, _module_global_list_init, _module_global_list_free, UNCONST(char *, lib_dir)); + fr_atexit_global_once(_module_global_list_init, _module_global_list_free, UNCONST(char *, lib_dir)); } diff --git a/src/lib/util/atexit.h b/src/lib/util/atexit.h index 1b1bd4fdc4a..eb47c7f410a 100644 --- a/src/lib/util/atexit.h +++ b/src/lib/util/atexit.h @@ -93,7 +93,7 @@ static inline int _fr_atexit_global_once_funcs(fr_atexit_t init_func, fr_atexit_ static inline void fr_atexit_noop(void) {} static inline void fr_atexit_result(int *ret, int val) { *ret = val; } -/** Setup pair of global init/free functions +/** Setup pair of global init/free functions, returning errors from the specified init function * * Simplifies setting up data structures the first time a given function * is called. @@ -103,9 +103,8 @@ static inline void fr_atexit_result(int *ret, int val) { *ret = val; } * * Will not share init status outside of the function. * - * @param[out] _res Where to write the result of the init - * function if called. - * May be NULL. + * @param[out] _ret A pointer to where to write the result + * of the init function if called. * @param[in] _init function to call. Will be called once * during the process lifetime. * May be NULL. @@ -114,7 +113,7 @@ static inline void fr_atexit_result(int *ret, int val) { *ret = val; } * May be NULL. * @param[in] _uctx data to be passed to free function. */ -#define fr_atexit_global_once(_res, _init, _free, _uctx) \ +#define fr_atexit_global_once_ret(_ret, _init, _free, _uctx) \ { \ static atomic_bool _init_done = false; \ static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER; \ @@ -123,15 +122,37 @@ static inline void fr_atexit_result(int *ret, int val) { *ret = val; } pthread_mutex_lock(&_init_mutex); \ if (!atomic_load(&_init_done)) { \ if (_fr_atexit_global_once_funcs(_init, _free, _our_uctx) < 0) { \ - _Generic((_res), int : fr_atexit_result((int *)&(_res), -1), default: fr_atexit_noop()); \ + *(_ret) = -1; \ pthread_mutex_unlock(&_init_mutex); \ } \ atomic_store(&_init_done, true); \ } \ pthread_mutex_unlock(&_init_mutex); \ } \ - _Generic((_res), int : fr_atexit_result((int *)&(_res), 0), default: fr_atexit_noop()); \ + *(_ret) = 0; \ } + +/** Setup pair of global init/free functions + * + * Simplifies setting up data structures the first time a given function + * is called. + * + * Should be used in the body of the function before any initialisation + * dependent code. + * + * Will not share init status outside of the function. + * + * @param[in] _init function to call. Will be called once + * during the process lifetime. + * May be NULL. + * @param[in] _free function to call. Will be called once + * at exit. + * May be NULL. + * @param[in] _uctx data to be passed to free function. + */ +#define fr_atexit_global_once(_init, _free, _uctx) \ + fr_atexit_global_once_ret(&(int){ 0 }, _init, _free, _uctx) + /** Set a destructor for thread local storage to free the memory on thread exit * * @note Pointers to thread local storage seem to become unusable as threads are diff --git a/src/lib/util/event.c b/src/lib/util/event.c index 8afdf5b2e75..f267d429e58 100644 --- a/src/lib/util/event.c +++ b/src/lib/util/event.c @@ -2791,7 +2791,7 @@ fr_event_list_t *fr_event_list_alloc(TALLOC_CTX *ctx, fr_event_status_cb_t statu * Build the map indexes the first time this * function is called. */ - fr_atexit_global_once(ret, _event_build_indexes, _event_free_indexes, NULL); + fr_atexit_global_once_ret(&ret, _event_build_indexes, _event_free_indexes, NULL); if (unlikely(ret < 0)) return NULL; el = talloc_zero(ctx, fr_event_list_t); diff --git a/src/lib/util/regex.c b/src/lib/util/regex.c index 2807b0f4a16..f7a211712c4 100644 --- a/src/lib/util/regex.c +++ b/src/lib/util/regex.c @@ -788,7 +788,7 @@ ssize_t regex_compile(TALLOC_CTX *ctx, regex_t **out, char const *pattern, size_ int ret; regex_t *preg; - fr_atexit_global_once(ret, _pcre_globals_configure, _pcre_globals_reset, NULL); + fr_atexit_global_once_ret(&ret, _pcre_globals_configure, _pcre_globals_reset, NULL); if (unlikely(ret < 0)) return -1;