sp_counted_base_solaris.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SOLARIS_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SOLARIS_HPP_INCLUDED
  3. //
  4. // detail/sp_counted_base_solaris.hpp
  5. // based on: detail/sp_counted_base_w32.hpp
  6. //
  7. // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
  8. // Copyright 2004-2005 Peter Dimov
  9. // Copyright 2006 Michael van der Westhuizen
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See
  12. // accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. //
  15. //
  16. // Lock-free algorithm by Alexander Terekhov
  17. //
  18. // Thanks to Ben Hitchings for the #weak + (#shared != 0)
  19. // formulation
  20. //
  21. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  22. #include <boost/config.hpp>
  23. #include <atomic.h>
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. class BOOST_SYMBOL_VISIBLE sp_counted_base
  29. {
  30. private:
  31. sp_counted_base( sp_counted_base const & );
  32. sp_counted_base & operator= ( sp_counted_base const & );
  33. uint32_t use_count_; // #shared
  34. uint32_t weak_count_; // #weak + (#shared != 0)
  35. public:
  36. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  37. {
  38. }
  39. virtual ~sp_counted_base() // nothrow
  40. {
  41. }
  42. // dispose() is called when use_count_ drops to zero, to release
  43. // the resources managed by *this.
  44. virtual void dispose() = 0; // nothrow
  45. // destroy() is called when weak_count_ drops to zero.
  46. virtual void destroy() // nothrow
  47. {
  48. delete this;
  49. }
  50. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  51. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  52. virtual void * get_untyped_deleter() = 0;
  53. void add_ref_copy()
  54. {
  55. atomic_inc_32( &use_count_ );
  56. }
  57. bool add_ref_lock() // true on success
  58. {
  59. for( ;; )
  60. {
  61. uint32_t tmp = static_cast< uint32_t const volatile& >( use_count_ );
  62. if( tmp == 0 ) return false;
  63. if( atomic_cas_32( &use_count_, tmp, tmp + 1 ) == tmp ) return true;
  64. }
  65. }
  66. void release() // nothrow
  67. {
  68. if( atomic_dec_32_nv( &use_count_ ) == 0 )
  69. {
  70. dispose();
  71. weak_release();
  72. }
  73. }
  74. void weak_add_ref() // nothrow
  75. {
  76. atomic_inc_32( &weak_count_ );
  77. }
  78. void weak_release() // nothrow
  79. {
  80. if( atomic_dec_32_nv( &weak_count_ ) == 0 )
  81. {
  82. destroy();
  83. }
  84. }
  85. long use_count() const // nothrow
  86. {
  87. return static_cast<long const volatile &>( use_count_ );
  88. }
  89. };
  90. } // namespace detail
  91. } // namespace boost
  92. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SOLARIS_HPP_INCLUDED