atomic_count_std_atomic.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED
  3. //
  4. // boost/detail/atomic_count_std_atomic.hpp
  5. //
  6. // atomic_count for std::atomic
  7. //
  8. // Copyright 2013 Peter Dimov
  9. //
  10. // Distributed under the Boost Software License, Version 1.0.
  11. // See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt
  13. //
  14. #include <atomic>
  15. #include <cstdint>
  16. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  17. #include <boost/config/pragma_message.hpp>
  18. BOOST_PRAGMA_MESSAGE("Using std::atomic atomic_count")
  19. #endif
  20. namespace boost
  21. {
  22. namespace detail
  23. {
  24. class atomic_count
  25. {
  26. public:
  27. explicit atomic_count( long v ): value_( static_cast< std::int_least32_t >( v ) )
  28. {
  29. }
  30. long operator++()
  31. {
  32. return value_.fetch_add( 1, std::memory_order_acq_rel ) + 1;
  33. }
  34. long operator--()
  35. {
  36. return value_.fetch_sub( 1, std::memory_order_acq_rel ) - 1;
  37. }
  38. operator long() const
  39. {
  40. return value_.load( std::memory_order_acquire );
  41. }
  42. private:
  43. atomic_count(atomic_count const &);
  44. atomic_count & operator=(atomic_count const &);
  45. std::atomic_int_least32_t value_;
  46. };
  47. } // namespace detail
  48. } // namespace boost
  49. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED