sp_counted_base_w32.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // detail/sp_counted_base_w32.hpp
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2005 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. //
  18. // Lock-free algorithm by Alexander Terekhov
  19. //
  20. // Thanks to Ben Hitchings for the #weak + (#shared != 0)
  21. // formulation
  22. //
  23. #include <boost/detail/interlocked.hpp>
  24. #include <boost/detail/workaround.hpp>
  25. #include <boost/detail/sp_typeinfo.hpp>
  26. namespace boost
  27. {
  28. namespace detail
  29. {
  30. class sp_counted_base
  31. {
  32. private:
  33. sp_counted_base( sp_counted_base const & );
  34. sp_counted_base & operator= ( sp_counted_base const & );
  35. long use_count_; // #shared
  36. long weak_count_; // #weak + (#shared != 0)
  37. public:
  38. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  39. {
  40. }
  41. virtual ~sp_counted_base() // nothrow
  42. {
  43. }
  44. // dispose() is called when use_count_ drops to zero, to release
  45. // the resources managed by *this.
  46. virtual void dispose() = 0; // nothrow
  47. // destroy() is called when weak_count_ drops to zero.
  48. virtual void destroy() // nothrow
  49. {
  50. delete this;
  51. }
  52. virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
  53. void add_ref_copy()
  54. {
  55. BOOST_INTERLOCKED_INCREMENT( &use_count_ );
  56. }
  57. bool add_ref_lock() // true on success
  58. {
  59. for( ;; )
  60. {
  61. long tmp = static_cast< long const volatile& >( use_count_ );
  62. if( tmp == 0 ) return false;
  63. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
  64. // work around a code generation bug
  65. long tmp2 = tmp + 1;
  66. if( BOOST_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
  67. #else
  68. if( BOOST_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
  69. #endif
  70. }
  71. }
  72. void release() // nothrow
  73. {
  74. if( BOOST_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
  75. {
  76. dispose();
  77. weak_release();
  78. }
  79. }
  80. void weak_add_ref() // nothrow
  81. {
  82. BOOST_INTERLOCKED_INCREMENT( &weak_count_ );
  83. }
  84. void weak_release() // nothrow
  85. {
  86. if( BOOST_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
  87. {
  88. destroy();
  89. }
  90. }
  91. long use_count() const // nothrow
  92. {
  93. return static_cast<long const volatile &>( use_count_ );
  94. }
  95. };
  96. } // namespace detail
  97. } // namespace boost
  98. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED