atomic_count_spin.hpp 1.3 KB

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