[cfe-commits] [libcxx] r120712 - in /libcxx/trunk: include/exception include/future include/new src/exception.cpp src/new.cpp test/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp test/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp test/language.support/support.exception/exception.terminate/set.terminate/get_terminate.pass.cpp

Howard Hinnant hhinnant at apple.com
Thu Dec 2 08:45:21 PST 2010


Author: hhinnant
Date: Thu Dec  2 10:45:21 2010
New Revision: 120712

URL: http://llvm.org/viewvc/llvm-project?rev=120712&view=rev
Log:
N3189 Observers for the three handler functions

Added:
    libcxx/trunk/test/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp
    libcxx/trunk/test/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
    libcxx/trunk/test/language.support/support.exception/exception.terminate/set.terminate/get_terminate.pass.cpp
Modified:
    libcxx/trunk/include/exception
    libcxx/trunk/include/future
    libcxx/trunk/include/new
    libcxx/trunk/src/exception.cpp
    libcxx/trunk/src/new.cpp

Modified: libcxx/trunk/include/exception
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/exception?rev=120712&r1=120711&r2=120712&view=diff
==============================================================================
--- libcxx/trunk/include/exception (original)
+++ libcxx/trunk/include/exception Thu Dec  2 10:45:21 2010
@@ -40,10 +40,12 @@
 
 typedef void (*unexpected_handler)();
 unexpected_handler set_unexpected(unexpected_handler  f ) throw();
+unexpected_handler get_unexpected() throw();
 void unexpected [[noreturn]] ();
 
 typedef void (*terminate_handler)();
 terminate_handler set_terminate(terminate_handler  f ) throw();
+terminate_handler get_terminate() throw();
 void terminate [[noreturn]] ();
 
 bool uncaught_exception() throw();
@@ -102,10 +104,12 @@
 
 typedef void (*unexpected_handler)();
 _LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) throw();
+_LIBCPP_VISIBLE unexpected_handler get_unexpected() throw();
 _LIBCPP_VISIBLE void unexpected();
 
 typedef void (*terminate_handler)();
 _LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) throw();
+_LIBCPP_VISIBLE terminate_handler get_terminate() throw();
 _LIBCPP_VISIBLE void terminate() __attribute__((__noreturn__));
 
 _LIBCPP_VISIBLE bool uncaught_exception() throw();

Modified: libcxx/trunk/include/future
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/future?rev=120712&r1=120711&r2=120712&view=diff
==============================================================================
--- libcxx/trunk/include/future (original)
+++ libcxx/trunk/include/future Thu Dec  2 10:45:21 2010
@@ -2297,6 +2297,8 @@
     return shared_future<_R&>(_STD::move(*this));
 }
 
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+
 inline _LIBCPP_INLINE_VISIBILITY
 shared_future<void>
 future<void>::share()
@@ -2304,6 +2306,8 @@
     return shared_future<void>(_STD::move(*this));
 }
 
+#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_FUTURE

Modified: libcxx/trunk/include/new
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/new?rev=120712&r1=120711&r2=120712&view=diff
==============================================================================
--- libcxx/trunk/include/new (original)
+++ libcxx/trunk/include/new Thu Dec  2 10:45:21 2010
@@ -32,6 +32,7 @@
 extern const nothrow_t nothrow;
 typedef void (*new_handler)();
 new_handler set_new_handler(new_handler new_p) throw();
+new_handler get_new_handler() throw();
 
 }  // std
 
@@ -85,6 +86,7 @@
 extern _LIBCPP_VISIBLE const nothrow_t nothrow;
 typedef void (*new_handler)();
 _LIBCPP_VISIBLE new_handler set_new_handler(new_handler) throw();
+_LIBCPP_VISIBLE new_handler get_new_handler() throw();
 
 }  // std
 

Modified: libcxx/trunk/src/exception.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/exception.cpp?rev=120712&r1=120711&r2=120712&view=diff
==============================================================================
--- libcxx/trunk/src/exception.cpp (original)
+++ libcxx/trunk/src/exception.cpp Thu Dec  2 10:45:21 2010
@@ -26,9 +26,13 @@
 std::unexpected_handler
 std::set_unexpected(std::unexpected_handler func) throw()
 {
-    std::terminate_handler old = __unexpected_handler;
-    __unexpected_handler = func;
-    return old;
+    return __sync_lock_test_and_set(&__unexpected_handler, func);
+}
+
+std::unexpected_handler
+std::get_unexpected() throw()
+{
+    return __sync_fetch_and_add(&__unexpected_handler, (std::unexpected_handler)0);
 }
 
 void
