Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

117 lines
3.4KB

  1. // Support for atomic operations -*- C++ -*-
  2. // Copyright (C) 2004-2020 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. inline _Atomic_word
  38. __attribute__((__always_inline__))
  39. __exchange_and_add(volatile _Atomic_word* __mem, int __val)
  40. { return __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
  41. inline void
  42. __attribute__((__always_inline__))
  43. __atomic_add(volatile _Atomic_word* __mem, int __val)
  44. { __atomic_fetch_add(__mem, __val, __ATOMIC_ACQ_REL); }
  45. #else
  46. _Atomic_word
  47. __exchange_and_add(volatile _Atomic_word*, int) _GLIBCXX_NOTHROW;
  48. void
  49. __atomic_add(volatile _Atomic_word*, int) _GLIBCXX_NOTHROW;
  50. #endif
  51. inline _Atomic_word
  52. __attribute__((__always_inline__))
  53. __exchange_and_add_single(_Atomic_word* __mem, int __val)
  54. {
  55. _Atomic_word __result = *__mem;
  56. *__mem += __val;
  57. return __result;
  58. }
  59. inline void
  60. __attribute__((__always_inline__))
  61. __atomic_add_single(_Atomic_word* __mem, int __val)
  62. { *__mem += __val; }
  63. inline _Atomic_word
  64. __attribute__ ((__always_inline__))
  65. __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
  66. {
  67. #ifdef __GTHREADS
  68. if (__gthread_active_p())
  69. return __exchange_and_add(__mem, __val);
  70. #endif
  71. return __exchange_and_add_single(__mem, __val);
  72. }
  73. inline void
  74. __attribute__ ((__always_inline__))
  75. __atomic_add_dispatch(_Atomic_word* __mem, int __val)
  76. {
  77. #ifdef __GTHREADS
  78. if (__gthread_active_p())
  79. {
  80. __atomic_add(__mem, __val);
  81. return;
  82. }
  83. #endif
  84. __atomic_add_single(__mem, __val);
  85. }
  86. _GLIBCXX_END_NAMESPACE_VERSION
  87. } // namespace
  88. // Even if the CPU doesn't need a memory barrier, we need to ensure
  89. // that the compiler doesn't reorder memory accesses across the
  90. // barriers.
  91. #ifndef _GLIBCXX_READ_MEM_BARRIER
  92. #define _GLIBCXX_READ_MEM_BARRIER __atomic_thread_fence (__ATOMIC_ACQUIRE)
  93. #endif
  94. #ifndef _GLIBCXX_WRITE_MEM_BARRIER
  95. #define _GLIBCXX_WRITE_MEM_BARRIER __atomic_thread_fence (__ATOMIC_RELEASE)
  96. #endif
  97. #endif