27RCSID(
"$Id: 47f0261a68d4435fc31f0afb4871471a5ec49a5b $")
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);
193 if (
unlikely(!fr_atexit_threads))
return -1;
195 ATEXIT_DEBUG(
"%s - Alloced threads destructor list %p", __FUNCTION__, fr_atexit_threads);
207#define CHECK_GLOBAL_SETUP() \
210 pthread_mutex_lock(&fr_atexit_global_mutex); \
211 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
212 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
213 pthread_mutex_unlock(&fr_atexit_global_mutex); \
214 if (_ret < 0) return _ret; \
217#define CHECK_GLOBAL_SETUP() \
220 fr_cond_assert_msg(!is_exiting, "New atexit handlers should not be allocated whilst exiting"); \
221 if (!fr_atexit_global) _ret = fr_atexit_global_setup(); \
222 if (_ret < 0) return _ret; \
251 unsigned int count = 0;
256 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
258 ATEXIT_DEBUG(
"%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
264 talloc_set_destructor(disarm, NULL);
285 ATEXIT_DEBUG(
"%s - Disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
289 talloc_set_destructor(e, NULL);
308 unsigned int count = 0;
316 ATEXIT_DEBUG(
"%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
327 to_free->func, to_free->uctx,
328 to_free->file, to_free->line);
357 unsigned int count = 0;
365 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
367 ATEXIT_DEBUG(
"%s - Triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
378 to_free->func, to_free->uctx,
379 to_free->file, to_free->line);
387 if (!fr_atexit_threads)
return 0;
395 if (!e->
func)
continue;
400 if ((ee->
func != func) || ((ee->
uctx != uctx) && uctx_scope))
continue;
402 ATEXIT_DEBUG(
"%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
404 (
unsigned int)pthread_self(),
414 to_free->func, to_free->uctx,
415 to_free->file, to_free->line);
455 bool save_is_exiting;
457 pthread_mutex_lock(&fr_atexit_global_mutex);
459 pthread_mutex_unlock(&fr_atexit_global_mutex);
461 return save_is_exiting;
473 ATEXIT_DEBUG(
"%s - Freeing _Thread_local destructor list %p", __FUNCTION__, list);
476 pthread_mutex_lock(&fr_atexit_global_mutex);
477 list->
e->
func = NULL;
478 pthread_mutex_unlock(&fr_atexit_global_mutex);
486static void _thread_local_pthread_free(
void *list)
495static int _thread_local_free(
void *list)
507int _fr_atexit_thread_local(
char const *
file,
int line,
526 ATEXIT_DEBUG(
"%s - Thread %u alloced _Thread_local destructor list %p",
528 (
unsigned int)pthread_self(), list);
531 (void) pthread_key_create(&list->
key, _thread_local_pthread_free);
540 (void) pthread_setspecific(list->
key, list);
541 talloc_set_destructor(list, _thread_local_list_free);
550 pthread_mutex_lock(&fr_atexit_global_mutex);
556 pthread_mutex_unlock(&fr_atexit_global_mutex);
582 unsigned int count = 0;
589 if ((e->
func != func) || ((e->
uctx != uctx) && uctx_scope))
continue;
591 ATEXIT_DEBUG(
"%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
593 (
unsigned int)pthread_self(),
597 talloc_set_destructor(disarm, NULL);
618 ATEXIT_DEBUG(
"%s - Thread %u disarming %p/%p func=%p, uctx=%p (alloced %s:%d)",
620 (
unsigned int)pthread_self(),
622 talloc_set_destructor(e, NULL);
644 unsigned int count = 0;
662 if (!e->
func)
continue;
667 ATEXIT_DEBUG(
"%s - Thread %u triggering %p/%p func=%p, uctx=%p (alloced %s:%d)",
669 (
unsigned int)pthread_self(),
670 list, ee, ee->func, ee->uctx, ee->file, ee->line);
679 to_free->func, to_free->uctx,
680 to_free->file, to_free->line
693bool 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.