Howard Hinnant | 44d4d3b | 2011-12-06 17:51:25 +0000 | [diff] [blame] | 1 | //===------------------------- cxa_handlers.cpp ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | // |
| 9 | // This file implements the functionality associated with the terminate_handler, |
| 10 | // unexpected_handler, and new_handler. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include <stdexcept> |
| 14 | #include <new> |
| 15 | #include <exception> |
| 16 | #include "abort_message.h" |
| 17 | |
| 18 | namespace std |
| 19 | { |
| 20 | |
| 21 | static const char* cause = "uncaught"; |
| 22 | |
| 23 | static void default_terminate_handler() |
| 24 | { |
| 25 | std::exception_ptr cp = std::current_exception(); |
| 26 | if (cp) |
| 27 | { |
| 28 | try |
| 29 | { |
| 30 | rethrow_exception(cp); |
| 31 | } |
| 32 | catch (const std::exception& e) |
| 33 | { |
| 34 | abort_message("terminating with %s exception: %s\n", cause, e.what()); |
| 35 | } |
| 36 | catch (...) |
| 37 | { |
| 38 | abort_message("terminating with %s exception\n", cause); |
| 39 | } |
| 40 | } |
| 41 | abort_message("terminating\n"); |
| 42 | } |
| 43 | |
| 44 | static void default_unexpected_handler() |
| 45 | { |
| 46 | cause = "unexpected"; |
| 47 | terminate(); |
| 48 | } |
| 49 | |
| 50 | static terminate_handler __terminate_handler = default_terminate_handler; |
| 51 | static unexpected_handler __unexpected_handler = default_unexpected_handler; |
| 52 | static new_handler __new_handler = 0; |
| 53 | |
| 54 | unexpected_handler |
| 55 | set_unexpected(unexpected_handler func) _NOEXCEPT |
| 56 | { |
| 57 | if (func == 0) |
| 58 | func = default_unexpected_handler; |
| 59 | return __sync_lock_test_and_set(&__unexpected_handler, func); |
| 60 | } |
| 61 | |
| 62 | unexpected_handler |
| 63 | get_unexpected() _NOEXCEPT |
| 64 | { |
| 65 | return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); |
| 66 | } |
| 67 | |
| 68 | _LIBCPP_HIDDEN |
| 69 | _ATTRIBUTE(noreturn) |
| 70 | void |
| 71 | __unexpected(unexpected_handler func) |
| 72 | { |
| 73 | func(); |
| 74 | // unexpected handler should not return |
| 75 | abort_message("unexpected_handler unexpectedly returned"); |
| 76 | } |
| 77 | |
| 78 | _ATTRIBUTE(noreturn) |
| 79 | void |
| 80 | unexpected() |
| 81 | { |
| 82 | __unexpected(get_unexpected()); |
| 83 | } |
| 84 | |
| 85 | terminate_handler |
| 86 | set_terminate(terminate_handler func) _NOEXCEPT |
| 87 | { |
| 88 | if (func == 0) |
| 89 | func = default_terminate_handler; |
| 90 | return __sync_lock_test_and_set(&__terminate_handler, func); |
| 91 | } |
| 92 | |
| 93 | terminate_handler |
| 94 | get_terminate() _NOEXCEPT |
| 95 | { |
| 96 | return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); |
| 97 | } |
| 98 | |
| 99 | _LIBCPP_HIDDEN |
| 100 | _ATTRIBUTE(noreturn) |
| 101 | void |
| 102 | __terminate(terminate_handler func) _NOEXCEPT |
| 103 | { |
| 104 | #if __has_feature(cxx_exceptions) |
| 105 | try |
| 106 | { |
| 107 | #endif // __has_feature(cxx_exceptions) |
| 108 | func(); |
| 109 | // handler should not return |
| 110 | abort_message("terminate_handler unexpectedly returned"); |
| 111 | #if __has_feature(cxx_exceptions) |
| 112 | } |
| 113 | catch (...) |
| 114 | { |
| 115 | // handler should not throw exception |
| 116 | abort_message("terminate_handler unexpectedly threw an exception"); |
| 117 | } |
| 118 | #endif // #if __has_feature(cxx_exceptions) |
| 119 | } |
| 120 | |
| 121 | _ATTRIBUTE(noreturn) |
| 122 | void |
| 123 | terminate() _NOEXCEPT |
| 124 | { |
| 125 | __terminate(get_terminate()); |
| 126 | } |
| 127 | |
| 128 | new_handler |
| 129 | set_new_handler(new_handler handler) _NOEXCEPT |
| 130 | { |
| 131 | return __sync_lock_test_and_set(&__new_handler, handler); |
| 132 | } |
| 133 | |
| 134 | new_handler |
| 135 | get_new_handler() _NOEXCEPT |
| 136 | { |
| 137 | return __sync_fetch_and_add(&__new_handler, (new_handler)0); |
| 138 | } |
| 139 | |
| 140 | } // std |