atomicity.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Support for atomic operations -*- C++ -*-
  2. // Copyright (C) 2004, 2005, 2006, 2008, 2009 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 atomicity.h
  21. * This is an internal header file, included by other library headers.
  22. * You should not attempt to use it directly.
  23. */
  24. #ifndef _GLIBCXX_ATOMICITY_H
  25. #define _GLIBCXX_ATOMICITY_H 1
  26. #include <bits/c++config.h>
  27. #include <bits/gthr.h>
  28. #include <bits/atomic_word.h>
  29. _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
  30. // Functions for portable atomic access.
  31. // To abstract locking primitives across all thread policies, use:
  32. // __exchange_and_add_dispatch
  33. // __atomic_add_dispatch
  34. #ifdef _GLIBCXX_ATOMIC_BUILTINS_4
  35. static inline _Atomic_word
  36. __exchange_and_add(volatile _Atomic_word* __mem, int __val)
  37. { return __sync_fetch_and_add(__mem, __val); }
  38. static inline void
  39. __atomic_add(volatile _Atomic_word* __mem, int __val)
  40. { __sync_fetch_and_add(__mem, __val); }
  41. #else
  42. _Atomic_word
  43. __attribute__ ((__unused__))
  44. __exchange_and_add(volatile _Atomic_word*, int);
  45. void
  46. __attribute__ ((__unused__))
  47. __atomic_add(volatile _Atomic_word*, int);
  48. #endif
  49. static inline _Atomic_word
  50. __exchange_and_add_single(_Atomic_word* __mem, int __val)
  51. {
  52. _Atomic_word __result = *__mem;
  53. *__mem += __val;
  54. return __result;
  55. }
  56. static inline void
  57. __atomic_add_single(_Atomic_word* __mem, int __val)
  58. { *__mem += __val; }
  59. static inline _Atomic_word
  60. __attribute__ ((__unused__))
  61. __exchange_and_add_dispatch(_Atomic_word* __mem, int __val)
  62. {
  63. #ifdef __GTHREADS
  64. if (__gthread_active_p())
  65. return __exchange_and_add(__mem, __val);
  66. else
  67. return __exchange_and_add_single(__mem, __val);
  68. #else
  69. return __exchange_and_add_single(__mem, __val);
  70. #endif
  71. }
  72. static inline void
  73. __attribute__ ((__unused__))
  74. __atomic_add_dispatch(_Atomic_word* __mem, int __val)
  75. {
  76. #ifdef __GTHREADS
  77. if (__gthread_active_p())
  78. __atomic_add(__mem, __val);
  79. else
  80. __atomic_add_single(__mem, __val);
  81. #else
  82. __atomic_add_single(__mem, __val);
  83. #endif
  84. }
  85. _GLIBCXX_END_NAMESPACE
  86. // Even if the CPU doesn't need a memory barrier, we need to ensure
  87. // that the compiler doesn't reorder memory accesses across the
  88. // barriers.
  89. #ifndef _GLIBCXX_READ_MEM_BARRIER
  90. #define _GLIBCXX_READ_MEM_BARRIER __asm __volatile ("":::"memory")
  91. #endif
  92. #ifndef _GLIBCXX_WRITE_MEM_BARRIER
  93. #define _GLIBCXX_WRITE_MEM_BARRIER __asm __volatile ("":::"memory")
  94. #endif
  95. #endif