From patchwork Thu Jun 29 15:20:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 114333 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp9717662vqr; Thu, 29 Jun 2023 08:23:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7kSTn5+1lfcA3ezroNPEmSVSvslXpmD9FEh412ORXGw6PAzbsZ6k68Pfl5fKYHY0h9BE10 X-Received: by 2002:a17:906:7943:b0:987:e23f:6d7a with SMTP id l3-20020a170906794300b00987e23f6d7amr30154350ejo.25.1688052182460; Thu, 29 Jun 2023 08:23:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688052182; cv=none; d=google.com; s=arc-20160816; b=KwWWGnWB4NV2fkh2kcAxuVIOgbfNc+KY9uWzjrfRdJd7UAqMsXG+1INwuiOWSAz9RW zUz+3DMkRlEQkoHEeMSQK6XLjZ2zol3HMI+lqJt2cxPb3ArZOwEDd0FslcjdoTqGKlDb 9VT6Uy9jfQrNHValnShFqdGErKNYm7n7mMguF3aAQ8WU5Sfwhl0eSkUxHAGrNi8KL77b MFUMGyS3fO77Rq0alYvOmcCurMZGSJ+R09fK9bXDmjEMO42JITHNwyNuGsbhXtnQusdz djBqorut9LMLCE38z8km/k+NJhQ0+lRblMoXwicKOJLx9L0pvg0jLw51Y5Fc2EDI07Er 4XKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Ou2/QdVemYOWnpxH75827+Gnr2vd/tKIqxsvmGmu9CQ=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=NSGCYZQjbSuIjWDC+H23mcMEey0MBWC5gX5VnuVS46kcNG0Xcc/R52sqhjEgrkfA7H PkindXFdAM/E98VRQDfk5cyi/fKcgIz/Tyf7GQuNrAAYz+dXVnuxdWI++lYAGkjI9iXO t1LjBpZJ0NOWa2Z6U819rzDy0wSq7LCskOekMvj9tnWuhhzlqFihZX9/MKzT/wPE8mMM wcsccVEEus3qWjYcllSfGcJRWtJrch8XeqBnNfw6UjgM6J8Zx/hE5KEAqYXF7aRWaoDR 2UYRpJpBQVS4Wyb+uGe1uQhcQUpMYNEE3Z7uRBO2vm8OMW6MRyf976lV5F0xqCiMSCLY viSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=IFXWovi9; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t19-20020a170906065300b009882f2a8aabsi8109671ejb.551.2023.06.29.08.23.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jun 2023 08:23:02 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=IFXWovi9; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 76D71385840A for ; Thu, 29 Jun 2023 15:22:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76D71385840A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688052171; bh=Ou2/QdVemYOWnpxH75827+Gnr2vd/tKIqxsvmGmu9CQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IFXWovi9M/2bT/97WT68GpNQwo+Rx4+x9MqiDUTGLyu0lLtNe7V/QKZ5QeJlX2OYc zjpilQgWF9RizlzXRw2XVQzCCBtFLdbOALf8dqrQ21pgBwmHCDHbx41Ez1xSMq7n60 TQ3RoPfk5GWy3Kak5ywptjVvqbDXh1TTKjD0veug= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id ACB733858298 for ; Thu, 29 Jun 2023 15:22:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ACB733858298 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-55-zlrP94ySNCO6B0fZaUhh_g-1; Thu, 29 Jun 2023 11:21:59 -0400 X-MC-Unique: zlrP94ySNCO6B0fZaUhh_g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 073E52A2AD62; Thu, 29 Jun 2023 15:21:31 +0000 (UTC) Received: from localhost (unknown [10.42.28.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id C54051121314; Thu, 29 Jun 2023 15:21:30 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix src/c++20/tzdb.cc for non-constexpr std::mutex Date: Thu, 29 Jun 2023 16:20:10 +0100 Message-ID: <20230629152130.607676-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770051005152394535?= X-GMAIL-MSGID: =?utf-8?q?1770051005152394535?= This build failure for riscv32-esp-elf was "reported" on the gcc-help list: https://gcc.gnu.org/pipermail/gcc-help/2023-June/142641.html Tested x86_64-linux. Pushed to trunk. -- >8 -- Building libstdc++ reportedly fails for targets without lock-free std::atomic which don't define __GTHREAD_MUTEX_INIT: src/c++20/tzdb.cc:110:21: error: 'constinit' variable 'std::chrono::{anonymous}::list_mutex' does not have a constant initializer src/c++20/tzdb.cc:110:21: error: call to non-'constexpr' function 'std::mutex::mutex()' The solution implemented by this commit is to use a local static mutex when it can't be constinit, so that it's constructed on first use. With this change, we can also simplify the preprocessor logic for defining USE_ATOMIC_SHARED_PTR. It now depends on the same conditions as USE_ATOMIC_LIST_HEAD, so in theory we could have a single macro. Keeping them separate would allow us to replace the use of atomic> with a mutex if that performs better, without having to give up on the lock-free cache for fast access to the list head. libstdc++-v3/ChangeLog: * src/c++20/tzdb.cc (USE_ATOMIC_SHARED_PTR): Define consistently with USE_ATOMIC_LIST_HEAD. (list_mutex): Replace global object with function. Use local static object when std::mutex constructor isn't constexpr. --- libstdc++-v3/src/c++20/tzdb.cc | 46 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/libstdc++-v3/src/c++20/tzdb.cc b/libstdc++-v3/src/c++20/tzdb.cc index a43b4f33eba..8d27726016e 100644 --- a/libstdc++-v3/src/c++20/tzdb.cc +++ b/libstdc++-v3/src/c++20/tzdb.cc @@ -41,22 +41,17 @@ # include // getenv #endif -#ifndef __GTHREADS -# define USE_ATOMIC_SHARED_PTR 0 -#elif _WIN32 -// std::mutex cannot be constinit, so Windows must use atomic>. -# define USE_ATOMIC_SHARED_PTR 1 -#elif ATOMIC_POINTER_LOCK_FREE < 2 -# define USE_ATOMIC_SHARED_PTR 0 -#else -// TODO benchmark atomic> vs mutex. -# define USE_ATOMIC_SHARED_PTR 1 -#endif - #if defined __GTHREADS && ATOMIC_POINTER_LOCK_FREE == 2 # define USE_ATOMIC_LIST_HEAD 1 +// TODO benchmark atomic> vs mutex. +# define USE_ATOMIC_SHARED_PTR 1 #else # define USE_ATOMIC_LIST_HEAD 0 +# define USE_ATOMIC_SHARED_PTR 0 +#endif + +#if USE_ATOMIC_SHARED_PTR && ! USE_ATOMIC_LIST_HEAD +# error Unsupported combination #endif #if ! __cpp_constinit @@ -106,9 +101,18 @@ namespace std::chrono // Dummy no-op mutex type for single-threaded targets. struct mutex { void lock() { } void unlock() { } }; #endif - /// XXX std::mutex::mutex() not constexpr on Windows, so can't be constinit - constinit mutex list_mutex; + inline mutex& list_mutex() + { +#ifdef __GTHREAD_MUTEX_INIT + constinit static mutex m; +#else + // Cannot use a constinit mutex, so use a local static. + alignas(mutex) constinit static char buf[sizeof(mutex)]; + static mutex& m = *::new(buf) mutex(); #endif + return m; + } +#endif // ! USE_ATOMIC_SHARED_PTR struct Rule; } @@ -154,7 +158,7 @@ namespace std::chrono static _Node* _S_list_head(memory_order) { - lock_guard l(list_mutex); + lock_guard l(list_mutex()); return _S_head_owner.get(); } @@ -1279,7 +1283,7 @@ namespace std::chrono } // XXX small window here where _S_head_cache still points to previous tzdb. #else - lock_guard l(list_mutex); + lock_guard l(list_mutex()); if (const _Node* h = _S_head_owner.get()) { if (h->db.version == new_head_ptr->db.version) @@ -1406,11 +1410,12 @@ namespace std::chrono #else if (Node::_S_list_head(memory_order::relaxed) != nullptr) [[likely]] { - lock_guard l(list_mutex); + lock_guard l(list_mutex()); const tzdb& current = Node::_S_head_owner->db; if (current.version == version) return current; } + shared_ptr head; // Passed as unused arg to _S_replace_head. #endif auto [leaps, leaps_ok] = Node::_S_read_leap_seconds(); @@ -1499,9 +1504,6 @@ namespace std::chrono ranges::sort(node->db.links, {}, &time_zone_link::name); ranges::stable_sort(node->rules, {}, &Rule::name); -#if ! USE_ATOMIC_SHARED_PTR - shared_ptr head; -#endif return Node::_S_replace_head(std::move(head), std::move(node)); #else __throw_disabled(); @@ -1526,7 +1528,7 @@ namespace std::chrono #if USE_ATOMIC_SHARED_PTR return const_iterator{_Node::_S_head_owner.load()}; #else - lock_guard l(list_mutex); + lock_guard l(list_mutex()); return const_iterator{_Node::_S_head_owner}; #endif } @@ -1539,7 +1541,7 @@ namespace std::chrono if (p._M_node) [[likely]] { #if ! USE_ATOMIC_SHARED_PTR - lock_guard l(list_mutex); + lock_guard l(list_mutex()); #endif if (auto next = p._M_node->next) [[likely]] return const_iterator{p._M_node->next = std::move(next->next)};