changeset 17742:b478eeaef4a3

localename: make gl_locale_name_thread really thread-safe on Windows * lib/localename.c [WINDOWS_NATIVE && !IN_LIBINTL]: Include "glthread/lock.h". (get_lcid_lock) [WINDOWS_NATIVE]: New variable. (get_lcid) [WINDOWS_NATIVE]: Lock while looking for an LCID.
author Daiki Ueno <ueno@gnu.org>
date Thu, 07 Aug 2014 08:46:52 +0900
parents 82713b8ea002
children d64a425dcfe5
files ChangeLog lib/localename.c
diffstat 2 files changed, 22 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-08-07  Daiki Ueno  <ueno@gnu.org>
+
+	localename: make gl_locale_name_thread really thread-safe on Windows
+	* lib/localename.c [WINDOWS_NATIVE && !IN_LIBINTL]: Include
+	"glthread/lock.h".
+	(get_lcid_lock) [WINDOWS_NATIVE]: New variable.
+	(get_lcid) [WINDOWS_NATIVE]: Lock while looking for an LCID.
+
 2014-08-07  Paul Eggert  <eggert@cs.ucla.edu>
 
 	getpass: don't assume struct termios
--- a/lib/localename.c
+++ b/lib/localename.c
@@ -55,6 +55,9 @@
 
 #if defined _WIN32 || defined __WIN32__
 # define WINDOWS_NATIVE
+# if !defined IN_LIBINTL
+#  include "glthread/lock.h"
+# endif
 #endif
 
 #if defined WINDOWS_NATIVE || defined __CYGWIN__ /* Native Windows or Cygwin */
@@ -2542,6 +2545,9 @@
   return TRUE;
 }
 
+/* This lock protects the get_lcid against multiple simultaneous calls.  */
+gl_lock_define_initialized(static, get_lcid_lock)
+
 /* Return the Locale ID (LCID) number given the locale's name, a
    string, in LOCALE_NAME.  This works by enumerating all the locales
    supported by the system, until we find one whose name matches
@@ -2553,8 +2559,14 @@
   static LCID last_lcid;
   static char last_locale[1000];
 
+  /* Lock while looking for an LCID, to protect access to static
+     variables: last_lcid, last_locale, found_lcid, and lname.  */
+  gl_lock_lock (get_lcid_lock);
   if (last_lcid > 0 && strcmp (locale_name, last_locale) == 0)
-    return last_lcid;
+    {
+      gl_lock_unlock (get_lcid_lock);
+      return last_lcid;
+    }
   strncpy (lname, locale_name, sizeof (lname) - 1);
   lname[sizeof (lname) - 1] = '\0';
   found_lcid = 0;
@@ -2564,6 +2576,7 @@
       last_lcid = found_lcid;
       strcpy (last_locale, locale_name);
     }
+  gl_lock_unlock (get_lcid_lock);
   return found_lcid;
 }