sp_counted_base_w32.hpp 3.2 KB

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