From patchwork Fri Feb 23 23:28:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 877329 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-473793-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Ulty1mSl"; 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 3zp6q02HYBz9sBR for ; Sat, 24 Feb 2018 10:28:27 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=yLVc1/7F9rHM3iRKC0/XjX2z0g+4RmIokh95zRHSOpf4h1 buOdN8snhH0b2e1cSdaK+soIGqJ1kYHR5xCf2GZTWTl+1uUI96z09euXvZja/Htz CQOMHI0p9HUkMOGRWwIt6/9kWTNt/ykxISNILdwVm8TMhfZqFtYPj7H6ITZbk= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=OfwHOgOb7xytE6OQ43aLcrqYu9k=; b=Ulty1mSlJOMNB5cpmbXP Ub7J1DMEi1XeKybs3kwHSTHhxxK7ei/Naavy1X05x+jbFiQgxhrmcwRBDkom41+D W4BgqKyBNmZnVWCD9vLDJMTc5owQFzxncX59Lx57YYf1EB94czFEWoSFOcFjth+n BKUFR4tFls2O3WBUF1gqHJ8= Received: (qmail 109204 invoked by alias); 23 Feb 2018 23:28:20 -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 109186 invoked by uid 89); 23 Feb 2018 23:28:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-23.0 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS, URIBL_SBL autolearn=ham version=3.3.2 spammy= X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-it0-f49.google.com Received: from mail-it0-f49.google.com (HELO mail-it0-f49.google.com) (209.85.214.49) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 23 Feb 2018 23:28:17 +0000 Received: by mail-it0-f49.google.com with SMTP id a203so6347049itd.1; Fri, 23 Feb 2018 15:28:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=CyO9dElNyhh/J2/YWSwZguSIECJMacxhjr49/Z9UAFM=; b=Nkdo0GldOiQsBuJPElW3WLiot1A1Di3klbnnjxKgEmQGIc3yhh9bG1Mht1fRh4uz6F wX5YXUIUJL9Sq0wzWcXf9FddDKtPXeoG6NFW0PJTDK9RXMDoOyy8BYanUNYhRp9UurF9 0MiqsT4tr1PWIXmXpzc6f4ktPY0LrxNc9fm8ap0wCln5RDAKCsS7Cz7wlpFrTFVJkkJI 4Mb7547JZyIepJQ6fP+TjZnimc3zkIkC/VId+bpL69KiyL+rWE3r5QaDNAdWLQBJaSF3 R/LDdIvK5dov3Dr5wHnpOTyn4Rmpa6tbkRzx8SCeT46CXsRp65g1BymuoFUPhVcYyWl8 CdOQ== X-Gm-Message-State: APf1xPAPwkmE+XqtLUiiI3BHo6FliqLCPwRe7gqaZZz4vIspbB2Ydp66 CQw7Md7Z0eTumL8ld7HE4nnzWwVYDLH/oMNziUNDQg== X-Google-Smtp-Source: AG47ELv3CeNzWBPvLer0WHZeUe3LXfrof+014s7w0qouRWAvHtmpad+w1p0CqmY1lChk2FMAR45GkpTAo3dwjUGqO5k= X-Received: by 10.36.108.19 with SMTP id w19mr4374996itb.65.1519428495766; Fri, 23 Feb 2018 15:28:15 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.50.83 with HTTP; Fri, 23 Feb 2018 15:28:14 -0800 (PST) From: Jonathan Wakely Date: Fri, 23 Feb 2018 23:28:14 +0000 Message-ID: Subject: [PATCH] PR libstdc++/84532 prevent unwrapping of reference_wrapper arguments To: "libstdc++" , gcc-patches When I refactored the std::thread internals I made it use std::make_tuple, which rather than just DECAY_COPYing the arguments it also unwraps any std::reference_wrapper arguments, which is wrong. This constructs a std::tuple of the decayed types, instead of using make_tuple, so that reference_wrapper arguments are preserved (and convert to the wrapped type on invocation, if required). PR libstdc++/84532 * include/std/thread (thread::__make_invoker): Construct tuple directly instead of using make_tuple. * testsuite/30_threads/async/84532.cc: New. * testsuite/30_threads/thread/84532.cc: New. Tested x86_64-linux, committed to trunk. Backport to gcc-7-branch will follow. commit 241b2ec07ff9e174c60239706799a1b42818e8c8 Author: redi Date: Fri Feb 23 23:23:43 2018 +0000 PR libstdc++/84532 prevent unwrapping of reference_wrapper arguments PR libstdc++/84532 * include/std/thread (thread::__make_invoker): Construct tuple directly instead of using make_tuple. * testsuite/30_threads/async/84532.cc: New. * testsuite/30_threads/thread/84532.cc: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@257956 138bc75d-0d04-0410-961f-82ee72b054a4 diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index 0c53294aac2..1cabd6ae0e6 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -243,21 +243,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return _M_invoke(_Indices()); } }; - // Alias for _Invoker> template - using __invoker_type - = _Invoker()...))>; + using __decayed_tuple = tuple::type...>; public: - // Returns a call wrapper that does - // INVOKE(DECAY_COPY(__callable), DECAY_COPY(__args)). + // Returns a call wrapper that stores + // tuple{DECAY_COPY(__callable), DECAY_COPY(__args)...}. template - static __invoker_type<_Callable, _Args...> + static _Invoker<__decayed_tuple<_Callable, _Args...>> __make_invoker(_Callable&& __callable, _Args&&... __args) { - return { { - std::make_tuple(std::forward<_Callable>(__callable), - std::forward<_Args>(__args)...) + return { __decayed_tuple<_Callable, _Args...>{ + std::forward<_Callable>(__callable), std::forward<_Args>(__args)... } }; } }; diff --git a/libstdc++-v3/testsuite/30_threads/async/84532.cc b/libstdc++-v3/testsuite/30_threads/async/84532.cc new file mode 100644 index 00000000000..480ed733ca3 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/async/84532.cc @@ -0,0 +1,38 @@ +// { dg-do compile { target c++11 } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +// PR libstdc++/84532 + +struct F +{ + template + void operator()(T, U, int&) + { + using std::is_same; + using std::reference_wrapper; + static_assert(is_same>::value, ""); + static_assert(is_same>::value, ""); + } +}; +int i = 0; +auto fut = std::async(F{}, std::ref(i), std::cref(i), std::ref(i)); diff --git a/libstdc++-v3/testsuite/30_threads/thread/84532.cc b/libstdc++-v3/testsuite/30_threads/thread/84532.cc new file mode 100644 index 00000000000..f389b9b88e3 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/thread/84532.cc @@ -0,0 +1,38 @@ +// { dg-do compile { target c++11 } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +// PR libstdc++/84532 + +struct F +{ + template + void operator()(T, U, int&) + { + using std::is_same; + using std::reference_wrapper; + static_assert(is_same>::value, ""); + static_assert(is_same>::value, ""); + } +}; +int i = 0; +std::thread t(F{}, std::ref(i), std::cref(i), std::ref(i));