From patchwork Tue Aug 14 13:13:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 957519 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-483633-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="BqbveneW"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41qY2b0pxdz9s9l for ; Tue, 14 Aug 2018 23:13:58 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=FskW9K+wf2eixTgJGEgpOjqqsKC0JT8WhhmnF9puCuOexPoeuU LAWs+Cy2rGQ8Z8YFbkbzWmHuqUFsR6vtFvXTG9ndG4V/+O5TyvZdS/w23riUlc4j ql8NZHmxvAo9gybqFp7W/zEx6TavA+Yjj8K/lCZ4fTyuxeS1Qiy1ItGc4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=LOSd1X4hNkVIq6x8AuKc8Dh8lts=; b=BqbveneWpxtx6ueQUO8c cK3n5H/ozBgScuwrY++TBlyB9ssexDlzySp33acAau03lMFj5zgC/rbCnaQu/ipW gI5cobMfbYwXLJGvI7o5+S5MBvgSM7U7rqB6chnksrRNwC0FEtxK43mQrv3E0MtR 9F9igECIAhbpeyIrI106ln8= Received: (qmail 3175 invoked by alias); 14 Aug 2018 13:13:44 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 3153 invoked by uid 89); 14 Aug 2018 13:13:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=H*Ad:U*danglin X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 14 Aug 2018 13:13:41 +0000 Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A521281DE8; Tue, 14 Aug 2018 13:13:40 +0000 (UTC) Received: from localhost (ovpn-117-178.phx2.redhat.com [10.3.117.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C7DF30012A5; Tue, 14 Aug 2018 13:13:40 +0000 (UTC) Date: Tue, 14 Aug 2018 14:13:39 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: John David Anglin Subject: [PATCH] PR libstdc++/86846 Alternative to pointer-width atomics Message-ID: <20180814131339.GA9685@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) Define a class using std::mutex for when std::atomic cannot be used to implement the default memory resource. When std::mutex constructor is not constexpr the constant_init trick won't work, so just define a global and use init_priority for it. The compiler warns about using reserved priority, so put the definition in a header file using #pragma GCC system_header to suppress the warning. PR libstdc++/86846 * src/c++17/default_resource.h: New file, defining default_res. * src/c++17/memory_resource.cc [ATOMIC_POINTER_LOCK_FREE != 2] (atomic_mem_res): Define alternative for atomic using a mutex instead of atomics. Tested x86_64-linux, committed to trunk. commit 81de951109506b684d079f0fbc7aebae2c228ba4 Author: Jonathan Wakely Date: Mon Aug 13 12:34:56 2018 +0100 PR libstdc++/86846 Alternative to pointer-width atomics Define a class using std::mutex for when std::atomic cannot be used to implement the default memory resource. When std::mutex constructor is not constexpr the constant_init trick won't work, so just define a global and use init_priority for it. The compiler warns about using reserved priority, so put the definition in a header file using #pragma GCC system_header to suppress the warning. PR libstdc++/86846 * src/c++17/default_resource.h: New file, defining default_res. * src/c++17/memory_resource.cc [ATOMIC_POINTER_LOCK_FREE != 2] (atomic_mem_res): Define alternative for atomic using a mutex instead of atomics. diff --git a/libstdc++-v3/src/c++17/default_resource.h b/libstdc++-v3/src/c++17/default_resource.h new file mode 100644 index 00000000000..522cee13b90 --- /dev/null +++ b/libstdc++-v3/src/c++17/default_resource.h @@ -0,0 +1,11 @@ +// This is only in a header so we can use the system_header pragma, +// to suppress the warning caused by using a reserved init_priority. +#pragma GCC system_header + +#if ATOMIC_POINTER_LOCK_FREE == 2 || defined(__GTHREAD_MUTEX_INIT) +# error "This file should not be included for this build" +#endif + +struct { + atomic_mem_res obj = &newdel_res.obj; +} default_res __attribute__ ((init_priority (100))); diff --git a/libstdc++-v3/src/c++17/memory_resource.cc b/libstdc++-v3/src/c++17/memory_resource.cc index c3ae2b69f71..bd8f32d931e 100644 --- a/libstdc++-v3/src/c++17/memory_resource.cc +++ b/libstdc++-v3/src/c++17/memory_resource.cc @@ -25,6 +25,10 @@ #include #include #include +#if ATOMIC_POINTER_LOCK_FREE != 2 +# include // std::mutex, std::lock_guard +# include // std::exchange +#endif namespace std _GLIBCXX_VISIBILITY(default) { @@ -81,7 +85,42 @@ namespace pmr constant_init newdel_res{}; constant_init null_res{}; - constant_init> default_res{&newdel_res.obj}; +#if ATOMIC_POINTER_LOCK_FREE == 2 + using atomic_mem_res = atomic; +# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED +#else + // Can't use pointer-width atomics, define a type using a mutex instead: + struct atomic_mem_res + { +# ifdef __GTHREAD_MUTEX_INIT +# define _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED + // std::mutex has constexpr constructor + constexpr +# endif + atomic_mem_res(memory_resource* r) : val(r) { } + + mutex mx; + memory_resource* val; + + memory_resource* load() + { + lock_guard lock(mx); + return val; + } + + memory_resource* exchange(memory_resource* r) + { + lock_guard lock(mx); + return std::exchange(val, r); + } + }; +#endif // ATOMIC_POINTER_LOCK_FREE == 2 + +#ifdef _GLIBCXX_ATOMIC_MEM_RES_CAN_BE_CONSTANT_INITIALIZED + constant_init default_res{&newdel_res.obj}; +#else +# include "default_resource.h" +#endif } // namespace memory_resource*