]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
libgettextpo: Make function po_format_list() multithread-safe.
authorBruno Haible <bruno@clisp.org>
Sun, 4 Aug 2024 23:32:32 +0000 (01:32 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 4 Aug 2024 23:32:32 +0000 (01:32 +0200)
* 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.

autogen.sh
gettext-tools/libgettextpo/gettext-po.c

index 0e7f34155695b62779d340b7d47666903735a850..e4d6d906506e763f4c906e92138e6aec2d3bb49f 100755 (executable)
@@ -375,6 +375,7 @@ if ! $skip_gnulib; then
     markup
     mem-hash-map
     minmax
+    once
     open
     relocatable-lib
     sigpipe
index 22a50c1dfc4a16bf329c5adcc95f62ed6a94e0ee..25a9ee14f772ffab8cb7ed78a04fcc094280d000 100644 (file)
@@ -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;
 }