eh_cpp.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. using namespace GTM;
  21. /* Everything from libstdc++ is weak, to avoid requiring that library
  22. to be linked into plain C applications using libitm.so. */
  23. #define WEAK __attribute__((weak))
  24. extern "C" {
  25. extern void *__cxa_allocate_exception (size_t) WEAK;
  26. extern void __cxa_throw (void *, void *, void *) WEAK;
  27. extern void *__cxa_begin_catch (void *) WEAK;
  28. extern void __cxa_end_catch (void) WEAK;
  29. extern void __cxa_tm_cleanup (void *, void *, unsigned int) WEAK;
  30. #if !defined (HAVE_ELF_STYLE_WEAKREF)
  31. void *__cxa_allocate_exception (size_t) { return NULL; }
  32. void __cxa_throw (void *, void *, void *) { return; }
  33. void *__cxa_begin_catch (void *) { return NULL; }
  34. void __cxa_end_catch (void) { return; }
  35. void __cxa_tm_cleanup (void *, void *, unsigned int) { return; }
  36. void _Unwind_DeleteException (_Unwind_Exception *) { return; }
  37. #endif /* HAVE_ELF_STYLE_WEAKREF */
  38. }
  39. void *
  40. _ITM_cxa_allocate_exception (size_t size)
  41. {
  42. void *r = __cxa_allocate_exception (size);
  43. gtm_thr()->cxa_unthrown = r;
  44. return r;
  45. }
  46. void
  47. _ITM_cxa_throw (void *obj, void *tinfo, void *dest)
  48. {
  49. gtm_thr()->cxa_unthrown = NULL;
  50. __cxa_throw (obj, tinfo, dest);
  51. }
  52. void *
  53. _ITM_cxa_begin_catch (void *exc_ptr)
  54. {
  55. gtm_thr()->cxa_catch_count++;
  56. return __cxa_begin_catch (exc_ptr);
  57. }
  58. void
  59. _ITM_cxa_end_catch (void)
  60. {
  61. gtm_thr()->cxa_catch_count--;
  62. __cxa_end_catch ();
  63. }
  64. void
  65. GTM::gtm_thread::revert_cpp_exceptions (gtm_transaction_cp *cp)
  66. {
  67. if (cp)
  68. {
  69. // If rolling back a nested transaction, only clean up unthrown
  70. // exceptions since the last checkpoint. Always reset eh_in_flight
  71. // because it just contains the argument provided to
  72. // _ITM_commitTransactionEH
  73. void *unthrown =
  74. (cxa_unthrown != cp->cxa_unthrown ? cxa_unthrown : NULL);
  75. assert (cxa_catch_count >= cp->cxa_catch_count);
  76. uint32_t catch_count = cxa_catch_count - cp->cxa_catch_count;
  77. if (unthrown || catch_count)
  78. {
  79. __cxa_tm_cleanup (unthrown, this->eh_in_flight, catch_count);
  80. cxa_catch_count = cp->cxa_catch_count;
  81. cxa_unthrown = cp->cxa_unthrown;
  82. this->eh_in_flight = NULL;
  83. }
  84. }
  85. else
  86. {
  87. // Both cxa_catch_count and cxa_unthrown are maximal because EH regions
  88. // and transactions are properly nested.
  89. if (this->cxa_unthrown || this->cxa_catch_count)
  90. {
  91. __cxa_tm_cleanup (this->cxa_unthrown, this->eh_in_flight,
  92. this->cxa_catch_count);
  93. this->cxa_catch_count = 0;
  94. this->cxa_unthrown = NULL;
  95. this->eh_in_flight = NULL;
  96. }
  97. }
  98. }