public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/arm-9-branch)] libstdc++: Fix type-erasure in experimental::net::executor (PR 94203)
@ 2020-05-11 18:54 Vidya Praveen
  0 siblings, 0 replies; only message in thread
From: Vidya Praveen @ 2020-05-11 18:54 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:942b32e261c414a033766ed7848d923f9630b991

commit 942b32e261c414a033766ed7848d923f9630b991
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Apr 24 14:29:37 2020 +0100

    libstdc++: Fix type-erasure in experimental::net::executor (PR 94203)
    
    The _Tgt and _TgtImpl types that implement type-erasure didn't agree on
    the virtual interface, so failed as soon as they were instantiated. With
    Clang they failed even sooner. The interface was also dependent on
    whether RTTI was enabled or not.
    
    This patch fixes the broken virtual functions and makes the type work
    without RTTI, by using a pointer to a specialization of a function
    template (similar to the approaches in std::function and std::any).
    
    The changes to the virtual functions would be an ABI change, except that
    the previous code didn't even compile if instantiated. This is
    experimental TS material anyway.
    
    Backport from mainline
    2020-03-18  Jonathan Wakely  <jwakely@redhat.com>
    
            PR libstdc++/94203
            * include/experimental/executor (executor::executor(Executor)): Call
            make_shared directly instead of _M_create. Create _Tgt1 object.
            (executor::executor(allocator_arg_t, const ProtoAlloc&, Executor)):
            Call allocate_shared directly instead of _M_create. Create _Tgt2
            object.
            (executor::target_type): Add cast needed for new _Tgt interface.
            (executor::target): Define when RTTI is disabled. Use _Tgt::_M_func.
            (executor::_Tgt): Define the same interface whether RTTI is enabled or
            not.
            (executor::_Tgt::target_type, executor::_Tgt::target): Do not use
            std::type_info in the interface.
            (executor::_Tgt::_M_func): Add data member.
            (executor::_TgtImpl): Replace with _Tgt1 and _Tgt2 class templates.
            (executor::_Tgt1::_S_func): Define function to access target without
            depending on RTTI.
            (executor::_M_create): Remove.
            (operator==, operator!=): Simplify comparisons for executor.
            * include/experimental/socket (is_error_code_enum<socket_errc>):
            Define specialization before use.
            * testsuite/experimental/net/executor/1.cc: New test.

Diff:
---
 libstdc++-v3/ChangeLog                     |  24 +++
 libstdc++-v3/include/experimental/executor | 226 +++++++++++++++++++----------
 libstdc++-v3/include/experimental/socket   |  18 ++-
 3 files changed, 191 insertions(+), 77 deletions(-)

diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 23203b3ed15..ceadacbba8d 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,29 @@
 2020-04-24  Jonathan Wakely  <jwakely@redhat.com>
 
+	Backport from mainline
+	2020-03-18  Jonathan Wakely  <jwakely@redhat.com>
+
+	PR libstdc++/94203
+	* include/experimental/executor (executor::executor(Executor)): Call
+	make_shared directly instead of _M_create. Create _Tgt1 object.
+	(executor::executor(allocator_arg_t, const ProtoAlloc&, Executor)):
+	Call allocate_shared directly instead of _M_create. Create _Tgt2
+	object.
+	(executor::target_type): Add cast needed for new _Tgt interface.
+	(executor::target): Define when RTTI is disabled. Use _Tgt::_M_func.
+	(executor::_Tgt): Define the same interface whether RTTI is enabled or
+	not.
+	(executor::_Tgt::target_type, executor::_Tgt::target): Do not use
+	std::type_info in the interface.
+	(executor::_Tgt::_M_func): Add data member.
+	(executor::_TgtImpl): Replace with _Tgt1 and _Tgt2 class templates.
+	(executor::_Tgt1::_S_func): Define function to access target without
+	depending on RTTI.
+	(executor::_M_create): Remove.
+	(operator==, operator!=): Simplify comparisons for executor.
+	* include/experimental/socket (is_error_code_enum<socket_errc>):
+	Define specialization before use.
+
 	Backport from mainline
 	2020-04-24  Jonathan Wakely  <jwakely@redhat.com>
 
