From patchwork Mon Aug 22 21:53:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 661 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp682834wro; Mon, 22 Aug 2022 14:55:21 -0700 (PDT) X-Google-Smtp-Source: AA6agR648CBwuCYr1OtlNAhK6q9zNgWiEZoswiRdPpzjcK7gWALCzaVHF6jvtV0WbruAKaixH6Hv X-Received: by 2002:a05:6402:1e90:b0:446:5aa1:808b with SMTP id f16-20020a0564021e9000b004465aa1808bmr977612edf.90.1661205320895; Mon, 22 Aug 2022 14:55:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661205320; cv=none; d=google.com; s=arc-20160816; b=I9iOOVzWHqjNqWjHY+cFmM2oevuKD7wfo3AVb6A+7DHRKEwyR5Iivn/2FKUDlPwI5l M9EbDxh/69bg/pUznBnuksou7jAAGyoxa1Fq29nOW9qplEv8c0yIbcl4289mijf7le5s Pltjor33HWm5o2YT8c51zQ8OFs3nOUuoJ6B6mtK6orRbKj465k5E2+SmBIa1elpQ0uAQ DNrJsiZeDZnQydg3R3Nak/2Q3qAaVrpQDTAiG5UKswobFqC9vFjsn5p5ydY6lt1bUQde hC96K22lEOaECvwHy15T2METEnbjWyPWqDlQWlxwIH/onyWL5KZm9GPE0p/iYmFxHwN0 CCOA== 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=OWaibvVUN3Abl6xPbEua/b1hZTZuHnKOy+wTvkovHQE=; b=W/eHcwRRD9fB3VfVEmIw9wx4zaLygI5uWeVgctWeFSvZnWFGhFRuegATq+VZMoDyKB ITgmi4svOW/+GI2LXTMcBnxq/1icn4jbkoJcNXT7hjkYNoOsDwNkZZ8dem9yOZ3P5q25 jx+m3ovxWDcd5fFiyWvsMWF1wfC0EQZwBcNPxY+3esJQ2yVeraLktlHh3UILZYpYMi9T MZgc0VVSmaEbmwQaVRcl3RSBL894NUDIDNjudRoYpU16HjQq8xfLqLaZlF4B92G2OLTs j4iHpHzKW5UWRTJ29dBBrgkEUsDG1uX0yZ4u34Pp3f04CGfYEMpTvKKtI9rILALGd/yK sbPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=muI3YetQ; 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 n13-20020a05640205cd00b0043d98dddc27si592001edx.59.2022.08.22.14.55.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Aug 2022 14:55:20 -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=muI3YetQ; 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 C4B423853809 for ; Mon, 22 Aug 2022 21:54:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C4B423853809 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661205249; bh=OWaibvVUN3Abl6xPbEua/b1hZTZuHnKOy+wTvkovHQE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=muI3YetQ3LQt4HPguKB/kTGmOxxOUghSSAUlmgl77ahsTls7YuRwc7c3QORER7WK5 QDGPSUL/Y2DV0ZYe2IC+hBVOu0kQgWCU7LHu26tpquYAUnfodvu4evRYh8MftfXTA7 Dgp/7MgAd4xC/rbOUuEhqW11c0In0H04jDZ+T8UU= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 846043857012 for ; Mon, 22 Aug 2022 21:53:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 846043857012 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-482-V8739xPNMMC_ZWfEjeAKqA-1; Mon, 22 Aug 2022 17:53:23 -0400 X-MC-Unique: V8739xPNMMC_ZWfEjeAKqA-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 DD5EA811E84; Mon, 22 Aug 2022 21:53:22 +0000 (UTC) Received: from localhost (unknown [10.33.36.78]) by smtp.corp.redhat.com (Postfix) with ESMTP id A817B1121314; Mon, 22 Aug 2022 21:53:22 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix for explicit copy ctors in and [PR106695] Date: Mon, 22 Aug 2022 22:53:22 +0100 Message-Id: <20220822215322.92997-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX autolearn=ham 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?1741900030493758681?= X-GMAIL-MSGID: =?utf-8?q?1741900030493758681?= Tested powerpc64le-linux, pushed to trunk. Needs backporting. -- >8 -- When I changed std::thread and std::async to avoid unnecessary move construction of temporaries, I introduced a regression where types with an explicit copy constructor could not be passed to std::thread or std::async. The fix is to add a constructor instead of using aggregate initialization of an unnamed temporary. libstdc++-v3/ChangeLog: PR libstdc++/106695 * include/bits/std_thread.h (thread::_State_impl): Forward individual arguments to _Invoker constructor. (thread::_Invoker): Add constructor. Delete copies. * include/std/future (__future_base::_Deferred_state): Forward individual arguments to _Invoker constructor. (__future_base::_Async_state_impl): Likewise. * testsuite/30_threads/async/106695.cc: New test. * testsuite/30_threads/thread/106695.cc: New test. --- libstdc++-v3/include/bits/std_thread.h | 8 ++++- libstdc++-v3/include/std/future | 4 +-- .../testsuite/30_threads/async/106695.cc | 29 +++++++++++++++++++ .../testsuite/30_threads/thread/106695.cc | 21 ++++++++++++++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/30_threads/async/106695.cc create mode 100644 libstdc++-v3/testsuite/30_threads/thread/106695.cc diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index d7fc0123564..3a7db12c6ba 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -227,7 +227,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _State_impl(_Args&&... __args) - : _M_func{{std::forward<_Args>(__args)...}} + : _M_func(std::forward<_Args>(__args)...) { } void @@ -261,6 +261,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct _Invoker { + template + explicit + _Invoker(_Args&&... __args) + : _M_t(std::forward<_Args>(__args)...) + { } + _Tuple _M_t; template diff --git a/libstdc++-v3/include/std/future b/libstdc++-v3/include/std/future index a925d03d19c..ba1f28c6c75 100644 --- a/libstdc++-v3/include/std/future +++ b/libstdc++-v3/include/std/future @@ -1681,7 +1681,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION explicit _Deferred_state(_Args&&... __args) : _M_result(new _Result<_Res>()), - _M_fn{{std::forward<_Args>(__args)...}} + _M_fn(std::forward<_Args>(__args)...) { } private: @@ -1748,7 +1748,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION explicit _Async_state_impl(_Args&&... __args) : _M_result(new _Result<_Res>()), - _M_fn{{std::forward<_Args>(__args)...}} + _M_fn(std::forward<_Args>(__args)...) { _M_thread = std::thread{&_Async_state_impl::_M_run, this}; } diff --git a/libstdc++-v3/testsuite/30_threads/async/106695.cc b/libstdc++-v3/testsuite/30_threads/async/106695.cc new file mode 100644 index 00000000000..74996342dc7 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/async/106695.cc @@ -0,0 +1,29 @@ +// { dg-do compile { target c++11 } } +// { dg-require-gthreads "" } + +// PR libstdc++/106695 +// Explicit copy constructor does not work for a parameter passed via std::async + +#include + +struct A { + A() = default; + explicit A(const A&) = default; +}; + +void func(const A&) { } + +void +test_async() +{ + (void) std::async(std::launch::async, func, A{}); + (void) std::async(std::launch::deferred, func, A{}); + (void) std::async(func, A{}); +} + +void +test_task() +{ + std::packaged_task task(func); + task(A{}); +} diff --git a/libstdc++-v3/testsuite/30_threads/thread/106695.cc b/libstdc++-v3/testsuite/30_threads/thread/106695.cc new file mode 100644 index 00000000000..97e9e922d8e --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/thread/106695.cc @@ -0,0 +1,21 @@ +// { dg-do compile { target c++11 } } +// { dg-require-gthreads "" } + +// PR libstdc++/106695 +// Explicit copy constructor does not work for a parameter passed via std::async + +#include + +struct A { + A() = default; + explicit A(const A&) = default; +}; + +void func(const A&) { } + +void +test_thread() +{ + std::thread t(func, A{}); + t.join(); +}