atomicity.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Support for atomic operations -*- C++ -*-
  2. // Copyright (C) 2004-2015 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. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. // Functions for portable atomic access.
  33. // To abstract locking primitives across all thread policies, use:
  34. // __exchange_and_add_dispatch
  35. // __atomic_add_dispatch
  36. #ifdef _GLIBCXX_ATOMIC_BUILTINS
  37. static inline _Atomic_word
  38. __exchange_and_add(volatile _Atomic_word* __mem, int __val)
  39. { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
  40. static inline void
  41. __atomic_add(volatile _Atomic_word* __mem, int __val)
  42. { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
  43. #else
  44. _Atomic_word
  45. __attribute__ ((__unused__))
  46. __exchange_and_add(volatile _Atomic_word*, int) throw ();
  47. void
  48. __attribute__ ((__unused__))
  49. __atomic_add(volatile _Atomic_word*, int) throw ();
  50. #endif
  51. static inline _Atomic_word
  52. __exchange_and_add_single(_Atomic_word* __mem, int __val)
  53. {
  54. _Atomic_word __result = *__mem;
  55. *__mem += __val;
  56. return __result;
  57. }
  58. static inline void
  59. __atomic_add_single(_Atomic_word* __mem, int __val)
  60. { *__mem += __val; }
  61. static inline _Atomic_word
  62. __attribute__ ((__unused__))
  63. __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
  64. {
  65. #ifdef __GTHREADS
  66. if (__gthread_active_p())
  67. return __exchange_and_add(__mem, __val);
  68. else
  69. return __exchange_and_add_single(__mem, __val);
  70. #else
  71. return __exchange_and_add_single(__mem, __val);
  72. #endif
  73. }
  74. static inline void
  75. __attribute__ ((__unused__))
  76. __atomic_add_dispatch(_Atomic_word* __mem, int __val)
  77. {
  78. #ifdef __GTHREADS
  79. if (__gthread_active_p())
  80. __atomic_add(__mem, __val);
  81. else
  82. __atomic_add_single(__mem, __val);
  83. #else
  84. __atomic_add_single(__mem, __val);
  85. #endif
  86. }
  87. _GLIBCXX_END_NAMESPACE_VERSION
  88. } // namespace
  89. // Even if the CPU doesn't need a memory barrier, we need to ensure
  90. // that the compiler doesn't reorder memory accesses across the
  91. // barriers.
  92. #ifndef _GLIBCXX_READ_MEM_BARRIER
  93. #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
  94. #endif
  95. #ifndef _GLIBCXX_WRITE_MEM_BARRIER
  96. #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")
  97. #endif
  98. #endif