]> Savannah Git Hosting - gnulib.git/commitdiff
localename: make gl_locale_name_thread really thread-safe on Windows
authorDaiki Ueno <ueno@gnu.org>
Wed, 6 Aug 2014 23:46:52 +0000 (08:46 +0900)
committerDaiki Ueno <ueno@gnu.org>
Fri, 8 Aug 2014 03:17:42 +0000 (12:17 +0900)
* 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.

ChangeLog
lib/localename.c

index f5fdea6b405df3f48084d6c775ec68c7e4f9b12b..7d4fc5cd42dad5d8e8e720787b877b1c43822481 100644 (file)
--- 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
index 4cce060efd4396a95bd245d91b85e65568f7c1dc..78dc344bba191417855670fb751210d3608db6e6 100644 (file)
@@ -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 @@ enum_locales_fn (LPTSTR locale_num_str)
   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 @@ get_lcid (const char *locale_name)
   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 @@ get_lcid (const char *locale_name)
       last_lcid = found_lcid;
       strcpy (last_locale, locale_name);
     }
+  gl_lock_unlock (get_lcid_lock);
   return found_lcid;
 }