From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id C200F396ECD2 for ; Fri, 27 Nov 2020 08:43:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C200F396ECD2 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-201-CFd8jURUPE237Sb5_T7Lxg-1; Fri, 27 Nov 2020 03:43:49 -0500 X-MC-Unique: CFd8jURUPE237Sb5_T7Lxg-1 Received: by mail-wr1-f72.google.com with SMTP id k1so3156245wrg.12 for ; Fri, 27 Nov 2020 00:43:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=mHW+v1ibBNDkmafLnpqVroYXz6p+EGKqWHX7IbiGkTA=; b=NVCGgML/qoBy2pLEff9cFxSNkir15BFoNFdVeTczsIun28ZzyRe/garEtu88z+4Q+l 1wwjGtGs520pRQApeImfHhoEq3AzkeNO8/Z+ZdlVUuhJWUGbansam6RkQxp0FO83KyZ0 +0jllxlEVJzz5f/Gd3DsPI+jUHA0ZpFSCfqU0Lm1BRBXGgIbCUKfS6rVG7p69xeUMtq4 f1t0K6LIxsbUkYlWg7T7paH6Dgv38p+JWaiv64451XzjjM0CFbkRJqaTpmwwSVyBZ0FT gv/i7pJkaCTgqE4N7GrpZ1WP55DSJIhuEWJnH2eQzoIoHquC3XgWzVgfLQdjBkc83tMA A2Qg== X-Gm-Message-State: AOAM530LSJ9BSEps9qXLe24MRUW6slbp6+CJ6S6YWT1HN6c53YAV9LTw c6YSr66DSjDaofnJX1IQ2A/B5lSnNrcfYOXh25RHFfmdihmyrTt1QTDpoF02F5MFab8eKXHlyW0 hWBGCnmGR70cokng= X-Received: by 2002:adf:ed51:: with SMTP id u17mr9252648wro.61.1606466628679; Fri, 27 Nov 2020 00:43:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzaWKOK6lAfwCq1UVTDac8RTYfjFNE28ryUotG7MVZKch6PMO7Wc/djKN851KLTmVtYi/KGBw== X-Received: by 2002:adf:ed51:: with SMTP id u17mr9252624wro.61.1606466628456; Fri, 27 Nov 2020 00:43:48 -0800 (PST) Received: from [192.168.188.47] (dynamic-077-003-206-224.77.3.pool.telefonica.de. [77.3.206.224]) by smtp.gmail.com with ESMTPSA id a1sm13502803wrv.61.2020.11.27.00.43.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 27 Nov 2020 00:43:47 -0800 (PST) Subject: Re: [committed] libstdc++: Use __libc_single_threaded for locale initialization To: Jonathan Wakely , libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org References: <20201124145952.GA1857606@redhat.com> From: Stephan Bergmann Message-ID: Date: Fri, 27 Nov 2020 09:43:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 In-Reply-To: <20201124145952.GA1857606@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 27 Nov 2020 08:44:03 -0000 On 24/11/2020 15:59, Jonathan Wakely via Gcc-patches wrote: > Most initialization of locales and facets happens before main() during > startup, when the program is likely to only have one thread. By using > the new __gnu_cxx::__is_single_threaded() function instead of checking > __gthread_active_p() we can avoid using pthread_once or atomics for the > common case. > > That said, I'm not sure why we don't just use a local static variable > instead, as __cxa_guard_acquire() already optimizes for the > single-threaded case: > > static const bool init = (_S_initialize_once(), true); > > I'll revisit that for GCC 12. > > libstdc++-v3/ChangeLog: > > * src/c++98/locale.cc (locale::facet::_S_get_c_locale()) > (locale::id::_M_id() const): Use __is_single_threaded. > * src/c++98/locale_init.cc (locale::_S_initialize()): > Likewise. > > Tested powerpc64le-linux. Committed to trunk. I now started to get weird crashes when running LibreOffice test code at least with Clang -fsanitize=address and latest libstdc++, where the Clang ASan machinery SEGVs while it wants to report some malloc/free issue. That goes away when reverting this commit, and I think the root cause is that locale::facet::_S_initialize_once() now gets called twice: First during __cxx_global_var_init when the process is still single threaded (so if (!__gnu_cxx::__is_single_threaded()) in locale::facet::_S_get_c_locale, reading __libc_single_threaded, is false, whereas if (__gthread_active_p()) would have been true even if the process still only had a single thread). And again after the process has spawned further threads via pthread_create (flipping __libc_single_threaded) and calls std::ostringstream() -> ... std::locale() -> ..., at which point if (!__gnu_cxx::__is_single_threaded()) in locale::facet::_S_get_c_locale is true now.