On 22/09/16 11:16 +0100, Jonathan Wakely wrote:
(Somebody should fix PR58938 so exception_ptr is portable).

Christophe, would you be able to test this patch?

It uses a single global mutex for exception_ptr objects, which doesn't
scale well but that probably isn't a problem for processors without
lock-free atomics for single words.

This also solves the problem of mismatched -march options, where the
header is compiled for a CPU that supports the atomics but
libstdc++.so was built for an older CPU that doesn't support them, and
linking fails (as in https://gcc.gnu.org/PR58938#c13).


diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index e9bb0ce..363900a 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,26 @@
 2016-09-22  Jonathan Wakely  <jwak...@redhat.com>
 
+	PR libstdc++/58938
+	* libsupc++/eh_ptr.cc [ATOMIC_INT_LOCK_FREE < 2] (eh_ptr_mutex)
+	(exception_ptr::_M_addref, exception_ptr::_M_release): Use mutex if
+	atomic builtins not available.
+	* libsupc++/exception_ptr.h [ATOMIC_INT_LOCK_FREE < 2]: Remove #error.
+	* testsuite/18_support/exception_ptr/40296.cc: Remove
+	dg-require-atomic-builtins directive.
+	* testsuite/18_support/exception_ptr/60612-terminate.cc: Likewise.
+	* testsuite/18_support/exception_ptr/60612-unexpected.cc: Likewise.
+	* testsuite/18_support/exception_ptr/62258.cc: Likewise.
+	* testsuite/18_support/exception_ptr/64241.cc: Likewise.
+	* testsuite/18_support/exception_ptr/current_exception.cc: Likewise.
+	* testsuite/18_support/exception_ptr/lifespan.cc: Likewise.
+	* testsuite/18_support/exception_ptr/make_exception_ptr.cc: Likewise.
+	* testsuite/18_support/exception_ptr/move.cc: Likewise.
+	* testsuite/18_support/exception_ptr/requirements.cc: Likewise.
+	* testsuite/18_support/exception_ptr/requirements_neg.cc: Likewise.
+	* testsuite/18_support/exception_ptr/rethrow_exception.cc: Likewise.
+
+2016-09-22  Jonathan Wakely  <jwak...@redhat.com>
+
 	* python/libstdcxx/v6/printers.py (StdVariantPrinter): Adjust for
 	recent change to _Variant_storage.
 	* testsuite/libstdc++-prettyprinters/cxx17.cc: Test variant with
diff --git a/libstdc++-v3/libsupc++/eh_ptr.cc b/libstdc++-v3/libsupc++/eh_ptr.cc
index 3b8e0a01..ac4e375 100644
--- a/libstdc++-v3/libsupc++/eh_ptr.cc
+++ b/libstdc++-v3/libsupc++/eh_ptr.cc
@@ -25,14 +25,23 @@
 #include <bits/c++config.h>
 #include <bits/atomic_lockfree_defines.h>
 
-#if ATOMIC_INT_LOCK_FREE > 1
-
 #define _GLIBCXX_EH_PTR_COMPAT
 
 #include <exception>
 #include <bits/exception_ptr.h>
 #include "unwind-cxx.h"
 
+#if ATOMIC_INT_LOCK_FREE < 2
+# include <ext/concurrence.h>
+# define USE_EH_PTR_MUTEX
+static inline __gnu_cxx::__mutex& eh_ptr_mutex()
+{
+    static __gnu_cxx::__mutex mx;
+    return mx;
+}
+using __gnu_cxx::__scoped_lock;
+#endif
+
 using namespace __cxxabiv1;
 
 // Verify assumptions about member layout in exception types
@@ -103,7 +112,12 @@ std::__exception_ptr::exception_ptr::_M_addref() _GLIBCXX_USE_NOEXCEPT
     {
       __cxa_refcounted_exception *eh =
 	__get_refcounted_exception_header_from_obj (_M_exception_object);
+#ifdef USE_EH_PTR_MUTEX
+      __scoped_lock lock(eh_ptr_mutex());
+      ++eh->referenceCount;
+#else
       __atomic_add_fetch (&eh->referenceCount, 1, __ATOMIC_ACQ_REL);
+#endif
     }
 }
 
@@ -115,7 +129,12 @@ std::__exception_ptr::exception_ptr::_M_release() _GLIBCXX_USE_NOEXCEPT
     {
       __cxa_refcounted_exception *eh =
 	__get_refcounted_exception_header_from_obj (_M_exception_object);
+#ifdef USE_EH_PTR_MUTEX
+      auto count = (__scoped_lock(eh_ptr_mutex()), --eh->referenceCount);
+      if (count == 0)
+#else
       if (__atomic_sub_fetch (&eh->referenceCount, 1, __ATOMIC_ACQ_REL) == 0)
+#endif
         {
 	  if (eh->exc.exceptionDestructor)
 	    eh->exc.exceptionDestructor (_M_exception_object);
@@ -260,5 +279,3 @@ std::rethrow_exception(std::exception_ptr ep)
 }
 
 #undef _GLIBCXX_EH_PTR_COMPAT
-
-#endif
diff --git a/libstdc++-v3/libsupc++/exception_ptr.h b/libstdc++-v3/libsupc++/exception_ptr.h
index 21e4e8b..ae8fb1f5 100644
--- a/libstdc++-v3/libsupc++/exception_ptr.h
+++ b/libstdc++-v3/libsupc++/exception_ptr.h
@@ -39,10 +39,6 @@
 #include <typeinfo>
 #include <new>
 
-#if ATOMIC_INT_LOCK_FREE < 2
-#  error This platform does not support exception propagation.
-#endif
-
 extern "C++" {
 
 namespace std 
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/40296.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/40296.cc
index 74307cc..a6659b8 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/40296.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/40296.cc
@@ -1,5 +1,4 @@
 // { dg-do compile { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2009-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/60612-terminate.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/60612-terminate.cc
index c00e287..d58f32d 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/60612-terminate.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/60612-terminate.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/60612-unexpected.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/60612-unexpected.cc
index 2b5ec2d..cfaea5d4 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/60612-unexpected.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/60612-unexpected.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc
index c83d5fb..5c15b48 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2015-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/64241.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/64241.cc
index fbb01ae..b7352ed 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/64241.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/64241.cc
@@ -17,7 +17,6 @@
 
 // { dg-options "-fno-exceptions -O0" }
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 #include <exception>
 #include <testsuite_hooks.h>
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/current_exception.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/current_exception.cc
index 2383cee..02bcd27 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/current_exception.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/current_exception.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // 2008-05-25  Sebastian Redl  <sebastian.r...@getdesigned.at>
 
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/lifespan.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/lifespan.cc
index e350df3..5c8cb11 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/lifespan.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/lifespan.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // 2008-05-25  Sebastian Redl  <sebastian.r...@getdesigned.at>
 
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/make_exception_ptr.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/make_exception_ptr.cc
index b496bb3..b9538e6 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/make_exception_ptr.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/make_exception_ptr.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2010-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/move.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/move.cc
index 6318bc4..80c6e3e 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/move.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/move.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2009-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/requirements.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/requirements.cc
index b3b9e0c..5f5f054 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/requirements.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/requirements.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2010-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/requirements_neg.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/requirements_neg.cc
index b80d688..95ec8ab 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/requirements_neg.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/requirements_neg.cc
@@ -1,5 +1,4 @@
 // { dg-do compile { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // Copyright (C) 2010-2016 Free Software Foundation, Inc.
 //
diff --git a/libstdc++-v3/testsuite/18_support/exception_ptr/rethrow_exception.cc b/libstdc++-v3/testsuite/18_support/exception_ptr/rethrow_exception.cc
index f0b61b1..c3ce206 100644
--- a/libstdc++-v3/testsuite/18_support/exception_ptr/rethrow_exception.cc
+++ b/libstdc++-v3/testsuite/18_support/exception_ptr/rethrow_exception.cc
@@ -1,5 +1,4 @@
 // { dg-do run { target c++11 } }
-// { dg-require-atomic-builtins "" }
 
 // 2008-05-25  Sebastian Redl  <sebastian.r...@getdesigned.at>
 

Reply via email to