atomic_count_gcc_atomic.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_ATOMIC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_ATOMIC_HPP_INCLUDED
  3. // boost/detail/atomic_count_gcc_atomic.hpp
  4. //
  5. // atomic_count for g++ 4.7+
  6. //
  7. // Copyright 2007, 2020 Peter Dimov
  8. //
  9. // Distributed under the Boost Software License, Version 1.0.
  10. // https://www.boost.org/LICENSE_1_0.txt
  11. #include <boost/cstdint.hpp>
  12. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  13. #include <boost/config/pragma_message.hpp>
  14. BOOST_PRAGMA_MESSAGE("Using __atomic atomic_count")
  15. #endif
  16. namespace boost
  17. {
  18. namespace detail
  19. {
  20. class atomic_count
  21. {
  22. public:
  23. explicit atomic_count( long v ): value_( static_cast< boost::int_least32_t >( v ) )
  24. {
  25. }
  26. long operator++()
  27. {
  28. return __atomic_add_fetch( &value_, +1, __ATOMIC_ACQ_REL );
  29. }
  30. long operator--()
  31. {
  32. return __atomic_add_fetch( &value_, -1, __ATOMIC_ACQ_REL );
  33. }
  34. operator long() const
  35. {
  36. return __atomic_load_n( &value_, __ATOMIC_ACQUIRE );
  37. }
  38. private:
  39. atomic_count(atomic_count const &);
  40. atomic_count & operator=(atomic_count const &);
  41. boost::int_least32_t value_;
  42. };
  43. } // namespace detail
  44. } // namespace boost
  45. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_GCC_ATOMIC_HPP_INCLUDED