sp_counted_base_sync.hpp 3.2 KB

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