atomic_count_std_atomic.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. namespace boost
  17. {
  18. namespace detail
  19. {
  20. class atomic_count
  21. {
  22. public:
  23. explicit atomic_count( long v ): value_( static_cast< std::int_least32_t >( v ) )
  24. {
  25. }
  26. long operator++()
  27. {
  28. return value_.fetch_add( 1, std::memory_order_acq_rel ) + 1;
  29. }
  30. long operator--()
  31. {
  32. return value_.fetch_sub( 1, std::memory_order_acq_rel ) - 1;
  33. }
  34. operator long() const
  35. {
  36. return value_.load( std::memory_order_acquire );
  37. }
  38. private:
  39. atomic_count(atomic_count const &);
  40. atomic_count & operator=(atomic_count const &);
  41. std::atomic_int_least32_t value_;
  42. };
  43. } // namespace detail
  44. } // namespace boost
  45. #endif // #ifndef BOOST_SMART_PTR_DETAIL_ATOMIC_COUNT_STD_ATOMIC_HPP_INCLUDED