sp_counted_base_w32.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/smart_ptr/detail/sp_interlocked.hpp>
  24. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  25. #include <boost/detail/workaround.hpp>
  26. #include <boost/config.hpp>
  27. namespace boost
  28. {
  29. namespace detail
  30. {
  31. class BOOST_SYMBOL_VISIBLE sp_counted_base
  32. {
  33. private:
  34. sp_counted_base( sp_counted_base const & );
  35. sp_counted_base & operator= ( sp_counted_base const & );
  36. long use_count_; // #shared
  37. long weak_count_; // #weak + (#shared != 0)
  38. public:
  39. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  40. {
  41. }
  42. virtual ~sp_counted_base() // nothrow
  43. {
  44. }
  45. // dispose() is called when use_count_ drops to zero, to release
  46. // the resources managed by *this.
  47. virtual void dispose() = 0; // nothrow
  48. // destroy() is called when weak_count_ drops to zero.
  49. virtual void destroy() // nothrow
  50. {
  51. delete this;
  52. }
  53. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  54. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  55. virtual void * get_untyped_deleter() = 0;
  56. void add_ref_copy()
  57. {
  58. BOOST_SP_INTERLOCKED_INCREMENT( &use_count_ );
  59. }
  60. bool add_ref_lock() // true on success
  61. {
  62. for( ;; )
  63. {
  64. long tmp = static_cast< long const volatile& >( use_count_ );
  65. if( tmp == 0 ) return false;
  66. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
  67. // work around a code generation bug
  68. long tmp2 = tmp + 1;
  69. if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
  70. #else
  71. if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
  72. #endif
  73. }
  74. }
  75. void release() // nothrow
  76. {
  77. if( BOOST_SP_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
  78. {
  79. dispose();
  80. weak_release();
  81. }
  82. }
  83. void weak_add_ref() // nothrow
  84. {
  85. BOOST_SP_INTERLOCKED_INCREMENT( &weak_count_ );
  86. }
  87. void weak_release() // nothrow
  88. {
  89. if( BOOST_SP_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
  90. {
  91. destroy();
  92. }
  93. }
  94. long use_count() const // nothrow
  95. {
  96. return static_cast<long const volatile &>( use_count_ );
  97. }
  98. };
  99. } // namespace detail
  100. } // namespace boost
  101. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED