From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id E44B2385841A; Thu, 29 Jun 2023 15:19:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E44B2385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688051959; bh=mJ3gAxeJP5djLYo4MHce9zifWI0vCCqB4y+7j+gpRBg=; h=From:To:Subject:Date:From; b=CGnd8IcKMvp+tkwCHzRLQUft6xEf28UiTHy9XtyI52CgMdAytiHk+bWUNvUZa/vBP rrDa040+QmtmVElsEIiChjpqmQtdOPlWDEE7zr/X+s/4naJeYfvFNQSBcfIkAvZ5A6 nQSnPUAwrFBjdcdsJqiEtjxClYcJtgyyeFyPhey8= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-2192] libstdc++: Fix src/c++20/tzdb.cc for non-constexpr std::mutex X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: ff29ee6af88f709e08ee467869d8c1b13889a724 X-Git-Newrev: 5dfdf0ae4dca44a4f572c346d322fd6244598190 Message-Id: <20230629151919.E44B2385841A@sourceware.org> Date: Thu, 29 Jun 2023 15:19:19 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5dfdf0ae4dca44a4f572c346d322fd6244598190 commit r14-2192-g5dfdf0ae4dca44a4f572c346d322fd6244598190 Author: Jonathan Wakely Date: Thu Jun 29 11:40:32 2023 +0100 libstdc++: Fix src/c++20/tzdb.cc for non-constexpr std::mutex 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. Diff: --- 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)};