diff --git a/libstdc++-v3/include/experimental/executor b/libstdc++-v3/include/experimental/executor
index 13a24a8cdb5..7e2a97fd8ec 100644
--- a/libstdc++-v3/include/experimental/executor
+++ b/libstdc++-v3/include/experimental/executor
@@ -294,26 +294,22 @@ inline namespace v1
   class executor;
 
   bool
-  operator==(const executor& __a, const executor& __b) noexcept;
+  operator==(const executor&, const executor&) noexcept;
 
   bool
-  operator==(const executor& __e, nullptr_t) noexcept;
+  operator==(const executor&, nullptr_t) noexcept;
 
-  inline bool
-  operator==(nullptr_t, const executor& __e) noexcept
-  { return __e == nullptr; }
+  bool
+  operator==(nullptr_t, const executor&) noexcept;
 
-  inline bool
-  operator!=(const executor& __a, const executor& __b) noexcept
-  { return !(__a == __b); }
+  bool
+  operator!=(const executor&, const executor&) noexcept;
 
-  inline bool
-  operator!=(const executor& __e, nullptr_t) noexcept
-  { return !(__e == nullptr); }
+  bool
+  operator!=(const executor&, nullptr_t) noexcept;
 
-  inline bool
-  operator!=(nullptr_t, const executor& __e) noexcept
-  { return !(__e == nullptr); }
+  bool
+  operator!=(nullptr_t, const executor&) noexcept;
 
   void swap(executor&, executor&) noexcept;
 
@@ -998,12 +994,13 @@ inline namespace v1
 
     template<typename _Executor>
       executor(_Executor __e)
-      : _M_target(_M_create(std::move(__e)))
+      : _M_target(make_shared<_Tgt1<_Executor>>(std::move(__e)))
       { }
 
     template<typename _Executor, typename _ProtoAlloc>
       executor(allocator_arg_t, const _ProtoAlloc& __a, _Executor __e)
-      : _M_target(_M_create(std::move(__e), __a))
+      : _M_target(allocate_shared<_Tgt2<_Executor, _ProtoAlloc>>(__a,
+	    std::move(__e), __a))
       { }
 
     executor& operator=(const executor&) noexcept = default;
@@ -1100,28 +1097,46 @@ inline namespace v1
 #if __cpp_rtti
     const type_info&
     target_type() const noexcept
-    { return _M_target ? _M_target->target_type() : typeid(void); }
+    {
+      if (_M_target)
+	return *static_cast<const type_info*>(_M_target->target_type());
+      return typeid(void);
+    }
+#endif
 
     template<typename _Executor>
       _Executor*
       target() noexcept
       {
+	void* __p = nullptr;
 	if (_M_target)
-	  if (const auto* __p = _M_target->target(typeid(_Executor)))
-	    return const_cast<_Executor*>(static_cast<const _Executor>(__p));
-	return nullptr;
+	  {
+	    if (_M_target->_M_func == &_Tgt1<remove_cv_t<_Executor>>::_S_func)
+	      __p = _M_target->_M_func(_M_target.get(), nullptr);
+#if __cpp_rtti
+	    else
+	      __p = _M_target->target(&typeid(_Executor));
+#endif
+	  }
+	return static_cast<_Executor*>(__p);
       }
 
     template<typename _Executor>
       const _Executor*
       target() const noexcept
       {
+	const void* __p = nullptr;
 	if (_M_target)
-	  if (const auto* __p = _M_target->target(typeid(_Executor)))
-	    return static_cast<const _Executor*>(__p);
-	return nullptr;
-      }
+	  {
+	    if (_M_target->_M_func == &_Tgt1<remove_cv_t<_Executor>>::_S_func)
+	      return (_Executor*)_M_target->_M_func(_M_target.get(), nullptr);
+#if __cpp_rtti
+	    else
+	      __p = _M_target->target(&typeid(_Executor));
 #endif
+	  }
+	return static_cast<const _Executor*>(__p);
+      }
 
   private:
     struct _Tgt
@@ -1132,85 +1147,134 @@ inline namespace v1
       virtual void dispatch(std::function<void()>) const = 0;
       virtual void post(std::function<void()>) const = 0;
       virtual void defer(std::function<void()>) const = 0;
