Update the returned value after locale changes. (#160271, Christian
authorMatthias Clasen <mclasen@redhat.com>
Tue, 7 Dec 2004 15:20:15 +0000 (15:20 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Tue, 7 Dec 2004 15:20:15 +0000 (15:20 +0000)
2004-12-07  Matthias Clasen  <mclasen@redhat.com>

* glib/gutils.c (g_get_language_names): Update the returned
value after locale changes.  (#160271, Christian Persch)
(_g_utils_thread_init): Initialize the language name cache
before going threaded.

* glib/gthread.c (g_thread_init_glib): Call _g_utils_thread_init().

* glib/gthreadinit.h: Add _g_utils_thread_init().

ChangeLog
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-6
ChangeLog.pre-2-8
glib/gthread.c
glib/gthreadinit.h
glib/gutils.c

index 59ab5ae0fe81ea5015f9b6c281471f604d669bad..467eec5691f01dfe0399a77c587f55268f430815 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2004-12-07  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/gutils.c (g_get_language_names): Update the returned
+       value after locale changes.  (#160271, Christian Persch)
+       (_g_utils_thread_init): Initialize the language name cache
+       before going threaded.
+
+       * glib/gthread.c (g_thread_init_glib): Call _g_utils_thread_init().
+
+       * glib/gthreadinit.h: Add _g_utils_thread_init().
+
 2004-12-06  Tor Lillqvist  <tml@iki.fi>
 
        * glib/giowin32.c (g_io_win32_finalize): Fix a write to freed
index 59ab5ae0fe81ea5015f9b6c281471f604d669bad..467eec5691f01dfe0399a77c587f55268f430815 100644 (file)
@@ -1,3 +1,14 @@
+2004-12-07  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/gutils.c (g_get_language_names): Update the returned
+       value after locale changes.  (#160271, Christian Persch)
+       (_g_utils_thread_init): Initialize the language name cache
+       before going threaded.
+
+       * glib/gthread.c (g_thread_init_glib): Call _g_utils_thread_init().
+
+       * glib/gthreadinit.h: Add _g_utils_thread_init().
+
 2004-12-06  Tor Lillqvist  <tml@iki.fi>
 
        * glib/giowin32.c (g_io_win32_finalize): Fix a write to freed
index 59ab5ae0fe81ea5015f9b6c281471f604d669bad..467eec5691f01dfe0399a77c587f55268f430815 100644 (file)
@@ -1,3 +1,14 @@
+2004-12-07  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/gutils.c (g_get_language_names): Update the returned
+       value after locale changes.  (#160271, Christian Persch)
+       (_g_utils_thread_init): Initialize the language name cache
+       before going threaded.
+
+       * glib/gthread.c (g_thread_init_glib): Call _g_utils_thread_init().
+
+       * glib/gthreadinit.h: Add _g_utils_thread_init().
+
 2004-12-06  Tor Lillqvist  <tml@iki.fi>
 
        * glib/giowin32.c (g_io_win32_finalize): Fix a write to freed
index 59ab5ae0fe81ea5015f9b6c281471f604d669bad..467eec5691f01dfe0399a77c587f55268f430815 100644 (file)
@@ -1,3 +1,14 @@
+2004-12-07  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/gutils.c (g_get_language_names): Update the returned
+       value after locale changes.  (#160271, Christian Persch)
+       (_g_utils_thread_init): Initialize the language name cache
+       before going threaded.
+
+       * glib/gthread.c (g_thread_init_glib): Call _g_utils_thread_init().
+
+       * glib/gthreadinit.h: Add _g_utils_thread_init().
+
 2004-12-06  Tor Lillqvist  <tml@iki.fi>
 
        * glib/giowin32.c (g_io_win32_finalize): Fix a write to freed
index 59ab5ae0fe81ea5015f9b6c281471f604d669bad..467eec5691f01dfe0399a77c587f55268f430815 100644 (file)
@@ -1,3 +1,14 @@
+2004-12-07  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/gutils.c (g_get_language_names): Update the returned
+       value after locale changes.  (#160271, Christian Persch)
+       (_g_utils_thread_init): Initialize the language name cache
+       before going threaded.
+
+       * glib/gthread.c (g_thread_init_glib): Call _g_utils_thread_init().
+
+       * glib/gthreadinit.h: Add _g_utils_thread_init().
+
 2004-12-06  Tor Lillqvist  <tml@iki.fi>
 
        * glib/giowin32.c (g_io_win32_finalize): Fix a write to freed
index 7feaf9850dd86a07db5f6a9e3775c277b936219e..fe69dfb4c043ccf75a757ea8358c747a30b40a77 100644 (file)
@@ -152,6 +152,7 @@ g_thread_init_glib (void)
   _g_mem_thread_init ();
   _g_messages_thread_init ();
   _g_atomic_thread_init ();
+  _g_utils_thread_init ();
 #ifdef G_OS_WIN32
   _g_win32_thread_init ();
 #endif
index 34fa1f3b3fa1ca2c34d1d38ad0bb02b4376500e2..5082cf852b834b71095e12b8b83d6cd7eea70bdf 100644 (file)
@@ -33,6 +33,7 @@ void _g_convert_thread_init (void) G_GNUC_INTERNAL;
 void _g_rand_thread_init (void) G_GNUC_INTERNAL;
 void _g_main_thread_init (void) G_GNUC_INTERNAL;
 void _g_atomic_thread_init (void) G_GNUC_INTERNAL;
+void _g_utils_thread_init (void) G_GNUC_INTERNAL;
 #ifdef G_OS_WIN32
 void _g_win32_thread_init (void) G_GNUC_INTERNAL;
 #endif
index 1c2523f2a3ff3874c2f8506f1317c5bf2ea5b4cf..570a818bb3a58a899b0a31bcc7198a80d4771cb9 100644 (file)
@@ -54,6 +54,7 @@
 #include "galias.h"
 #include "glib.h"
 #include "gprintfint.h"
+#include "gthreadinit.h"
 
 #ifdef MAXPATHLEN
 #define        G_PATH_LENGTH   MAXPATHLEN
@@ -1919,7 +1920,21 @@ guess_category_value (const gchar *category_name)
   return NULL;
 }
 
-static gchar **languages = NULL;
+typedef struct _GLanguageNamesCache GLanguageNamesCache;
+
+struct _GLanguageNamesCache {
+  gchar *languages;
+  gchar **language_names;
+};
+
+static void
+language_names_cache_free (gpointer data)
+{
+  GLanguageNamesCache *cache = data;
+  g_free (cache->languages);
+  g_strfreev (cache->language_names);
+  g_free (cache);
+}
 
 /**
  * g_get_language_names:
@@ -1944,18 +1959,30 @@ static gchar **languages = NULL;
 G_CONST_RETURN gchar * G_CONST_RETURN * 
 g_get_language_names (void)
 {
-  G_LOCK (g_utils_global);
+  static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
+  GLanguageNamesCache *cache = g_static_private_get (&cache_private);
+  const gchar *value;
+
+  if (!cache)
+    {
+      cache = g_new0 (GLanguageNamesCache, 1);
+      g_static_private_set (&cache_private, cache, language_names_cache_free);
+    }
+
+  value = guess_category_value ("LC_MESSAGES");
+  if (!value)
+    value = "C";
 
-  if (!languages)
+  if (!(cache->languages && strcmp (cache->languages, value) == 0))
     {
-      const gchar *value;
+      gchar **languages;
       gchar **alist, **a;
       GSList *list, *l;
       gint i;
 
-      value = guess_category_value ("LC_MESSAGES");
-      if (!value)
-       value = "C";
+      g_free (cache->languages);
+      g_strfreev (cache->language_names);
+      cache->languages = g_strdup (value);
 
       alist = g_strsplit (value, ":", 0);
       list = NULL;
@@ -1965,9 +1992,9 @@ g_get_language_names (void)
          list = g_slist_concat (list, _g_compute_locale_variants (b));
        }
       g_strfreev (alist);
-      list = g_slist_append (list, "C");
+      list = g_slist_append (list, g_strdup ("C"));
 
-      languages = g_new (gchar *, g_slist_length (list) + 1);
+      cache->language_names = languages = g_new (gchar *, g_slist_length (list) + 1);
       for (l = list, i = 0; l; l = l->next, i++)
        languages[i] = l->data;
       languages[i] = NULL;
@@ -1975,9 +2002,7 @@ g_get_language_names (void)
       g_slist_free (list);
     }
 
-  G_UNLOCK (g_utils_global);
-
-  return (G_CONST_RETURN gchar * G_CONST_RETURN *) languages;
+  return (G_CONST_RETURN gchar * G_CONST_RETURN *) cache->language_names;
 }
 
 guint
@@ -2038,6 +2063,15 @@ g_get_codeset (void)
   return g_strdup (charset);
 }
 
+/* This is called from g_thread_init(). It's used to
+ * initialize some static data in a threadsafe way.
+ */
+void
+_g_utils_thread_init (void)
+{
+  g_get_language_names ();
+}
+
 #ifdef ENABLE_NLS
 
 #include <libintl.h>