[glib] gutils: split out g_get_host_name()



commit 3c9691f7f8b3b66905ab689a5dddc6157182b50e
Author: Ryan Lortie <desrt desrt ca>
Date:   Mon Feb 4 13:49:06 2013 +0100

    gutils: split out g_get_host_name()
    
    Remove the code for getting the hostname from g_get_any_init_do() and
    outside of the g_utils_global lock.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=693204

 glib/gutils.c |   33 ++++++++++++++++++---------------
 1 files changed, 18 insertions(+), 15 deletions(-)
---
diff --git a/glib/gutils.c b/glib/gutils.c
index f82ba56..46aee9b 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -581,7 +581,6 @@ static      gchar   *g_tmp_dir = NULL;
 static gchar   *g_user_name = NULL;
 static gchar   *g_real_name = NULL;
 static gchar   *g_home_dir = NULL;
-static gchar   *g_host_name = NULL;
 
 static  gchar   *g_user_data_dir = NULL;
 static  gchar  **g_system_data_dirs = NULL;
@@ -648,8 +647,6 @@ get_windows_directory_root (void)
 static void
 g_get_any_init_do (void)
 {
-  gchar hostname[100];
-
   g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
 
   if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
@@ -879,16 +876,6 @@ g_get_any_init_do (void)
   if (!g_real_name)
     g_real_name = g_strdup ("Unknown");
 
-  {
-#ifndef G_OS_WIN32
-    gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
-#else
-    DWORD size = sizeof (hostname);
-    gboolean hostname_fail = (!GetComputerName (hostname, &size));
-#endif
-    g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
-  }
-
 #ifdef G_OS_WIN32
   g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
   g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
@@ -1042,8 +1029,24 @@ g_get_tmp_dir (void)
 const gchar *
 g_get_host_name (void)
 {
-  g_get_any_init_locked ();
-  return g_host_name;
+  static gchar *hostname;
+
+  if (g_once_init_enter (&hostname))
+    {
+      gboolean failed;
+      gchar tmp[100];
+
+#ifndef G_OS_WIN32
+      failed = (gethostname (tmp, sizeof (tmp)) == -1);
+#else
+      DWORD size = sizeof (tmp);
+      failed = (!GetComputerName (tmp, &size));
+#endif
+
+      g_once_init_leave (&hostname, g_strdup (failed ? "localhost" : tmp));
+    }
+
+  return hostname;
 }
 
 G_LOCK_DEFINE_STATIC (g_prgname);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]