-#if __cpp_rtti
-      virtual const type_info& target_type() const = 0;
-      virtual void* target(const std::type_info&) const = 0;
+      virtual const void* target_type() const noexcept = 0;
+      virtual void* target(const void*) noexcept = 0;
       virtual bool _M_equals(_Tgt*) const noexcept = 0;
-      virtual const void* _M_get_executor() const noexcept = 0;
-#endif
+
+      using _Func = void* (_Tgt*, const _Tgt*);
+      _Func* _M_func; // Provides access to target without RTTI
     };
 
-    template<typename _Ex, typename _Alloc>
-      struct _TgtImpl : _Tgt
+    template<typename _Ex>
+      struct _Tgt1 : _Tgt
       {
 	explicit
-	_TgtImpl(_Ex&& __ex, const _Alloc& __a)
-	: _M_impl(std::move(__ex), __a) { }
+	_Tgt1(_Ex&& __ex)
+	: _M_ex(std::move(__ex))
+	{ this->_M_func = &_S_func; }
+
+	void
+	on_work_started() const noexcept override
+	{ _M_ex.on_work_started(); }
 
-	void on_work_started() const noexcept { _M_ex().on_work_started(); }
-	void on_work_finished() const noexcept { _M_ex().on_work_finished(); }
-	execution_context& context() const noexcept { return _M_ex().context(); }
 	void
-	dispatch(std::function<void()> __f) const
-	{ _M_ex().dispatch(std::move(__f), _M_alloc()); }
+	on_work_finished() const noexcept override
+	{ _M_ex.on_work_finished(); }
+
+	execution_context&
+	context() const noexcept override
+	{ return _M_ex.context(); }
+
 	void
-	post(std::function<void()> __f) const
-	{ _M_ex().post(std::move(__f), _M_alloc()); }
+	dispatch(std::function<void()> __f) const override
+	{ _M_ex.dispatch(std::move(__f), allocator<void>()); }
+
+	void
+	post(std::function<void()> __f) const override
+	{ _M_ex.post(std::move(__f), allocator<void>()); }
+
 	void
-	defer(std::function<void()> __f) const
-	{ _M_ex().defer(std::move(__f), _M_alloc()); }
+	defer(std::function<void()> __f) const override
+	{ _M_ex.defer(std::move(__f), allocator<void>()); }
 
+	const void*
+	target_type() const noexcept override
+	{
 #if __cpp_rtti
-	virtual const type_info&
-	target_type() const
-	{ return typeid(_Ex); }
+	  return &typeid(_Ex);
+#else
+	  return nullptr;
+#endif
+	}
 
-	virtual const void*
-	target(const std::type_info& __ti) const
+	void*
+	target(const void* __ti) noexcept override
 	{
-	  if (__ti == typeid(_Ex))
-	    return std::addressof(_M_ex());
+#if __cpp_rtti
+	  if (*static_cast<const type_info*>(__ti) == typeid(_Ex))
+	    return std::__addressof(_M_ex);
+#endif
 	  return nullptr;
 	}
 
-	virtual bool
-	_M_equals(const _Tgt* __tgt) const noexcept
+	bool
+	_M_equals(_Tgt* __tgt) const noexcept override
 	{
-	  if (__tgt->target_type() == typeid(_Ex))
-	    *static_cast<const _Ex*>(__tgt->_M_get_executor()) == _M_ex();
+#if __cpp_rtti
+	  if (const void* __p = __tgt->target(&typeid(_Ex)))
+	    return *static_cast<const _Ex*>(__p) == _M_ex;
+#endif
 	  return false;
 	}
 
-	virtual const void*
-	_M_get_executor() const noexcept
-	{ return std::addressof(_M_ex()); }
-#endif
+	_Ex _M_ex [[__no_unique_address__]];
 
-	_Ex& _M_ex() { return std::get<0>(_M_impl); }
-	_Alloc& _M_alloc() { return std::get<1>(_M_impl); }
-	std::tuple<_Ex, _Alloc> _M_impl;
+	static void*
+	_S_func(_Tgt* __p, const _Tgt* __q) noexcept
+	{
+	  auto& __ex = static_cast<_Tgt1*>(__p)->_M_ex;
+	  if (__q)
+	    {
+	      if (__ex == static_cast<const _Tgt1*>(__q)->_M_ex)
+		return __p;
+	      else
+		return nullptr;
+	    }
+	  else
+	    return std::__addressof(__ex);
+	}
       };
 
