diff mbox series

PR libstdc++/84532 prevent unwrapping of reference_wrapper arguments

Message ID CAH6eHdTizdi3wEM-RKyFe6WWefnGQiuf8jaFcjHdmdp_086j5g@mail.gmail.com
State New
Headers show
Series PR libstdc++/84532 prevent unwrapping of reference_wrapper arguments | expand

Commit Message

Jonathan Wakely Feb. 23, 2018, 11:28 p.m. UTC
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 <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
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 mbox series

Patch

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<tuple<DECAY_COPY(_Tp)...>>
     template<typename... _Tp>
-      using __invoker_type
-	= _Invoker<decltype(std::make_tuple(std::declval<_Tp>()...))>;
+      using __decayed_tuple = tuple<typename std::decay<_Tp>::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<typename _Callable, typename... _Args>
-      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
+// <http://www.gnu.org/licenses/>.
+
+#include <future>
+
+// PR libstdc++/84532
+
+struct F
+{
+  template<typename T, typename U>
+    void operator()(T, U, int&)
+    {
+      using std::is_same;
+      using std::reference_wrapper;
+      static_assert(is_same<T, reference_wrapper<int>>::value, "");
+      static_assert(is_same<U, reference_wrapper<const int>>::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
+// <http://www.gnu.org/licenses/>.
+
+#include <thread>
+
+// PR libstdc++/84532
+
+struct F
+{
+  template<typename T, typename U>
+    void operator()(T, U, int&)
+    {
+      using std::is_same;
+      using std::reference_wrapper;
+      static_assert(is_same<T, reference_wrapper<int>>::value, "");
+      static_assert(is_same<U, reference_wrapper<const int>>::value, "");
+    }
+};
+int i = 0;
+std::thread t(F{}, std::ref(i), std::cref(i), std::ref(i));