]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Just create two fr_atexit_global_once macros
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 12 Jan 2023 20:17:47 +0000 (14:17 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 12 Jan 2023 20:17:47 +0000 (14:17 -0600)
One that returns values, one that doesn't

src/lib/server/module.c
src/lib/util/atexit.h
src/lib/util/event.c
src/lib/util/regex.c

index c0bf9391247ded6bda36f219da60830a4a4b26a6..85632c45b9d5cc8e778698f9c97b89096355cb29 100644 (file)
@@ -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));
 }
index 1b1bd4fdc4a25a0b604e01f0b9b0096cdb1220b9..eb47c7f410a93b1e74cc7c2f270d7f35056fba96 100644 (file)
@@ -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
index 8afdf5b2e7571e8a3a53dbb2129a47f4394f0a06..f267d429e58620861e63589324d9d8aac68a428d 100644 (file)
@@ -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);
index 2807b0f4a1639eaf36df7c6d8f4898d6e81e378a..f7a211712c4b0b844375ee2355424dbe3db1276b 100644 (file)
@@ -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;