Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdkzD-000epm-Mk for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Aug 2024 06:26:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sdkzC-0024Pf-6K for pgsql-hackers@arkaria.postgresql.org; Tue, 13 Aug 2024 06:26:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdkw7-0021UU-HI for pgsql-hackers@lists.postgresql.org; Tue, 13 Aug 2024 06:23:15 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sdkw4-004bYx-PD for pgsql-hackers@postgresql.org; Tue, 13 Aug 2024 06:23:14 +0000 Received: from [192.168.1.110] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4WjhCL631Hz49PxX; Tue, 13 Aug 2024 09:23:10 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1723530191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pe9IHkRQoot/O9S9u5Aku0pe5+wSWpSBEvC0os1a7HU=; b=YBwWz/lRbtyGQyPkprvvvN6xNNEMhawo4cm0bac5FucjNW8KF42ekCJ+ksjvOrCMcRT62w e8UuQgt/xH6hYNu0NerZU8LybsOzHNg1S6DzGwbhTJyqZaJl4Es2m2Bn0S6F0rPfhjl81L Hx3Er+VoaQr8bc65BLMSRNwOD/oWCtCURQWoj3j5rUWLxbnWmo2PQcZFdKSV6r0ouxb8o/ nEn/7EQFfQ0h7N3BQn+fPuX1WJ4vkLXUg1SXlibf4qbjMvoVHqa4O+p8ImXUAmMocmb8hQ O63rjNfNJObV2q8mHA3f7CBayOOeR/Wt9y71ECy2/ZOGmlgK2r3SkTIK8Y2Wqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1723530191; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pe9IHkRQoot/O9S9u5Aku0pe5+wSWpSBEvC0os1a7HU=; b=j3HlasjIK5UBx3s4QGoOfyHlYrL2tSljIuWqSZdORjCftzSA+Ot4IXncpac00IVKj+AI+7 sbnq4//kYWBLhknh7l89Io66zGL191zfLNUky9v+ndZN86IqKdK/ARuh8JGfddGW40Iyli WSmYPoSK953R9eqyk5td1GGaOORvzaU3QZVq72KtNEM5wm9hMWdQxtfe4/ka1+yvmUpUL5 wemYXX5AOTs1FH8qH42ehocf1OywKyeUaDBP+YHiUzglNE2xPz0VQ9LhPGwsKBAxtOBT5o 1Z9wkH8JkXSFvfz5h4meK9TxLSWQHhBJnvjFB5nhRTGPz+qhoFulZiqt3/6O8g== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1723530191; a=rsa-sha256; cv=none; b=oNqtkETzaX7qksFNAk4e+Zvl2YOSzQBnYkL95us4SAnrxfK+rZZxr1dnU3T1uqywizgZvu J3tMDOzgZ2S2XIFehWlI0hl5AQGrrFFgXmtZbWieffBUJo6QJpn97V9G0xQItjolTfs5cr na48emoqM8V0/w3NmnyIoEUZYalYn0Ty3rz71zSo1HiUoZsKmuIJytruoOvRALbBNSBhZz wpqg/JvOo9ZcoRUOnwhsQ3FqUEJ+qmJ78M264RseuodpKJ4KvwYpYjao5lUvPWubIJ7037 W4qLP4D+LhY5iZN3vuUCrMKIyy2cuHjhyXA6llC2JFMigFLVGZh2Z/3YJRoMog== Message-ID: <32e1b3e0-590c-4eac-852e-f681ee36594a@iki.fi> Date: Tue, 13 Aug 2024 09:23:10 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Thread-safe nl_langinfo() and localeconv() To: Thomas Munro , pgsql-hackers References: Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 13/08/2024 08:45, Thomas Munro wrote: > Hi, > > Over on the discussion thread about remaining setlocale() work[1], I > wrote out some long boring theories about $SUBJECT. Here are some > draft patches to try those theories out, and make a commitfest entry. > nl_langinfo_l() is a trivial drop-in replacement, and > pg_localeconv_r() has 4 different implementation strategies: > > 1. Windows, with ugly _configthreadlocale() and thread-local result. > 2. Glibc, with nice nl_langinfo_l() extensions. > 3. macOS/*BSD, with nice localeconv_l(). > 4. Baseline POSIX: uselocale() + localeconv() + honking great lock. > > In reality it'd just be Solaris running #4 (and AIX if it comes back). > Whether they truly implement it as pessimally as the standard allows, > who knows... you could drop the lock if you somehow knew that they > returned a pointer to thread-local storage or a member of the locale_t > object. Patches 1 and 2 look good to me. Patch 3 makes sense too, some comments on the details: The #ifdefs and the LCONV_MEMBER stuff makes it a bit hard to follow what happens in each implementation strategy. I wonder if it would be more clear to duplicate more code. There's a comment at the top of pg_locale.c ("!!! NOW HEAR THIS !!!") that needs to be removed or adjusted now. > * The POSIX standard explicitly says that it is undefined what happens if > * LC_MONETARY or LC_NUMERIC imply an encoding (codeset) different from > * that implied by LC_CTYPE. In practice, all Unix-ish platforms seem to > * believe that localeconv() should return strings that are encoded in the > * codeset implied by the LC_MONETARY or LC_NUMERIC locale name. Hence, > * once we have successfully collected the localeconv() results, we will > * convert them from that codeset to the desired server encoding. The patch loses this comment, leaving just a much shorter comment in the WIN32 implementation. But it still seems like a relevant comment for the !WIN32 implementation too. > This gets rid of some setlocale() calls and makes the returned value > unclobberable with a defined lifetime. The remaining call to > setlocale() is only a query of the name of the current local (in a typo: local -> locale > multi-threaded future this would have to be changed, perhaps to use a > per-database or per-backend locale_t instead of LC_GLOBAL_LOCALE). > > All known non-Windows targets have nl_langinfo_l(), from POSIX 2018. I think that's supposed to be POSIX 2008 -- Heikki Linnakangas Neon (https://neon.tech)