Eli Zaretskii <e...@gnu.org> writes: >> > Actually, I see that libunistring already includes functions for >> > locking (which on Windows wrap the 2 APIs I mentioned above). So I >> > guess it's best to use them. >> >> Yes, could you create a patch in that direction? > > Will do.
Here is a simple patch for this. Is it OK for you?
>From a9576350604e97fc3c3fa16778e6e3d10094f97a Mon Sep 17 00:00:00 2001 From: Daiki Ueno <u...@gnu.org> Date: Thu, 7 Aug 2014 08:46:52 +0900 Subject: [PATCH] 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. --- ChangeLog | 8 ++++++++ lib/localename.c | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 8e74a54..99b0dec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2014-08-07 Daiki Ueno <u...@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-05 Paul Eggert <egg...@cs.ucla.edu> sys_select: fix FD_ZERO problem on Solaris 10 diff --git a/lib/localename.c b/lib/localename.c index 4cce060..78dc344 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; } -- 1.9.3
Regards, -- Daiki Ueno