sp_counted_base_spin.hpp 2.8 KB

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