From: Bruno Haible Date: Sun, 4 Aug 2024 23:32:32 +0000 (+0200) Subject: libgettextpo: Make function po_format_list() multithread-safe. X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=afc3f4029a71266d9e105d7514a4f9dc35bc93bf;p=thirdparty%2Fgettext.git libgettextpo: Make function po_format_list() multithread-safe. * autogen.sh (GNULIB_MODULES_LIBGETTEXTPO): Add 'once'. * gettext-tools/libgettextpo/gettext-po.c: Include glthread/once.h. (all_formats): New variable. (all_formats_init): New function, extracted from po_format_list. (all_formats_init_once): New variable. (po_format_list): Invoke all_formats_init once. --- diff --git a/autogen.sh b/autogen.sh index 0e7f34155..e4d6d9065 100755 --- a/autogen.sh +++ b/autogen.sh @@ -375,6 +375,7 @@ if ! $skip_gnulib; then markup mem-hash-map minmax + once open relocatable-lib sigpipe diff --git a/gettext-tools/libgettextpo/gettext-po.c b/gettext-tools/libgettextpo/gettext-po.c index 22a50c1df..25a9ee14f 100644 --- a/gettext-tools/libgettextpo/gettext-po.c +++ b/gettext-tools/libgettextpo/gettext-po.c @@ -35,6 +35,7 @@ #include "write-po.h" #include "xvasprintf.h" #include "msgl-check.h" +#include "glthread/once.h" #include "gettext.h" #define _(str) gettext(str) @@ -1039,22 +1040,30 @@ po_filepos_start_line (po_filepos_t filepos) } +/* A NULL terminated array of the supported format types. */ +static const char * const * all_formats; + +static void +all_formats_init (void) +{ + const char **list = XNMALLOC (NFORMATS + 1, const char *); + size_t i; + for (i = 0; i < NFORMATS; i++) + list[i] = xasprintf ("%s-format", format_language[i]); + list[i] = NULL; + all_formats = list; +} + +/* Ensure that all_formats_init is called once only. */ +gl_once_define(static, all_formats_init_once) + /* Return a NULL terminated array of the supported format types. */ const char * const * po_format_list (void) { - static const char * const * whole_list /* = NULL */; - if (whole_list == NULL) - { - const char **list = XNMALLOC (NFORMATS + 1, const char *); - size_t i; - for (i = 0; i < NFORMATS; i++) - list[i] = xasprintf ("%s-format", format_language[i]); - list[i] = NULL; - whole_list = list; - } - return whole_list; + gl_once (all_formats_init_once, all_formats_init); + return all_formats; }