sp_counted_base_sync.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_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_sync.hpp - g++ 4.1+ __sync intrinsics
  8. //
  9. // Copyright (c) 2007 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/config.hpp>
  16. #include <limits.h>
  17. #if defined( __ia64__ ) && defined( __INTEL_COMPILER )
  18. # include <ia64intrin.h>
  19. #endif
  20. namespace boost
  21. {
  22. namespace detail
  23. {
  24. #if INT_MAX >= 2147483647
  25. typedef int sp_int32_t;
  26. #else
  27. typedef long sp_int32_t;
  28. #endif
  29. inline void atomic_increment( sp_int32_t * pw )
  30. {
  31. __sync_fetch_and_add( pw, 1 );
  32. }
  33. inline sp_int32_t atomic_decrement( sp_int32_t * pw )
  34. {
  35. return __sync_fetch_and_add( pw, -1 );
  36. }
  37. inline sp_int32_t atomic_conditional_increment( sp_int32_t * pw )
  38. {
  39. // long r = *pw;
  40. // if( r != 0 ) ++*pw;
  41. // return r;
  42. sp_int32_t r = *pw;
  43. for( ;; )
  44. {
  45. if( r == 0 )
  46. {
  47. return r;
  48. }
  49. sp_int32_t r2 = __sync_val_compare_and_swap( pw, r, r + 1 );
  50. if( r2 == r )
  51. {
  52. return r;
  53. }
  54. else
  55. {
  56. r = r2;
  57. }
  58. }
  59. }
  60. class BOOST_SYMBOL_VISIBLE sp_counted_base
  61. {
  62. private:
  63. sp_counted_base( sp_counted_base const & );
  64. sp_counted_base & operator= ( sp_counted_base const & );
  65. sp_int32_t use_count_; // #shared
  66. sp_int32_t weak_count_; // #weak + (#shared != 0)
  67. public:
  68. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  69. {
  70. }
  71. virtual ~sp_counted_base() // nothrow
  72. {
  73. }
  74. // dispose() is called when use_count_ drops to zero, to release
  75. // the resources managed by *this.
  76. virtual void dispose() = 0; // nothrow
  77. // destroy() is called when weak_count_ drops to zero.
  78. virtual void destroy() // nothrow
  79. {
  80. delete this;
  81. }
  82. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  83. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  84. virtual void * get_untyped_deleter() = 0;
  85. void add_ref_copy()
  86. {
  87. atomic_increment( &use_count_ );
  88. }
  89. bool add_ref_lock() // true on success
  90. {
  91. return atomic_conditional_increment( &use_count_ ) != 0;
  92. }
  93. void release() // nothrow
  94. {
  95. if( atomic_decrement( &use_count_ ) == 1 )
  96. {
  97. dispose();
  98. weak_release();
  99. }
  100. }
  101. void weak_add_ref() // nothrow
  102. {
  103. atomic_increment( &weak_count_ );
  104. }
  105. void weak_release() // nothrow
  106. {
  107. if( atomic_decrement( &weak_count_ ) == 1 )
  108. {
  109. destroy();
  110. }
  111. }
  112. long use_count() const // nothrow
  113. {
  114. return const_cast< sp_int32_t const volatile & >( use_count_ );
  115. }
  116. };
  117. } // namespace detail
  118. } // namespace boost
  119. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_HPP_INCLUDED