sp_counted_base_spin.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SPIN_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SPIN_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_spin.hpp - spinlock pool atomic emulation
  9. //
  10. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  11. // Copyright 2004-2008 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. #include <boost/detail/sp_typeinfo.hpp>
  18. #include <boost/smart_ptr/detail/spinlock_pool.hpp>
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. inline int atomic_exchange_and_add( int * pw, int dv )
  24. {
  25. spinlock_pool<1>::scoped_lock lock( pw );
  26. int r = *pw;
  27. *pw += dv;
  28. return r;
  29. }
  30. inline void atomic_increment( int * pw )
  31. {
  32. spinlock_pool<1>::scoped_lock lock( pw );
  33. ++*pw;
  34. }
  35. inline int atomic_conditional_increment( int * pw )
  36. {
  37. spinlock_pool<1>::scoped_lock lock( pw );
  38. int rv = *pw;
  39. if( rv != 0 ) ++*pw;
  40. return rv;
  41. }
  42. class sp_counted_base
  43. {
  44. private:
  45. sp_counted_base( sp_counted_base const & );
  46. sp_counted_base & operator= ( sp_counted_base const & );
  47. int use_count_; // #shared
  48. int weak_count_; // #weak + (#shared != 0)
  49. public:
  50. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  51. {
  52. }
  53. virtual ~sp_counted_base() // nothrow
  54. {
  55. }
  56. // dispose() is called when use_count_ drops to zero, to release
  57. // the resources managed by *this.
  58. virtual void dispose() = 0; // nothrow
  59. // destroy() is called when weak_count_ drops to zero.
  60. virtual void destroy() // nothrow
  61. {
  62. delete this;
  63. }
  64. virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
  65. void add_ref_copy()
  66. {
  67. atomic_increment( &use_count_ );
  68. }
  69. bool add_ref_lock() // true on success
  70. {
  71. return atomic_conditional_increment( &use_count_ ) != 0;
  72. }
  73. void release() // nothrow
  74. {
  75. if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
  76. {
  77. dispose();
  78. weak_release();
  79. }
  80. }
  81. void weak_add_ref() // nothrow
  82. {
  83. atomic_increment( &weak_count_ );
  84. }
  85. void weak_release() // nothrow
  86. {
  87. if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
  88. {
  89. destroy();
  90. }
  91. }
  92. long use_count() const // nothrow
  93. {
  94. spinlock_pool<1>::scoped_lock lock( &use_count_ );
  95. return use_count_;
  96. }
  97. };
  98. } // namespace detail
  99. } // namespace boost
  100. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SPIN_HPP_INCLUDED