sp_counted_base_snc_ps3.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // detail/sp_counted_base_gcc_sparc.hpp - g++ on Sparc V8+
  8. //
  9. // Copyright (c) 2006 Piotr Wyderski
  10. // Copyright (c) 2006 Tomas Puverle
  11. // Copyright (c) 2006 Peter Dimov
  12. // Copyright (c) 2011 Emil Dotchevski
  13. //
  14. // Distributed under the Boost Software License, Version 1.0.
  15. // See accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt
  17. //
  18. // Thanks to Michael van der Westhuizen
  19. #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
  20. #include <boost/config.hpp>
  21. #include <inttypes.h> // uint32_t
  22. namespace boost
  23. {
  24. namespace detail
  25. {
  26. inline uint32_t compare_and_swap( uint32_t * dest_, uint32_t compare_, uint32_t swap_ )
  27. {
  28. return __builtin_cellAtomicCompareAndSwap32(dest_,compare_,swap_);
  29. }
  30. inline uint32_t atomic_fetch_and_add( uint32_t * pw, uint32_t dv )
  31. {
  32. // long r = *pw;
  33. // *pw += dv;
  34. // return r;
  35. for( ;; )
  36. {
  37. uint32_t r = *pw;
  38. if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
  39. {
  40. return r;
  41. }
  42. }
  43. }
  44. inline void atomic_increment( uint32_t * pw )
  45. {
  46. (void) __builtin_cellAtomicIncr32( pw );
  47. }
  48. inline uint32_t atomic_decrement( uint32_t * pw )
  49. {
  50. return __builtin_cellAtomicDecr32( pw );
  51. }
  52. inline uint32_t atomic_conditional_increment( uint32_t * pw )
  53. {
  54. // long r = *pw;
  55. // if( r != 0 ) ++*pw;
  56. // return r;
  57. for( ;; )
  58. {
  59. uint32_t r = *pw;
  60. if( r == 0 )
  61. {
  62. return r;
  63. }
  64. if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
  65. {
  66. return r;
  67. }
  68. }
  69. }
  70. class BOOST_SYMBOL_VISIBLE sp_counted_base
  71. {
  72. private:
  73. sp_counted_base( sp_counted_base const & );
  74. sp_counted_base & operator= ( sp_counted_base const & );
  75. uint32_t use_count_; // #shared
  76. uint32_t weak_count_; // #weak + (#shared != 0)
  77. public:
  78. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  79. {
  80. }
  81. virtual ~sp_counted_base() // nothrow
  82. {
  83. }
  84. // dispose() is called when use_count_ drops to zero, to release
  85. // the resources managed by *this.
  86. virtual void dispose() = 0; // nothrow
  87. // destroy() is called when weak_count_ drops to zero.
  88. virtual void destroy() // nothrow
  89. {
  90. delete this;
  91. }
  92. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  93. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  94. virtual void * get_untyped_deleter() = 0;
  95. void add_ref_copy()
  96. {
  97. atomic_increment( &use_count_ );
  98. }
  99. bool add_ref_lock() // true on success
  100. {
  101. return atomic_conditional_increment( &use_count_ ) != 0;
  102. }
  103. void release() // nothrow
  104. {
  105. if( atomic_decrement( &use_count_ ) == 1 )
  106. {
  107. dispose();
  108. weak_release();
  109. }
  110. }
  111. void weak_add_ref() // nothrow
  112. {
  113. atomic_increment( &weak_count_ );
  114. }
  115. void weak_release() // nothrow
  116. {
  117. if( atomic_decrement( &weak_count_ ) == 1 )
  118. {
  119. destroy();
  120. }
  121. }
  122. long use_count() const // nothrow
  123. {
  124. return const_cast< uint32_t const volatile & >( use_count_ );
  125. }
  126. };
  127. } // namespace detail
  128. } // namespace boost
  129. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED