From: Arran Cudbard-Bell Date: Wed, 5 Jan 2022 02:09:15 +0000 (-0600) Subject: More, minor, atexit cleanups X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a62b04adcc2cd70d909cda25c1e3b358b1c996c;p=thirdparty%2Ffreeradius-server.git More, minor, atexit cleanups --- diff --git a/src/lib/util/atexit.c b/src/lib/util/atexit.c index 45ebb953c7..c880c0635b 100644 --- a/src/lib/util/atexit.c +++ b/src/lib/util/atexit.c @@ -358,7 +358,7 @@ void fr_atexit_thread_local_disarm_all(void) * @param[in] uctx associated with the entry. * @return How many global destructors were disarmed. */ -unsigned int fr_atexit_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx) +unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx) { fr_atexit_entry_t *e = NULL; unsigned int count = 0; @@ -390,7 +390,7 @@ unsigned int fr_atexit_disarm(bool uctx_scope, fr_atexit_t func, void const *uct * @note This function's primary purpose is to help diagnose issues with destructors * from within a debugger. */ -void fr_atexit_disarm_all(void) +void fr_atexit_global_disarm_all(void) { fr_atexit_entry_t *e = NULL; diff --git a/src/lib/util/atexit.h b/src/lib/util/atexit.h index 57f14c1629..b4b0a767cc 100644 --- a/src/lib/util/atexit.h +++ b/src/lib/util/atexit.h @@ -15,13 +15,13 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -/** Functions to help with thread local destructors +/** Functions to help with cleanup * - * Simplifies calling thread local destructors (called when the thread exits). + * Simplifies cleaning up thread local and global resources * * @file lib/util/atexit.h * - * @copyright 2020-2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org) + * @copyright 2020-2022 Arran Cudbard-Bell (a.cudbardb@freeradius.org) * @copyright 2013-2016 The FreeRADIUS server project */ RCSIDH(atexit_h, "$Id$") @@ -74,16 +74,17 @@ int _atexit_global(NDEBUG_LOCATION_ARGS fr_atexit_t func, void const *uctx); * during the process lifetime. * @param[in] _free function to call. Will be called once * at exit. + * @param[in] _uctx data to be passed to free function. */ -#define fr_atexit_global_once(_init, _free) \ +#define fr_atexit_global_once(_init, _free, _uctx) \ { \ static atomic_bool _init_done = false; \ static pthread_mutex_t _init_mutex = PTHREAD_MUTEX_INITIALIZER; \ if (unlikely(!atomic_load(&_init_done))) { \ pthread_mutex_lock(&_init_mutex); \ if (!atomic_load(&_init_done)) { \ - _init(); \ - atexit(_free); \ + _init(_uctx); \ + fr_atexit_global(_free, _uctx); \ atomic_store(&_init_done, true); \ } \ pthread_mutex_unlock(&_init_mutex); \ @@ -96,25 +97,26 @@ int _atexit_global(NDEBUG_LOCATION_ARGS fr_atexit_t func, void const *uctx); * destroyed. So we need to store the address of the memory to free, not * the address of the thread local variable. * - * @param _n Name of variable e.g. 'my_tls'. - * @param _f Destructor, called when the thread exits to clean up any data. - * @param _v Memory to free. + * @param[in] _name Name of variable e.g. 'my_tls'. + * @param[in] _free Destructor, called when the thread exits to clean up any data. + * @param[in] _uctx Memory to free. */ -# define fr_atexit_thread_local(_n, _f, _v) \ +# define fr_atexit_thread_local(_name, _free, _uctx) \ do { \ - _fr_atexit_thread_local(NDEBUG_LOCATION_EXP _f, _v); \ - _n = _v; \ + _fr_atexit_thread_local(NDEBUG_LOCATION_EXP _free, _uctx); \ + _name = _uctx; \ } while (0); -int _fr_atexit_thread_local(NDEBUG_LOCATION_ARGS - fr_atexit_t func, void const *uctx); + +int _fr_atexit_thread_local(NDEBUG_LOCATION_ARGS + fr_atexit_t func, void const *uctx); unsigned int fr_atexit_thread_local_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx); void fr_atexit_thread_local_disarm_all(void); -unsigned int fr_atexit_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx); +unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx); -void fr_atexit_disarm_all(void); +void fr_atexit_global_disarm_all(void); unsigned int fr_atexit_trigger(bool uctx_scope, fr_atexit_t func, void const *uctx); diff --git a/src/lib/util/event.c b/src/lib/util/event.c index 45b281c261..fa1e63e687 100644 --- a/src/lib/util/event.c +++ b/src/lib/util/event.c @@ -2583,14 +2583,14 @@ static int _event_list_free(fr_event_list_t *el) /** Free any memory we allocated for indexes * */ -static void _event_free_indexes(void) +static void _event_free_indexes(UNUSED void *uctx) { unsigned int i; for (i = 0; i < NUM_ELEMENTS(filter_maps); i++) talloc_free(filter_maps[i].ev_to_func); } -static void _event_build_indexes(void) +static void _event_build_indexes(UNUSED void *uctx) { unsigned int i; @@ -2615,7 +2615,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(_event_build_indexes, _event_free_indexes); + fr_atexit_global_once(_event_build_indexes, _event_free_indexes, NULL); el = talloc_zero(ctx, fr_event_list_t); if (!fr_cond_assert(el)) {