sp_counted_base_sync.hpp 2.9 KB

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