atomicity.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Support for atomic operations -*- C++ -*-
  2. // Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/atomicity.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_ATOMICITY_H
  24. #define _GLIBCXX_ATOMICITY_H 1
  25. #pragma GCC system_header
  26. #include <bits/c++config.h>
  27. #include <bits/gthr.h>
  28. #include <bits/atomic_word.h>
  29. #if __has_include(<sys/single_threaded.h>)
  30. # include <sys/single_threaded.h>
  31. #endif
  32. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. __attribute__((__always_inline__))
  36. inline bool
  37. __is_single_threaded() _GLIBCXX_NOTHROW
  38. {
  39. #ifndef __GTHREADS
  40. return true;
  41. #elif __has_include(<sys/single_threaded.h>)
  42. return ::__libc_single_threaded;
  43. #else
  44. return !__gthread_active_p();
  45. #endif
  46. }
  47. // Functions for portable atomic access.
  48. // To abstract locking primitives across all thread policies, use:
  49. // __exchange_and_add_dispatch
  50. // __atomic_add_dispatch
  51. #ifdef _GLIBCXX_ATOMIC_BUILTINS
  52. inline _Atomic_word
  53. __attribute__((__always_inline__))
  54. __exchange_and_add(volatile _Atomic_word* __mem, int __val)
  55. { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
  56. inline void
  57. __attribute__((__always_inline__))
  58. __atomic_add(volatile _Atomic_word* __mem, int __val)
  59. { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
  60. #else
  61. _Atomic_word
  62. __exchange_and_add(volatile _Atomic_word*, int) _GLIBCXX_NOTHROW;
  63. void
  64. __atomic_add(volatile _Atomic_word*, int) _GLIBCXX_NOTHROW;
  65. #endif
  66. inline _Atomic_word
  67. __attribute__((__always_inline__))
  68. __exchange_and_add_single(_Atomic_word* __mem, int __val)
  69. {
  70. _Atomic_word __result = *__mem;
  71. *__mem += __val;
  72. return __result;
  73. }
  74. inline void
  75. __attribute__((__always_inline__))
  76. __atomic_add_single(_Atomic_word* __mem, int __val)
  77. { *__mem += __val; }
  78. inline _Atomic_word
  79. __attribute__ ((__always_inline__))
  80. __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
  81. {
  82. if (__is_single_threaded())
  83. return __exchange_and_add_single(__mem, __val);
  84. else
  85. return __exchange_and_add(__mem, __val);
  86. }
  87. inline void
  88. __attribute__ ((__always_inline__))
  89. __atomic_add_dispatch(_Atomic_word* __mem, int __val)
  90. {
  91. if (__is_single_threaded())
  92. __atomic_add_single(__mem, __val);
  93. else
  94. __atomic_add(__mem, __val);
  95. }
  96. _GLIBCXX_END_NAMESPACE_VERSION
  97. } // namespace
  98. // Even if the CPU doesn't need a memory barrier, we need to ensure
  99. // that the compiler doesn't reorder memory accesses across the
  100. // barriers.
  101. #ifndef _GLIBCXX_READ_MEM_BARRIER
  102. #define _GLIBCXX_READ_MEM_BARRIER __atomic_thread_fence (__ATOMIC_ACQUIRE)
  103. #endif
  104. #ifndef _GLIBCXX_WRITE_MEM_BARRIER
  105. #define _GLIBCXX_WRITE_MEM_BARRIER __atomic_thread_fence (__ATOMIC_RELEASE)
  106. #endif
  107. #endif