OSDN Git Service

Merge from transactional-memory branch.
[pf3gnuchains/gcc-fork.git] / libitm / eh_cpp.cc
1 /* Copyright (C) 2009, 2011 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3
4    This file is part of the GNU Transactional Memory Library (libitm).
5
6    Libitm is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14    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 "libitm_i.h"
26
27 using namespace GTM;
28
29 /* Everything from libstdc++ is weak, to avoid requiring that library
30    to be linked into plain C applications using libitm.so.  */
31
32 #define WEAK  __attribute__((weak))
33
34 extern "C" {
35
36 extern void *__cxa_allocate_exception (size_t) WEAK;
37 extern void __cxa_throw (void *, void *, void *) WEAK;
38 extern void *__cxa_begin_catch (void *) WEAK;
39 extern void *__cxa_end_catch (void) WEAK;
40 extern void __cxa_tm_cleanup (void *, void *, unsigned int) WEAK;
41
42 }
43
44
45 void *
46 _ITM_cxa_allocate_exception (size_t size)
47 {
48   void *r = __cxa_allocate_exception (size);
49   gtm_thr()->cxa_unthrown = r;
50   return r;
51 }
52
53 void
54 _ITM_cxa_throw (void *obj, void *tinfo, void *dest)
55 {
56   gtm_thr()->cxa_unthrown = NULL;
57   __cxa_throw (obj, tinfo, dest);
58 }
59
60 void *
61 _ITM_cxa_begin_catch (void *exc_ptr)
62 {
63   gtm_thr()->cxa_catch_count++;
64   return __cxa_begin_catch (exc_ptr);
65 }
66
67 void
68 _ITM_cxa_end_catch (void)
69 {
70   gtm_thr()->cxa_catch_count--;
71   __cxa_end_catch ();
72 }
73
74 void
75 GTM::gtm_thread::revert_cpp_exceptions (gtm_transaction_cp *cp)
76 {
77   if (cp)
78     {
79       // If rolling back a nested transaction, only clean up unthrown
80       // exceptions since the last checkpoint. Always reset eh_in_flight
81       // because it just contains the argument provided to
82       // _ITM_commitTransactionEH
83       void *unthrown =
84           (cxa_unthrown != cp->cxa_unthrown ? cxa_unthrown : NULL);
85       assert (cxa_catch_count >= cp->cxa_catch_count);
86       uint32_t catch_count = cxa_catch_count - cp->cxa_catch_count;
87       if (unthrown || catch_count)
88         {
89           __cxa_tm_cleanup (unthrown, this->eh_in_flight, catch_count);
90           cxa_catch_count = cp->cxa_catch_count;
91           cxa_unthrown = cp->cxa_unthrown;
92           this->eh_in_flight = NULL;
93         }
94     }
95   else
96     {
97       // Both cxa_catch_count and cxa_unthrown are maximal because EH regions
98       // and transactions are properly nested.
99       if (this->cxa_unthrown || this->cxa_catch_count)
100         {
101           __cxa_tm_cleanup (this->cxa_unthrown, this->eh_in_flight,
102               this->cxa_catch_count);
103           this->cxa_catch_count = 0;
104           this->cxa_unthrown = NULL;
105           this->eh_in_flight = NULL;
106         }
107     }
108 }