From: Daiki Ueno Date: Wed, 6 Aug 2014 23:46:52 +0000 (+0900) Subject: localename: make gl_locale_name_thread really thread-safe on Windows X-Git-Tag: v1.0~7316 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=f089beeaec517618a8f1afe580aa3ed99fc01550;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index f5fdea6b40..7d4fc5cd42 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2014-08-07 Daiki Ueno + + 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 getpass: don't assume struct termios diff --git a/lib/localename.c b/lib/localename.c index 4cce060efd..78dc344bba 100644 --- 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 @@ 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; }