sp_counted_base_std_atomic.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. namespace boost
  20. {
  21. namespace detail
  22. {
  23. inline void atomic_increment( std::atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  24. {
  25. pw->fetch_add( 1, std::memory_order_relaxed );
  26. }
  27. inline std::int_least32_t atomic_decrement( std::atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  28. {
  29. return pw->fetch_sub( 1, std::memory_order_acq_rel );
  30. }
  31. inline std::int_least32_t atomic_conditional_increment( std::atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  32. {
  33. // long r = *pw;
  34. // if( r != 0 ) ++*pw;
  35. // return r;
  36. std::int_least32_t r = pw->load( std::memory_order_relaxed );
  37. for( ;; )
  38. {
  39. if( r == 0 )
  40. {
  41. return r;
  42. }
  43. if( pw->compare_exchange_weak( r, r + 1, std::memory_order_relaxed, std::memory_order_relaxed ) )
  44. {
  45. return r;
  46. }
  47. }
  48. }
  49. class BOOST_SYMBOL_VISIBLE sp_counted_base
  50. {
  51. private:
  52. sp_counted_base( sp_counted_base const & );
  53. sp_counted_base & operator= ( sp_counted_base const & );
  54. std::atomic_int_least32_t use_count_; // #shared
  55. std::atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
  56. public:
  57. sp_counted_base() BOOST_SP_NOEXCEPT: use_count_( 1 ), weak_count_( 1 )
  58. {
  59. }
  60. virtual ~sp_counted_base() /*BOOST_SP_NOEXCEPT*/
  61. {
  62. }
  63. // dispose() is called when use_count_ drops to zero, to release
  64. // the resources managed by *this.
  65. virtual void dispose() BOOST_SP_NOEXCEPT = 0;
  66. // destroy() is called when weak_count_ drops to zero.
  67. virtual void destroy() BOOST_SP_NOEXCEPT
  68. {
  69. delete this;
  70. }
  71. virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  72. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  73. virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT = 0;
  74. void add_ref_copy() BOOST_SP_NOEXCEPT
  75. {
  76. atomic_increment( &use_count_ );
  77. }
  78. bool add_ref_lock() BOOST_SP_NOEXCEPT // true on success
  79. {
  80. return atomic_conditional_increment( &use_count_ ) != 0;
  81. }
  82. void release() BOOST_SP_NOEXCEPT
  83. {
  84. if( atomic_decrement( &use_count_ ) == 1 )
  85. {
  86. dispose();
  87. weak_release();
  88. }
  89. }
  90. void weak_add_ref() BOOST_SP_NOEXCEPT
  91. {
  92. atomic_increment( &weak_count_ );
  93. }
  94. void weak_release() BOOST_SP_NOEXCEPT
  95. {
  96. if( atomic_decrement( &weak_count_ ) == 1 )
  97. {
  98. destroy();
  99. }
  100. }
  101. long use_count() const BOOST_SP_NOEXCEPT
  102. {
  103. return use_count_.load( std::memory_order_acquire );
  104. }
  105. };
  106. } // namespace detail
  107. } // namespace boost
  108. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED