[cfe-dev] [PATCH] Replaces __sync_swap with std::atomic

Dan Albert danalbert at google.com
Tue May 6 15:32:31 PDT 2014


The commented out regions mentioning this worked as is.
---
 src/cxa_default_handlers.cpp | 16 ++++------------
 src/cxa_handlers.cpp         | 23 +++++------------------
 src/cxa_handlers.hpp         | 27 +++++----------------------
 3 files changed, 14 insertions(+), 52 deletions(-)

diff --git a/src/cxa_default_handlers.cpp b/src/cxa_default_handlers.cpp
index 27ffb71..6a48cd9 100644
--- a/src/cxa_default_handlers.cpp
+++ b/src/cxa_default_handlers.cpp
@@ -87,12 +87,8 @@ static void default_unexpected_handler()
 //
 // Global variables that hold the pointers to the current handler
 //
-std::terminate_handler  __cxa_terminate_handler = default_terminate_handler;
-std::unexpected_handler __cxa_unexpected_handler = default_unexpected_handler;
-
-// In the future these will become:
-// std::atomic<std::terminate_handler>  __cxa_terminate_handler(default_terminate_handler);
-// std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
+std::atomic<std::terminate_handler>  __cxa_terminate_handler(default_terminate_handler);
+std::atomic<std::unexpected_handler> __cxa_unexpected_handler(default_unexpected_handler);
 
 namespace std
 {
@@ -102,9 +98,7 @@ set_unexpected(unexpected_handler func) _NOEXCEPT
 {
 	if (func == 0)
 		func = default_unexpected_handler;
-	return __sync_swap(&__cxa_unexpected_handler, func);
-//  Using of C++11 atomics this should be rewritten
-//  return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
+	return __cxa_unexpected_handler.exchange(func, memory_order_acq_rel);
 }
 
 terminate_handler
@@ -112,9 +106,7 @@ set_terminate(terminate_handler func) _NOEXCEPT
 {
 	if (func == 0)
 		func = default_terminate_handler;
-	return __sync_swap(&__cxa_terminate_handler, func);
-//  Using of C++11 atomics this should be rewritten
-//  return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
+	return __cxa_terminate_handler.exchange(func, memory_order_acq_rel);
 }
 
 }
diff --git a/src/cxa_handlers.cpp b/src/cxa_handlers.cpp
index 6c13fcd..2d1682e 100644
--- a/src/cxa_handlers.cpp
+++ b/src/cxa_handlers.cpp
@@ -25,10 +25,7 @@ namespace std
 unexpected_handler
 get_unexpected() _NOEXCEPT
 {
-    return __sync_fetch_and_add(&__cxa_unexpected_handler, (unexpected_handler)0);
-//  The above is safe but overkill on x86
-//  Using of C++11 atomics this should be rewritten
-//  return __cxa_unexpected_handler.load(memory_order_acq);
+	return __cxa_unexpected_handler.load(memory_order_acquire);
 }
 
 __attribute__((visibility("hidden"), noreturn))
@@ -50,10 +47,7 @@ unexpected()
 terminate_handler
 get_terminate() _NOEXCEPT
 {
-    return __sync_fetch_and_add(&__cxa_terminate_handler, (terminate_handler)0);
-//  The above is safe but overkill on x86
-//  Using of C++11 atomics this should be rewritten
-//  return __cxa_terminate_handler.load(memory_order_acq);
+	return __cxa_terminate_handler.load(memory_order_acquire);
 }
 
 __attribute__((visibility("hidden"), noreturn))
@@ -101,25 +95,18 @@ terminate() _NOEXCEPT
     __terminate(get_terminate());
 }
 
-extern "C" new_handler __cxa_new_handler = 0;
-// In the future these will become:
-// std::atomic<std::new_handler>  __cxa_new_handler(0);
+std::atomic<std::new_handler>  __cxa_new_handler(0);
 
 new_handler
 set_new_handler(new_handler handler) _NOEXCEPT
 {
-    return __sync_swap(&__cxa_new_handler, handler);
-//  Using of C++11 atomics this should be rewritten
-//  return __cxa_new_handler.exchange(handler, memory_order_acq_rel);
+	return __cxa_new_handler.exchange(handler, memory_order_acq_rel);
 }
 
 new_handler
 get_new_handler() _NOEXCEPT
 {
-    return __sync_fetch_and_add(&__cxa_new_handler, (new_handler)0);
-//  The above is safe but overkill on x86
-//  Using of C++11 atomics this should be rewritten
-//  return __cxa_new_handler.load(memory_order_acq);
+	return __cxa_new_handler.load(memory_order_acquire);
 }
 
 }  // std
diff --git a/src/cxa_handlers.hpp b/src/cxa_handlers.hpp
index ce567ec..23669fe 100644
--- a/src/cxa_handlers.hpp
+++ b/src/cxa_handlers.hpp
@@ -13,7 +13,9 @@
 #ifndef _CXA_HANDLERS_H
 #define _CXA_HANDLERS_H
 
+#include <atomic>
 #include <exception>
+#include <new>
 
 namespace std
 {
@@ -28,27 +30,8 @@ __terminate(terminate_handler func) _NOEXCEPT;
 
 }  // std
 
-extern "C"
-{
-
-extern void (*__cxa_terminate_handler)();
-extern void (*__cxa_unexpected_handler)();
-extern void (*__cxa_new_handler)();
-
-/*
-
-    At some point in the future these three symbols will become
-    C++11 atomic variables:
-
-    extern std::atomic<std::terminate_handler>  __cxa_terminate_handler;
-    extern std::atomic<std::unexpected_handler> __cxa_unexpected_handler;
-    extern std::atomic<std::new_handler>        __cxa_new_handler;
-
-    This change will not impact their ABI.  But it will allow for a
-    portable performance optimization.
-
-*/
-
-} // extern "C"
+extern std::atomic<std::terminate_handler>  __cxa_terminate_handler;
+extern std::atomic<std::unexpected_handler> __cxa_unexpected_handler;
+extern std::atomic<std::new_handler>        __cxa_new_handler;
 
 #endif  // _CXA_HANDLERS_H
-- 
1.9.2




More information about the cfe-dev mailing list