eh_atomics.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Exception Handling support header for -*- C++ -*-
  2. // Copyright (C) 2016-2022 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. // You should have received a copy of the GNU General Public License and
  20. // a copy of the GCC Runtime Library Exception along with this program;
  21. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  22. // <http://www.gnu.org/licenses/>.
  23. /** @file eh_atomics.h
  24. * This is an internal header file, included by library source files.
  25. * Do not attempt to use it directly.
  26. */
  27. #ifndef _EH_ATOMICS_H
  28. #define _EH_ATOMICS_H 1
  29. #include <bits/c++config.h>
  30. #include <bits/atomic_word.h>
  31. #include <bits/atomic_lockfree_defines.h>
  32. #if ATOMIC_INT_LOCK_FREE <= 1
  33. # include <ext/atomicity.h>
  34. #endif
  35. #pragma GCC visibility push(default)
  36. extern "C++" {
  37. namespace __gnu_cxx
  38. {
  39. void
  40. __eh_atomic_inc (_Atomic_word* __count) __attribute__((always_inline));
  41. bool
  42. __eh_atomic_dec (_Atomic_word* __count) __attribute__((always_inline));
  43. // Increments the count.
  44. inline void
  45. __eh_atomic_inc (_Atomic_word* __count)
  46. {
  47. #if ATOMIC_INT_LOCK_FREE > 1
  48. __atomic_add_fetch (__count, 1, __ATOMIC_ACQ_REL);
  49. #else
  50. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE (__count);
  51. __gnu_cxx::__atomic_add_dispatch (__count, 1);
  52. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER (__count);
  53. #endif
  54. }
  55. // Decrements the count and returns true if it reached zero.
  56. inline bool
  57. __eh_atomic_dec (_Atomic_word* __count)
  58. {
  59. #if ATOMIC_INT_LOCK_FREE > 1
  60. return __atomic_sub_fetch (__count, 1, __ATOMIC_ACQ_REL) == 0;
  61. #else
  62. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE (__count);
  63. if (__gnu_cxx::__exchange_and_add_dispatch (__count, -1) == 1)
  64. {
  65. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER (__count);
  66. return true;
  67. }
  68. return false;
  69. #endif
  70. }
  71. } // namespace __gnu_cxx
  72. }
  73. #pragma GCC visibility pop
  74. #endif // _EH_ATOMICS_H