27RCSID(
"$Id: 8d2651abf6cf4540dfd2e18016dab74eb5913768 $")
29#include <freeradius-devel/util/debug.h>
30#include <freeradius-devel/util/dlist.h>
31#include <freeradius-devel/util/atexit.h>
38#if defined(DEBUG_ATEXIT) && !defined(NDEBUG)
39# define ATEXIT_DEBUG FR_FAULT_LOG
41# define ATEXIT_DEBUG(...)
74static pthread_mutex_t fr_atexit_global_mutex = PTHREAD_MUTEX_INITIALIZER;
104 ATEXIT_DEBUG(
"%s - Thread %u freeing %p/%p func=%p, uctx=%p (alloced %s:%d)",
105 __FUNCTION__, (
unsigned int)pthread_self(),
136 ATEXIT_DEBUG(
"%s - Thread %u arming %p/%p func=%p, uctx=%p (alloced %s:%d)",
137 __FUNCTION__, (
unsigned int)pthread_self(),
151 ATEXIT_DEBUG(
"%s - Freeing destructor list %p", __FUNCTION__, list);
163 pthread_mutex_lock(&fr_atexit_global_mutex);
170 pthread_mutex_unlock(&fr_atexit_global_mutex);
171 TALLOC_FREE(fr_atexit_threads);
198 ATEXIT_DEBUG(
"%s - Alloced threads destructor list %p", __FUNCTION__, fr_atexit_threads);
210#define CHECK_GLOBAL_SETUP() \
213 pthread_mutex_lock(&fr_atexit_global_mutex); \
214 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
215 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
216 pthread_mutex_unlock(&fr_atexit_global_mutex); \
217 if (_ret < 0) return _ret; \
220#define CHECK_GLOBAL_SETUP() \
223 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
224 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
225 if (_ret < 0) return _ret; \
254 unsigned int count = 0;
261 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
263 ATEXIT_DEBUG(
"%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
269 talloc_set_destructor(disarm, NULL);
290 ATEXIT_DEBUG(
"%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
294 talloc_set_destructor(e, NULL);
313 unsigned int count = 0;
321 ATEXIT_DEBUG(
"%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
332 to_free->func, to_free->uctx,
333 to_free->file, to_free->line);
362 unsigned int count = 0;
370 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
372 ATEXIT_DEBUG(
"%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
383 to_free->func, to_free->uctx,
384 to_free->file, to_free->line);
392 if (!fr_atexit_threads)
return 0;
400 if (!e->
func)
continue;
405 if ((ee->
func != func) || ((ee->
uctx != uctx) && uctx_scope))
continue;
407 ATEXIT_DEBUG(
"%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
409 (
unsigned int)pthread_self(),
419 to_free->func, to_free->uctx,
420 to_free->file, to_free->line);
460 bool save_is_exiting;
462 pthread_mutex_lock(&fr_atexit_global_mutex);
464 pthread_mutex_unlock(&fr_atexit_global_mutex);
466 return save_is_exiting;
478 ATEXIT_DEBUG(
"%s - Freeing _Thread_local destructor list %p", __FUNCTION__, list);
481 pthread_mutex_lock(&fr_atexit_global_mutex);
482 list->
e->
func = NULL;
483 pthread_mutex_unlock(&fr_atexit_global_mutex);
491static void _thread_local_pthread_free(
void *list)
500static int _thread_local_free(
void *list)
512int _fr_atexit_thread_local(
char const *file,
int line,
531 ATEXIT_DEBUG(
"%s - Thread %u alloced _Thread_local destructor list %p",
533 (
unsigned int)pthread_self(), list);
536 (void) pthread_key_create(&list->
key, _thread_local_pthread_free);
545 (void) pthread_setspecific(list->
key, list);
546 talloc_set_destructor(list, _thread_local_list_free);
555 pthread_mutex_lock(&fr_atexit_global_mutex);
561 pthread_mutex_unlock(&fr_atexit_global_mutex);
587 unsigned int count = 0;
594 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
596 ATEXIT_DEBUG(
"%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
598 (
unsigned int)pthread_self(),
602 talloc_set_destructor(disarm, NULL);
623 ATEXIT_DEBUG(
"%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
625 (
unsigned int)pthread_self(),
627 talloc_set_destructor(e, NULL);
649 unsigned int count = 0;
667 if (!e->
func)
continue;
672 ATEXIT_DEBUG(
"%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
674 (
unsigned int)pthread_self(),
675 list, ee, ee->func, ee->uctx, ee->file, ee->line);
684 to_free->func, to_free->uctx,
685 to_free->file, to_free->line
698bool fr_atexit_thread_is_exiting(
void)
bool fr_atexit_thread_local_alloc_disabled(void)
Has fr_atexit_thread_local_disable_alloc been called yet.
void fr_atexit_thread_local_disable_alloc(void)
Disable lazy allocation of thread-local caches for the rest of the process.
unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
Remove a specific global destructor (without executing it)
static fr_atexit_list_t * fr_atexit_global
char const * file
File where this exit handler was added.
#define CHECK_GLOBAL_SETUP()
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
fr_dlist_t entry
Entry in the handler dlist.
static _Thread_local bool thread_is_exiting
pthread_key_t key
Key used to trigger thread local destructors.
fr_atexit_t func
Function to call.
#define ATEXIT_DEBUG(...)
fr_dlist_head_t head
Head of the list of destructors.
static fr_atexit_entry_t * atexit_entry_alloc(char const *file, int line, fr_atexit_list_t *list, fr_atexit_t func, void const *uctx)
Allocate a new exit handler entry.
void fr_atexit_global_disarm_all(void)
Remove all global destructors (without executing them)
fr_atexit_entry_t * e
Inserted into the global exit handler list to ensure this memory is cleaned up.
static int _atexit_entry_free(fr_atexit_entry_t *e)
Call the exit handler.
static atomic_bool thread_local_alloc_disabled
Latched once main has decided no more thread-local pools should be handed out.
int line
Line where this exit handler was added.
static void _global_free(void)
Free any thread-local exit handler lists that pthread_key failed to fre.
bool fr_atexit_is_exiting(void)
Return whether we're currently in the teardown phase.
static int _destructor_list_free(fr_atexit_list_t *list)
Talloc destructor for freeing list elements in order.
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
int fr_atexit_trigger(bool uctx_scope, fr_atexit_t func, void const *uctx)
Iterates through all thread local destructor lists, causing destructor to be triggered.
int _atexit_global(char const *file, int line, fr_atexit_t func, void const *uctx)
Add a free function to be called when the process exits.
fr_atexit_list_t * list
List this entry is in.
Entry in exit handler list.
Head of a list of exit handlers.
int(* fr_atexit_t)(void *uctx)
Destructor callback.
#define fr_atexit_thread_trigger_all(...)
#define fr_atexit_thread_local_disarm(...)
#define fr_atexit_thread_local_disarm_all(...)
#define fr_atexit_thread_local(_name, _free, _uctx)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
static bool fr_dlist_entry_in_list(fr_dlist_t const *entry)
Check if a list entry is part of a list.
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
static int fr_dlist_insert_head(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the head of a list.
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Head of a doubly linked list.
Entry in a doubly linked list.
#define atomic_load_explicit(object, order)
#define atomic_store_explicit(object, desired, order)
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.