-    template<typename _Ex, typename _Alloc = std::allocator<void>>
-      shared_ptr<_Tgt>
-      _M_create(_Ex&& __ex, const _Alloc& __a = _Alloc())
+    template<typename _Ex, typename _Alloc>
+      struct _Tgt2 : _Tgt1<_Ex>
       {
-	return allocate_shared<_TgtImpl<_Ex, _Alloc>>(__a, std::move(__ex),
-						      __a);
-      }
+	explicit
+	_Tgt2(_Ex&& __ex, const _Alloc& __a)
+	: _Tgt1<_Ex>(std::move(__ex)), _M_alloc(__a) { }
+
+	void
+	dispatch(std::function<void()> __f) const override
+	{ this->_M_ex.dispatch(std::move(__f), _M_alloc); }
+
+	void
+	post(std::function<void()> __f) const override
+	{ this->_M_ex.post(std::move(__f), _M_alloc); }
+
+	void
+	defer(std::function<void()> __f) const override
+	{ this->_M_ex.defer(std::move(__f), _M_alloc); }
+
+	_Alloc _M_alloc [[__no_unique_address__]];
+      };
+
+    // Partial specialization for std::allocator<T>.
+    // Don't store the allocator.
+    template<typename _Ex, typename _Tp>
+      struct _Tgt2<_Ex, std::allocator<_Tp>> : _Tgt1<_Ex>
+      { };
 
     friend bool
     operator==(const executor& __a, const executor& __b) noexcept
     {
-      if (__a._M_target == __b._M_target)
+      _Tgt* __ta = __a._M_target.get();
+      _Tgt* __tb = __b._M_target.get();
+      if (__ta == __tb)
 	return true;
-      if (!__a._M_target || !__b._M_target)
+      if (!__ta || !__tb)
 	return false;
-#if __cpp_rtti
-      return __a._M_target->_M_equals(__b._M_target.get());
-#else
-      return false; // XXX can we do better?
-#endif
+      if (__ta->_M_func == __tb->_M_func)
+	return __ta->_M_func(__ta, __tb);
+      return __ta->_M_equals(__tb);
     }
 
     shared_ptr<_Tgt> _M_target;
@@ -1223,6 +1287,22 @@ inline namespace v1
   operator==(const executor& __e, nullptr_t) noexcept
   { return !__e; }
 
+  inline bool
+  operator==(nullptr_t, const executor& __e) noexcept
+  { return !__e; }
+
+  inline bool
+  operator!=(const executor& __a, const executor& __b) noexcept
+  { return !(__a == __b); }
+
+  inline bool
+  operator!=(const executor& __e, nullptr_t) noexcept
+  { return (bool)__e; }
+
+  inline bool
+  operator!=(nullptr_t, const executor& __e) noexcept
+  { return (bool)__e; }
+
   /// Swap two executor objects.
   inline void swap(executor& __a, executor& __b) noexcept { __a.swap(__b); }
 
diff --git a/libstdc++-v3/include/experimental/socket b/libstdc++-v3/include/experimental/socket
index e80626528ce..cea17312dbd 100644
--- a/libstdc++-v3/include/experimental/socket
+++ b/libstdc++-v3/include/experimental/socket
@@ -79,6 +79,20 @@ inline namespace v1
     not_found = 4
   };
 
+} // namespace v1
+} // namespace net
+} // namespace experimental
+
+  template<>
+    struct is_error_code_enum<experimental::net::v1::socket_errc>
+    : public true_type {};
+
+namespace experimental
+{
+namespace net
+{
+inline namespace v1
+{
   const error_category& socket_category() noexcept
   {
     struct __cat : error_category
@@ -2602,10 +2616,6 @@ inline namespace v1
 } // namespace net
 } // namespace experimental
 
-  template<>
-    struct is_error_code_enum<experimental::net::v1::socket_errc>
-    : public true_type {};
-
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-05-11 18:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-11 18:54 [gcc(refs/vendors/ARM/heads/arm-9-branch)] libstdc++: Fix type-erasure in experimental::net::executor (PR 94203) Vidya Praveen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).