@@ -42,9 +46,13 @@
 std::terminate_handler
 std::set_terminate(std::terminate_handler func) throw()
 {
-    std::terminate_handler old = __terminate_handler;
-    __terminate_handler = func;
-    return old;
+    return __sync_lock_test_and_set(&__terminate_handler, func);
+}
+
+std::terminate_handler
+std::get_terminate() throw()
+{
+    return __sync_fetch_and_add(&__terminate_handler, (std::terminate_handler)0);
 }
 
 void

Modified: libcxx/trunk/src/new.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/new.cpp?rev=120712&r1=120711&r2=120712&view=diff
==============================================================================
--- libcxx/trunk/src/new.cpp (original)
+++ libcxx/trunk/src/new.cpp Thu Dec  2 10:45:21 2010
@@ -130,9 +130,13 @@
 new_handler
 set_new_handler(new_handler handler) throw()
 {
-    new_handler r = __new_handler;
-    __new_handler = handler;
-    return r;
+    return __sync_lock_test_and_set(&__new_handler, handler);
+}
+
+new_handler
+get_new_handler() throw()
+{
+    return __sync_fetch_and_add(&__new_handler, (new_handler)0);
 }
 
 bad_alloc::bad_alloc() throw()

Added: libcxx/trunk/test/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp?rev=120712&view=auto
==============================================================================
--- libcxx/trunk/test/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp (added)
+++ libcxx/trunk/test/depr/exception.unexpected/set.unexpected/get_unexpected.pass.cpp Thu Dec  2 10:45:21 2010
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// test get_unexpected
+
+#include <exception>
+#include <cassert>
+#include <cstdlib>
+
+void f1() {}
+void f2() {}
+
+void f3()
+{
+    std::exit(0);
+}
+
+int main()
+{
+    
+    std::unexpected_handler old = std::get_unexpected();
+    // verify there is a previous unexpected handler
+    assert(old);
+    std::set_unexpected(f1);
+    assert(std::get_unexpected() == f1);
+    // verify f1 was replace with f2
+    std::set_unexpected(f2);
+    assert(std::get_unexpected() == f2);
+    // verify calling original unexpected handler calls terminate
+    std::set_terminate(f3);
+    (*old)();
+    assert(0);
+}

Added: libcxx/trunk/test/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp?rev=120712&view=auto
==============================================================================
--- libcxx/trunk/test/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp (added)
+++ libcxx/trunk/test/language.support/support.dynamic/alloc.errors/set.new.handler/get_new_handler.pass.cpp Thu Dec  2 10:45:21 2010
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// test get_new_handler
+
+#include <new>
+#include <cassert>
+
+void f1() {}
+void f2() {}
+
+int main()
+{
+    assert(std::get_new_handler() == 0);
+    std::set_new_handler(f1);
+    assert(std::get_new_handler() == f1);
+    std::set_new_handler(f2);
+    assert(std::get_new_handler() == f2);
+}

Added: libcxx/trunk/test/language.support/support.exception/exception.terminate/set.terminate/get_terminate.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/language.support/support.exception/exception.terminate/set.terminate/get_terminate.pass.cpp?rev=120712&view=auto
==============================================================================
--- libcxx/trunk/test/language.support/support.exception/exception.terminate/set.terminate/get_terminate.pass.cpp (added)
+++ libcxx/trunk/test/language.support/support.exception/exception.terminate/set.terminate/get_terminate.pass.cpp Thu Dec  2 10:45:21 2010
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// test get_terminate
+
+#include <exception>
+#include <cstdlib>
+#include <cassert>
+
+void f1() {}
+void f2() {}
+
+int main()
+{
+    std::set_terminate(f1);
+    assert(std::get_terminate() == f1);
+    std::set_terminate(f2);
+    assert(std::get_terminate() == f2);
+}





More information about the cfe-commits mailing list