From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 9CD2F1F463 for ; Wed, 18 Dec 2019 10:46:25 +0000 (UTC) Received: from localhost ([::1]:52342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihWqR-00066q-Ct for normalperson@yhbt.net; Wed, 18 Dec 2019 05:46:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:59409) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ihWqM-00060A-22 for bug-gnulib@gnu.org; Wed, 18 Dec 2019 05:46:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ihWqK-0006uT-IO for bug-gnulib@gnu.org; Wed, 18 Dec 2019 05:46:13 -0500 Received: from mo6-p00-ob.smtp.rzone.de ([2a01:238:20a:202:5300::5]:17140) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ihWqJ-0006lH-TV for bug-gnulib@gnu.org; Wed, 18 Dec 2019 05:46:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1576665968; s=strato-dkim-0002; d=clisp.org; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=tbweAWOkMKlqTurhfoNvez74vlZTlwroq0rK29oj+Q0=; b=mz+ahUovVVgNM5kEabwGnw2CMIBoemFmUAXfnQqvC05arj6CFbbD1R3nLuHaY6RDYr iM03Ap7gjLbwJVnZQj7MsKLrwaAEmtskpEIvi/I+OxkTrmjY03unaSnNLUP00HBgNT5W NzSIgR+ZJWLT/vbW1QiH46/H4A5SsA18cf0Ky276fkF1/GUXNlkQKBEqMzBVluPqqx1l 1q7PPNS4fBBsbdQoFk3QCWArfMtF+oMd9tzMuaAvm3Zi8fBPBHE6HlXSNrAfHkWQbpZf ncaoshkP3srhNZx05PNYAOFxT66gg+LRkoExJLVgh+Js5HreSe5gKH8ZIDuV6SkNb+in krsQ== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOH6fzxfs=" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 46.0.7 DYNA|AUTH) with ESMTPSA id t0ad5bvBIAk85AT (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve X9_62_prime256v1 with 256 ECDH bits, eq. 3072 bits RSA)) (Client did not present a certificate); Wed, 18 Dec 2019 11:46:08 +0100 (CET) From: Bruno Haible To: Paul Eggert Subject: Re: hard-locale: make multithread-safe Date: Wed, 18 Dec 2019 11:46:07 +0100 Message-ID: <2129360.Yuudax7xD6@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-166-generic; KDE/5.18.0; x86_64; ; ) In-Reply-To: <09a43701-a998-5c26-ea9e-51c8c3446084@cs.ucla.edu> References: <175192568.e2XXTFFdkW@omega> <09a43701-a998-5c26-ea9e-51c8c3446084@cs.ucla.edu> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a01:238:20a:202:5300::5 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: bug-gnulib@gnu.org Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" 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 hard-locale: Make multithread-safe. * lib/hard-locale.h (hard_locale): Move documentation to here. * lib/hard-locale.c: Don't include . (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 -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 -#include #include -#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@