sp_counted_base_spin.hpp 3.0 KB

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