]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/eh_tm.cc
Merge from transactional-memory branch.
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / eh_tm.cc
1 // -*- C++ -*- Exception handling routines for Transactional Memory.
2 // Copyright (C) 2009 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // GCC is distributed in the hope that it will be useful,
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 //
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.
19
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/>.
24
25 #include <cstdlib>
26 #include "unwind-cxx.h"
27
28 using namespace __cxxabiv1;
29
30 // Free one C++ exception.
31
32 static void
33 free_any_cxa_exception (_Unwind_Exception *eo)
34 {
35 __cxa_refcounted_exception *h
36 = __get_refcounted_exception_header_from_ue (eo);
37
38 if (__is_dependent_exception (eo->exception_class))
39 {
40 __cxa_dependent_exception *dep
41 = __get_dependent_exception_from_ue (eo);
42
43 h = __get_refcounted_exception_header_from_obj (dep->primaryException);
44
45 __cxa_free_dependent_exception (dep);
46 }
47
48 if (__sync_sub_and_fetch (&h->referenceCount, 1) == 0)
49 __cxa_free_exception (h + 1);
50 }
51
52 // Cleanup exception handling state while rolling back state for
53 // a software transactional memory transaction.
54 //
55 // UNTHROWN_OBJ is non-null if we've called __cxa_allocate_exception
56 // but not yet called __cxa_throw for it.
57 //
58 // CLEANUP_EXC is non-null if we're currently processing a cleanup
59 // along an exception path, but we've not caught the exception yet.
60 //
61 // CAUGHT_COUNT is the nesting depth of __cxa_begin_catch within
62 // the transaction; undo as if calling __cxa_end_catch that many times.
63
64 extern "C" void
65 __cxxabiv1::__cxa_tm_cleanup (void *unthrown_obj,
66 void *cleanup_exc,
67 unsigned int caught_count) throw()
68 {
69 __cxa_eh_globals *globals = __cxa_get_globals_fast ();
70
71 // Handle a C++ exception not yet thrown.
72 if (unthrown_obj)
73 {
74 globals->uncaughtExceptions -= 1;
75 __cxa_free_exception (unthrown_obj);
76 }
77
78 // Handle an exception not yet caught ie. processing a cleanup
79 // in between the throw and the catch.
80 if (cleanup_exc)
81 {
82 _Unwind_Exception *eo
83 = reinterpret_cast <_Unwind_Exception *>(cleanup_exc);
84 if (__is_gxx_exception_class (eo->exception_class))
85 free_any_cxa_exception (eo);
86 else
87 _Unwind_DeleteException (eo);
88 }
89
90 // Do __cxa_end_catch caught_count times, but don't bother running
91 // the destructors for the objects involved. All of that is being
92 // undone by the transaction restart.
93 if (caught_count > 0)
94 {
95 __cxa_exception *h = globals->caughtExceptions;
96
97 // Rethrown foreign exceptions are removed from the stack immediately.
98 // We would have freed this exception via THIS_EXC above.
99 if (h == NULL)
100 return;
101
102 do
103 {
104 __cxa_exception *next;
105 _Unwind_Exception *eo = &h->unwindHeader;
106
107 if (__is_gxx_exception_class (eo->exception_class))
108 {
109 next = h->nextException;
110 free_any_cxa_exception (eo);
111 }
112 else
113 {
114 _Unwind_DeleteException (eo);
115 next = 0;
116 }
117
118 h = next;
119 }
120 while (--caught_count);
121
122 globals->caughtExceptions = h;
123 }
124 }