bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: bug-gnulib@gnu.org
Subject: Re: hard-locale: make multithread-safe
Date: Wed, 18 Dec 2019 11:46:07 +0100	[thread overview]
Message-ID: <2129360.Yuudax7xD6@omega> (raw)
In-Reply-To: <09a43701-a998-5c26-ea9e-51c8c3446084@cs.ucla.edu>

Paul Eggert wrote:
> Thanks, this change looks fine to me.

I'm pushing it. Tested: on all platforms, the unit test still passes.
Additionally, the test failure on Android is gone.

It adds a new link requirement for the users of this module. It's
necessary because on AIX, -lpthread is necessary for an MT-safe
setlocale_null and therefore also for 'hard-locale'.


2019-12-18  Bruno Haible  <bruno@clisp.org>

	hard-locale: Make multithread-safe.
	* lib/hard-locale.h (hard_locale): Move documentation to here.
	* lib/hard-locale.c: Don't include <stdlib.h>.
	(GLIBC_VERSION): Remove macro.
	(hard_locale): Assume that all systems name the "C" and "POSIX" locales
	"C" or "POSIX". Invoke setlocale_null instead of setlocale.
	* modules/hard-locale (Depends-on): Remove strdup. Add setlocale-null.
	(configure.ac): Require gl_FUNC_SETLOCALE_NULL. Set LIB_HARD_LOCALE.
	(Link): New section.
	* modules/hard-locale-tests (Makefile.am): Link test-hard-locale against
	$(LIB_HARD_LOCALE).

diff --git a/lib/hard-locale.h b/lib/hard-locale.h
index 8f1da96..160674b 100644
--- a/lib/hard-locale.h
+++ b/lib/hard-locale.h
@@ -20,6 +20,9 @@
 
 # include <stdbool.h>
 
-bool hard_locale (int);
+/* Return true if the specified CATEGORY of the current locale is hard, i.e.
+   different from the C or POSIX locale that has a fixed behavior.
+   CATEGORY must be one of the LC_* values, but not LC_ALL.  */
+extern bool hard_locale (int category);
 
 #endif /* HARD_LOCALE_H_ */
diff --git a/lib/hard-locale.c b/lib/hard-locale.c
index dcfcad6..4a2adab 100644
--- a/lib/hard-locale.c
+++ b/lib/hard-locale.c
@@ -21,52 +21,15 @@
 #include "hard-locale.h"
 
 #include <locale.h>
-#include <stdlib.h>
 #include <string.h>
 
-#ifdef __GLIBC__
-# define GLIBC_VERSION __GLIBC__
-#elif defined __UCLIBC__
-# define GLIBC_VERSION 2
-#else
-# define GLIBC_VERSION 0
-#endif
-
-/* Return true if the current CATEGORY locale is hard, i.e. if you
-   can't get away with assuming traditional C or POSIX behavior.  */
 bool
 hard_locale (int category)
 {
-  bool hard = true;
-  char const *p = setlocale (category, NULL);
-
-  if (p)
-    {
-      if (2 <= GLIBC_VERSION)
-        {
-          if (strcmp (p, "C") == 0 || strcmp (p, "POSIX") == 0)
-            hard = false;
-        }
-      else
-        {
-          char *locale = strdup (p);
-          if (locale)
-            {
-              /* Temporarily set the locale to the "C" and "POSIX" locales
-                 to find their names, so that we can determine whether one
-                 or the other is the caller's locale.  */
-              if (((p = setlocale (category, "C"))
-                   && strcmp (p, locale) == 0)
-                  || ((p = setlocale (category, "POSIX"))
-                      && strcmp (p, locale) == 0))
-                hard = false;
+  char locale[SETLOCALE_NULL_MAX];
 
-              /* Restore the caller's locale.  */
-              setlocale (category, locale);
-              free (locale);
-            }
-        }
-    }
+  if (setlocale_null (category, locale, sizeof (locale)))
+    return false;
 
-  return hard;
+  return !(strcmp (locale, "C") == 0 || strcmp (locale, "POSIX") == 0);
 }
diff --git a/modules/hard-locale b/modules/hard-locale
index d4463b7..df07d4a 100644
--- a/modules/hard-locale
+++ b/modules/hard-locale
@@ -7,9 +7,12 @@ lib/hard-locale.c
 
 Depends-on:
 stdbool
-strdup
+setlocale-null
 
 configure.ac:
+AC_REQUIRE([gl_FUNC_SETLOCALE_NULL])
+LIB_HARD_LOCALE="$LIB_SETLOCALE_NULL"
+AC_SUBST([LIB_HARD_LOCALE])
 
 Makefile.am:
 lib_SOURCES += hard-locale.c
@@ -17,6 +20,9 @@ lib_SOURCES += hard-locale.c
 Include:
 "hard-locale.h"
 
+Link:
+$(LIB_HARD_LOCALE)
+
 License:
 LGPLv2+
 
diff --git a/modules/hard-locale-tests b/modules/hard-locale-tests
index cdc084e..a501781 100644
--- a/modules/hard-locale-tests
+++ b/modules/hard-locale-tests
@@ -16,3 +16,4 @@ Makefile.am:
 TESTS += test-hard-locale
 check_PROGRAMS += test-hard-locale
 noinst_PROGRAMS += locale
+test_hard_locale_LDADD = $(LDADD) @LIB_HARD_LOCALE@



  parent reply	other threads:[~2019-12-18 10:46 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-17 13:45 hard-locale: make multithread-safe Bruno Haible
2019-12-17 14:08 ` Tim Rühsen
     [not found]   ` <20191226221225.GA800@HATZ>
2019-12-27 10:51     ` string types Bruno Haible
2019-12-28 13:14       ` ag
2019-12-28 18:28         ` Paul Eggert
2019-12-28 20:44           ` ag
2019-12-28 22:40             ` Paul Eggert
2019-12-29  9:19         ` Bruno Haible
2019-12-29 17:13           ` ag
2019-12-29 20:02           ` ag
2019-12-29 21:24       ` Tim Rühsen
2019-12-31  9:53         ` Bruno Haible
2020-01-06 10:34           ` Tim Rühsen
2020-01-06 12:46             ` Bruno Haible
2020-01-06 16:08               ` Tim Rühsen
2020-01-06 16:49                 ` Tim Rühsen
2019-12-18  1:45 ` hard-locale: make multithread-safe Paul Eggert
2019-12-18  8:51   ` Bruno Haible
2019-12-21  6:33     ` Bruno Haible
2019-12-18 10:29   ` LC_COLLATE in the C locale Bruno Haible
2019-12-18 16:27     ` Paul Eggert
2019-12-18 10:46   ` Bruno Haible [this message]
2019-12-24 23:36     ` hard-locale: make multithread-safe Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2129360.Yuudax7xD6@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).