sp_counted_base_snc_ps3.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_snc_ps3.hpp - PS3 Cell
  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/smart_ptr/detail/sp_obsolete.hpp>
  21. #include <boost/config.hpp>
  22. #include <inttypes.h> // uint32_t
  23. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  24. #include <boost/config/pragma_message.hpp>
  25. BOOST_PRAGMA_MESSAGE("Using PS3 sp_counted_base")
  26. #endif
  27. BOOST_SP_OBSOLETE()
  28. namespace boost
  29. {
  30. namespace detail
  31. {
  32. inline uint32_t compare_and_swap( uint32_t * dest_, uint32_t compare_, uint32_t swap_ )
  33. {
  34. return __builtin_cellAtomicCompareAndSwap32(dest_,compare_,swap_);
  35. }
  36. inline uint32_t atomic_fetch_and_add( uint32_t * pw, uint32_t dv )
  37. {
  38. // long r = *pw;
  39. // *pw += dv;
  40. // return r;
  41. for( ;; )
  42. {
  43. uint32_t r = *pw;
  44. if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
  45. {
  46. return r;
  47. }
  48. }
  49. }
  50. inline void atomic_increment( uint32_t * pw )
  51. {
  52. (void) __builtin_cellAtomicIncr32( pw );
  53. }
  54. inline uint32_t atomic_decrement( uint32_t * pw )
  55. {
  56. return __builtin_cellAtomicDecr32( pw );
  57. }
  58. inline uint32_t atomic_conditional_increment( uint32_t * pw )
  59. {
  60. // long r = *pw;
  61. // if( r != 0 ) ++*pw;
  62. // return r;
  63. for( ;; )
  64. {
  65. uint32_t r = *pw;
  66. if( r == 0 )
  67. {
  68. return r;
  69. }
  70. if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
  71. {
  72. return r;
  73. }
  74. }
  75. }
  76. class BOOST_SYMBOL_VISIBLE sp_counted_base
  77. {
  78. private:
  79. sp_counted_base( sp_counted_base const & );
  80. sp_counted_base & operator= ( sp_counted_base const & );
  81. uint32_t use_count_; // #shared
  82. uint32_t weak_count_; // #weak + (#shared != 0)
  83. public:
  84. sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
  85. {
  86. }
  87. virtual ~sp_counted_base() // nothrow
  88. {
  89. }
  90. // dispose() is called when use_count_ drops to zero, to release
  91. // the resources managed by *this.
  92. virtual void dispose() = 0; // nothrow
  93. // destroy() is called when weak_count_ drops to zero.
  94. virtual void destroy() // nothrow
  95. {
  96. delete this;
  97. }
  98. virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
  99. virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
  100. virtual void * get_untyped_deleter() = 0;
  101. void add_ref_copy()
  102. {
  103. atomic_increment( &use_count_ );
  104. }
  105. bool add_ref_lock() // true on success
  106. {
  107. return atomic_conditional_increment( &use_count_ ) != 0;
  108. }
  109. void release() // nothrow
  110. {
  111. if( atomic_decrement( &use_count_ ) == 1 )
  112. {
  113. dispose();
  114. weak_release();
  115. }
  116. }
  117. void weak_add_ref() // nothrow
  118. {
  119. atomic_increment( &weak_count_ );
  120. }
  121. void weak_release() // nothrow
  122. {
  123. if( atomic_decrement( &weak_count_ ) == 1 )
  124. {
  125. destroy();
  126. }
  127. }
  128. long use_count() const // nothrow
  129. {
  130. return const_cast< uint32_t const volatile & >( use_count_ );
  131. }
  132. };
  133. } // namespace detail
  134. } // namespace boost
  135. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED