[PATCH] Do not copy std:call_once arguments (LWG 2442)

Jonathan Wakely jwakely@redhat.com
Wed Oct 12 11:22:00 GMT 2016


This replaces the use of std::__bind_simple by direct calls to
std::__invoke so that the arguments are not decay-copied.

	* doc/xml/manual/intro.xml: Document LWG 2442 status.
	* include/std/mutex [_GLIBCXX_HAVE_TLS] (__once_call_impl): Remove.
	[_GLIBCXX_HAVE_TLS] (_Once_call): Declare primary template and define
	partial specialization to unpack args and forward to std::invoke.
	(call_once) [_GLIBCXX_HAVE_TLS]: Use forward_as_tuple and _Once_call
	instead of __bind_simple and __once_call_impl.
	(call_once) [!_GLIBCXX_HAVE_TLS]: Use __invoke instead of
	__bind_simple.
	* testsuite/30_threads/call_once/dr2442.cc: New test.

Tested powerpc64le-linux, committed to trunk.

-------------- next part --------------
commit 67819bb3bee8fb170e46b324c65d35b856136cd1
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Wed Oct 12 11:20:25 2016 +0000

    Do not copy std:call_once arguments (LWG 2442)
    
    	* doc/xml/manual/intro.xml: Document LWG 2442 status.
    	* include/std/mutex [_GLIBCXX_HAVE_TLS] (__once_call_impl): Remove.
    	[_GLIBCXX_HAVE_TLS] (_Once_call): Declare primary template and define
    	partial specialization to unpack args and forward to std::invoke.
    	(call_once) [_GLIBCXX_HAVE_TLS]: Use forward_as_tuple and _Once_call
    	instead of __bind_simple and __once_call_impl.
    	(call_once) [!_GLIBCXX_HAVE_TLS]: Use __invoke instead of
    	__bind_simple.
    	* testsuite/30_threads/call_once/dr2442.cc: New test.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@241031 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml
index 22b792a..528b192 100644
--- a/libstdc++-v3/doc/xml/manual/intro.xml
+++ b/libstdc++-v3/doc/xml/manual/intro.xml
@@ -1043,6 +1043,13 @@ requirements of the license of GCC.
     <listitem><para>Define the typedefs.
     </para></listitem></varlistentry>
 
+    <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../ext/lwg-defects.html#2442">2442</link>:
+       <emphasis><code>call_once()</code> shouldn't <code>DECAY_COPY()</code></emphasis>
+    </term>
+    <listitem><para>Remove indirection through call wrapper that made copies
+    of arguments and forward arguments straight to <code>std::invoke</code>.
+    </para></listitem></varlistentry>
+
     <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../ext/lwg-defects.html#2454">2454</link>:
        <emphasis>Add <code>raw_storage_iterator::base()</code> member
        </emphasis>
diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex
index 7a7bd2e..4c6f036 100644
--- a/libstdc++-v3/include/std/mutex
+++ b/libstdc++-v3/include/std/mutex
@@ -580,12 +580,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   extern __thread void* __once_callable;
   extern __thread void (*__once_call)();
 
-  template<typename _Callable>
-    inline void
-    __once_call_impl()
+  template<typename _Tuple, typename _IndexSeq
+	   = typename _Build_index_tuple<tuple_size<_Tuple>::value>::__type>
+    struct _Once_call;
+
+  template<typename _Tuple, size_t... _Ind>
+    struct _Once_call<_Tuple, _Index_tuple<_Ind...>>
     {
-      (*(_Callable*)__once_callable)();
-    }
+      static void
+      _S_call()
+      {
+	auto& __f_args = *static_cast<_Tuple*>(__once_callable);
+	std::__invoke(std::get<_Ind>(std::move(__f_args))...);
+      }
+    };
 #else
   extern function<void()> __once_functor;
 
@@ -603,16 +611,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     void
     call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
     {
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // 2442. call_once() shouldn't DECAY_COPY()
 #ifdef _GLIBCXX_HAVE_TLS
-      auto __bound_functor = std::__bind_simple(std::forward<_Callable>(__f),
-          std::forward<_Args>(__args)...);
-      __once_callable = std::__addressof(__bound_functor);
-      __once_call = &__once_call_impl<decltype(__bound_functor)>;
+      auto __f_args = std::forward_as_tuple(
+	  std::forward<_Callable>(__f), std::forward<_Args>(__args)...);
+      __once_callable = std::__addressof(__f_args);
+      __once_call = _Once_call<decltype(__f_args)>::_S_call;
 #else
       unique_lock<mutex> __functor_lock(__get_once_mutex());
-      auto __callable = std::__bind_simple(std::forward<_Callable>(__f),
-          std::forward<_Args>(__args)...);
-      __once_functor = [&]() { __callable(); };
+      __once_functor = [&] {
+	  std::__invoke(std::forward<_Callable>(__f),
+			std::forward<_Args>(__args)...);
+      };
       __set_once_functor_lock_ptr(&__functor_lock);
 #endif
 
diff --git a/libstdc++-v3/testsuite/30_threads/call_once/dr2442.cc b/libstdc++-v3/testsuite/30_threads/call_once/dr2442.cc
new file mode 100644
index 0000000..5b66687
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/call_once/dr2442.cc
@@ -0,0 +1,45 @@
+// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-rtems* *-*-darwin* powerpc-ibm-aix* } }
+// { dg-options "-pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* powerpc-ibm-aix* } }
+// { dg-require-effective-target c++11 }
+// { dg-require-cstdint "" }
+// { dg-require-gthreads "" }
+
+// Copyright (C) 2016 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 <mutex>
+#include <testsuite_hooks.h>
+
+void f(int& a, int&& b) { a = 1; b = 2; }
+
+void
+test01()
+{
+  // LWG 2442. call_once() shouldn't DECAY_COPY()
+  std::once_flag once;
+  int i = 0;
+  int j = 0;
+  call_once(once, f, i, std::move(j));
+  VERIFY( i == 1 );
+  VERIFY( j == 2 );
+}
+
+int
+main()
+{
+  test01();
+}


More information about the Gcc-patches mailing list