sp_counted_base_std_atomic.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // detail/sp_counted_base_std_atomic.hpp - C++11 std::atomic
  8. //
  9. // Copyright (c) 2007, 2013 Peter Dimov
  10. //
  11. // Distributed under the Boost Software License, Version 1.0.
  12. // See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt
  14. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  15. #include <boost/smart_ptr/detail/sp_noexcept.hpp>
  16. #include <boost/config.hpp>
  17. #include <atomic>
  18. #include <cstdint>
  19. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  20. #include <boost/config/pragma_message.hpp>
  21. BOOST_PRAGMA_MESSAGE("Using std::atomic sp_counted_base")
  22. #endif
  23. namespace boost
  24. {
  25. namespace detail
  26. {
  27. inline void atomic_increment( std::atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  28. {
  29. pw->fetch_add( 1, std::memory_order_relaxed );
  30. }
  31. inline std::int_least32_t atomic_decrement( std::atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  32. {
  33. return pw->fetch_sub( 1, std::memory_order_acq_rel );
  34. }
  35. inline std::int_least32_t atomic_conditional_increment( std::atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  36. {
  37. // long r = *pw;
  38. // if( r != 0 ) ++*pw;
  39. // return r;
  40. std::int_least32_t r = pw->load( std::memory_order_relaxed );
  41. for( ;; )
  42. {
  43. if( r == 0 )
  44. {
  45. return r;
  46. }
  47. if( pw->compare_exchange_weak( r, r + 1, std::memory_order_relaxed, std::memory_order_relaxed ) )
  48. {
  49. return r;
  50. }
  51. }
  52. }
  53. class BOOST_SYMBOL_VISIBLE sp_counted_base
  54. {
  55. private:
  56. sp_counted_base( sp_counted_base const & );
  57. sp_counted_base & operator= ( sp_counted_base const & );
  58. std::atomic_int_least32_t use_count_; // #shared
  59. std::atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
  60. public:
  61. sp_counted_base() BOOST_SP_NOEXCEPT: use_count_( 1 ), weak_count_( 1 )
  62. {
  63. }
  64. virtual ~sp_counted_base() /*BOOST_SP_NOEXCEPT*/
  65. {
  66. }
  67. // dispose() is called when use_count_ drops to zero, to release
  68. // the resources managed by *this.
  69. virtual void dispose() BOOST_SP_NOEXCEPT = 0;
  70. // destroy() is called when weak_count_ drops to zero.
  71. virtual void destroy() BOOST_SP_NOEXCEPT
  72. {
  73. delete this;
  74. }
  75. virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  76. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  77. virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT = 0;
  78. void add_ref_copy() BOOST_SP_NOEXCEPT
  79. {
  80. atomic_increment( &use_count_ );
  81. }
  82. bool add_ref_lock() BOOST_SP_NOEXCEPT // true on success
  83. {
  84. return atomic_conditional_increment( &use_count_ ) != 0;
  85. }
  86. void release() BOOST_SP_NOEXCEPT
  87. {
  88. if( atomic_decrement( &use_count_ ) == 1 )
  89. {
  90. dispose();
  91. weak_release();
  92. }
  93. }
  94. void weak_add_ref() BOOST_SP_NOEXCEPT
  95. {
  96. atomic_increment( &weak_count_ );
  97. }
  98. void weak_release() BOOST_SP_NOEXCEPT
  99. {
  100. if( atomic_decrement( &weak_count_ ) == 1 )
  101. {
  102. destroy();
  103. }
  104. }
  105. long use_count() const BOOST_SP_NOEXCEPT
  106. {
  107. return use_count_.load( std::memory_order_acquire );
  108. }
  109. };
  110. } // namespace detail
  111. } // namespace boost
  112. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED