sp_counted_base_clang.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_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_clang.hpp - __c11 clang intrinsics
  8. //
  9. // Copyright (c) 2007, 2013, 2015 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 <boost/cstdint.hpp>
  18. namespace boost
  19. {
  20. namespace detail
  21. {
  22. typedef _Atomic( boost::int_least32_t ) atomic_int_least32_t;
  23. inline void atomic_increment( atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  24. {
  25. __c11_atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
  26. }
  27. inline boost::int_least32_t atomic_decrement( atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  28. {
  29. return __c11_atomic_fetch_sub( pw, 1, __ATOMIC_ACQ_REL );
  30. }
  31. inline boost::int_least32_t atomic_conditional_increment( atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
  32. {
  33. // long r = *pw;
  34. // if( r != 0 ) ++*pw;
  35. // return r;
  36. boost::int_least32_t r = __c11_atomic_load( pw, __ATOMIC_RELAXED );
  37. for( ;; )
  38. {
  39. if( r == 0 )
  40. {
  41. return r;
  42. }
  43. if( __c11_atomic_compare_exchange_weak( pw, &r, r + 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED ) )
  44. {
  45. return r;
  46. }
  47. }
  48. }
  49. #if defined(__clang__)
  50. # pragma clang diagnostic push
  51. # pragma clang diagnostic ignored "-Wweak-vtables"
  52. #endif
  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. atomic_int_least32_t use_count_; // #shared
  59. atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
  60. public:
  61. sp_counted_base() BOOST_SP_NOEXCEPT
  62. {
  63. __c11_atomic_init( &use_count_, 1 );
  64. __c11_atomic_init( &weak_count_, 1 );
  65. }
  66. virtual ~sp_counted_base() /*BOOST_SP_NOEXCEPT*/
  67. {
  68. }
  69. // dispose() is called when use_count_ drops to zero, to release
  70. // the resources managed by *this.
  71. virtual void dispose() BOOST_SP_NOEXCEPT = 0; // nothrow
  72. // destroy() is called when weak_count_ drops to zero.
  73. virtual void destroy() BOOST_SP_NOEXCEPT // nothrow
  74. {
  75. delete this;
  76. }
  77. virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  78. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
  79. virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT = 0;
  80. void add_ref_copy() BOOST_SP_NOEXCEPT
  81. {
  82. atomic_increment( &use_count_ );
  83. }
  84. bool add_ref_lock() BOOST_SP_NOEXCEPT // true on success
  85. {
  86. return atomic_conditional_increment( &use_count_ ) != 0;
  87. }
  88. void release() BOOST_SP_NOEXCEPT
  89. {
  90. if( atomic_decrement( &use_count_ ) == 1 )
  91. {
  92. dispose();
  93. weak_release();
  94. }
  95. }
  96. void weak_add_ref() BOOST_SP_NOEXCEPT
  97. {
  98. atomic_increment( &weak_count_ );
  99. }
  100. void weak_release() BOOST_SP_NOEXCEPT
  101. {
  102. if( atomic_decrement( &weak_count_ ) == 1 )
  103. {
  104. destroy();
  105. }
  106. }
  107. long use_count() const BOOST_SP_NOEXCEPT
  108. {
  109. return __c11_atomic_load( const_cast< atomic_int_least32_t* >( &use_count_ ), __ATOMIC_ACQUIRE );
  110. }
  111. };
  112. #if defined(__clang__)
  113. # pragma clang diagnostic pop
  114. #endif
  115. } // namespace detail
  116. } // namespace boost
  117. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED