]>
Commit | Line | Data |
---|---|---|
52a11cbf | 1 | // -*- C++ -*- std::terminate, std::unexpected and friends. |
a945c346 | 2 | // Copyright (C) 1994-2024 Free Software Foundation, Inc. |
52a11cbf | 3 | // |
cbecceb9 | 4 | // This file is part of GCC. |
52a11cbf | 5 | // |
cbecceb9 | 6 | // GCC is free software; you can redistribute it and/or modify |
52a11cbf | 7 | // it under the terms of the GNU General Public License as published by |
748086b7 | 8 | // the Free Software Foundation; either version 3, or (at your option) |
52a11cbf RH |
9 | // any later version. |
10 | // | |
cbecceb9 | 11 | // GCC is distributed in the hope that it will be useful, |
52a11cbf RH |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | // | |
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
52a11cbf | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
52a11cbf RH |
24 | |
25 | #include "typeinfo" | |
26 | #include "exception" | |
27 | #include <cstdlib> | |
28 | #include "unwind-cxx.h" | |
3228289e | 29 | #include "eh_term_handler.h" |
7c3e9502 | 30 | #include <bits/exception_defines.h> |
7e20f4b0 JW |
31 | #include <bits/atomic_lockfree_defines.h> |
32 | ||
33 | #if ATOMIC_POINTER_LOCK_FREE < 2 | |
34 | #include <ext/concurrence.h> | |
35 | namespace | |
36 | { | |
37 | __gnu_cxx::__mutex mx; | |
38 | } | |
39 | #endif | |
52a11cbf RH |
40 | |
41 | using namespace __cxxabiv1; | |
42 | ||
52a11cbf | 43 | void |
5cf217ff | 44 | __cxxabiv1::__terminate (std::terminate_handler handler) throw () |
52a11cbf | 45 | { |
bbcfe54a BK |
46 | __try |
47 | { | |
48 | handler (); | |
49 | std::abort (); | |
50 | } | |
51 | __catch(...) | |
52 | { std::abort (); } | |
52a11cbf RH |
53 | } |
54 | ||
55 | void | |
5cf217ff | 56 | std::terminate () throw() |
52a11cbf | 57 | { |
250ddf4c | 58 | __cxxabiv1::__terminate (get_terminate ()); |
52a11cbf RH |
59 | } |
60 | ||
f4130a3e | 61 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
52a11cbf RH |
62 | void |
63 | __cxxabiv1::__unexpected (std::unexpected_handler handler) | |
64 | { | |
65 | handler(); | |
66 | std::terminate (); | |
67 | } | |
68 | ||
69 | void | |
70 | std::unexpected () | |
71 | { | |
dca77a8a | 72 | __unexpected (get_unexpected ()); |
52a11cbf RH |
73 | } |
74 | ||
75 | std::terminate_handler | |
76 | std::set_terminate (std::terminate_handler func) throw() | |
77 | { | |
3228289e JW |
78 | if (!func) |
79 | func = _GLIBCXX_DEFAULT_TERM_HANDLER; | |
80 | ||
dca77a8a | 81 | std::terminate_handler old; |
7e20f4b0 | 82 | #if ATOMIC_POINTER_LOCK_FREE > 1 |
dca77a8a | 83 | __atomic_exchange (&__terminate_handler, &func, &old, __ATOMIC_ACQ_REL); |
7e20f4b0 JW |
84 | #else |
85 | __gnu_cxx::__scoped_lock l(mx); | |
86 | old = __terminate_handler; | |
87 | __terminate_handler = func; | |
88 | #endif | |
52a11cbf RH |
89 | return old; |
90 | } | |
91 | ||
dca77a8a JW |
92 | std::terminate_handler |
93 | std::get_terminate () noexcept | |
94 | { | |
95 | std::terminate_handler func; | |
7e20f4b0 | 96 | #if ATOMIC_POINTER_LOCK_FREE > 1 |
dca77a8a | 97 | __atomic_load (&__terminate_handler, &func, __ATOMIC_ACQUIRE); |
7e20f4b0 JW |
98 | #else |
99 | __gnu_cxx::__scoped_lock l(mx); | |
100 | func = __terminate_handler; | |
101 | #endif | |
dca77a8a JW |
102 | return func; |
103 | } | |
104 | ||
52a11cbf RH |
105 | std::unexpected_handler |
106 | std::set_unexpected (std::unexpected_handler func) throw() | |
107 | { | |
3228289e JW |
108 | if (!func) |
109 | func = std::terminate; | |
110 | ||
dca77a8a | 111 | std::unexpected_handler old; |
7e20f4b0 | 112 | #if ATOMIC_POINTER_LOCK_FREE > 1 |
dca77a8a | 113 | __atomic_exchange (&__unexpected_handler, &func, &old, __ATOMIC_ACQ_REL); |
7e20f4b0 JW |
114 | #else |
115 | __gnu_cxx::__scoped_lock l(mx); | |
116 | old = __unexpected_handler; | |
117 | __unexpected_handler = func; | |
118 | #endif | |
52a11cbf RH |
119 | return old; |
120 | } | |
dca77a8a JW |
121 | |
122 | std::unexpected_handler | |
123 | std::get_unexpected () noexcept | |
124 | { | |
125 | std::unexpected_handler func; | |
7e20f4b0 | 126 | #if ATOMIC_POINTER_LOCK_FREE > 1 |
dca77a8a | 127 | __atomic_load (&__unexpected_handler, &func, __ATOMIC_ACQUIRE); |
7e20f4b0 JW |
128 | #else |
129 | __gnu_cxx::__scoped_lock l(mx); | |
130 | func = __unexpected_handler; | |
131 | #endif | |
dca77a8a JW |
132 | return func; |
133 | } |