File indexing completed on 2025-01-18 09:51:42
0001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED
0003
0004
0005 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0006 # pragma once
0007 #endif
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
0023 #include <boost/smart_ptr/detail/sp_obsolete.hpp>
0024 #include <boost/config.hpp>
0025 #include <inttypes.h> // uint32_t
0026
0027 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0028
0029 #include <boost/config/pragma_message.hpp>
0030 BOOST_PRAGMA_MESSAGE("Using PS3 sp_counted_base")
0031
0032 #endif
0033
0034 BOOST_SP_OBSOLETE()
0035
0036 namespace boost
0037 {
0038
0039 namespace detail
0040 {
0041
0042 inline uint32_t compare_and_swap( uint32_t * dest_, uint32_t compare_, uint32_t swap_ )
0043 {
0044 return __builtin_cellAtomicCompareAndSwap32(dest_,compare_,swap_);
0045 }
0046
0047 inline uint32_t atomic_fetch_and_add( uint32_t * pw, uint32_t dv )
0048 {
0049
0050
0051
0052
0053 for( ;; )
0054 {
0055 uint32_t r = *pw;
0056
0057 if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
0058 {
0059 return r;
0060 }
0061 }
0062 }
0063
0064 inline void atomic_increment( uint32_t * pw )
0065 {
0066 (void) __builtin_cellAtomicIncr32( pw );
0067 }
0068
0069 inline uint32_t atomic_decrement( uint32_t * pw )
0070 {
0071 return __builtin_cellAtomicDecr32( pw );
0072 }
0073
0074 inline uint32_t atomic_conditional_increment( uint32_t * pw )
0075 {
0076
0077
0078
0079
0080 for( ;; )
0081 {
0082 uint32_t r = *pw;
0083
0084 if( r == 0 )
0085 {
0086 return r;
0087 }
0088
0089 if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
0090 {
0091 return r;
0092 }
0093 }
0094 }
0095
0096 class BOOST_SYMBOL_VISIBLE sp_counted_base
0097 {
0098 private:
0099
0100 sp_counted_base( sp_counted_base const & );
0101 sp_counted_base & operator= ( sp_counted_base const & );
0102
0103 uint32_t use_count_;
0104 uint32_t weak_count_;
0105
0106 public:
0107
0108 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0109 {
0110 }
0111
0112 virtual ~sp_counted_base()
0113 {
0114 }
0115
0116
0117
0118
0119 virtual void dispose() = 0;
0120
0121
0122
0123 virtual void destroy()
0124 {
0125 delete this;
0126 }
0127
0128 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
0129 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
0130 virtual void * get_untyped_deleter() = 0;
0131
0132 void add_ref_copy()
0133 {
0134 atomic_increment( &use_count_ );
0135 }
0136
0137 bool add_ref_lock()
0138 {
0139 return atomic_conditional_increment( &use_count_ ) != 0;
0140 }
0141
0142 void release()
0143 {
0144 if( atomic_decrement( &use_count_ ) == 1 )
0145 {
0146 dispose();
0147 weak_release();
0148 }
0149 }
0150
0151 void weak_add_ref()
0152 {
0153 atomic_increment( &weak_count_ );
0154 }
0155
0156 void weak_release()
0157 {
0158 if( atomic_decrement( &weak_count_ ) == 1 )
0159 {
0160 destroy();
0161 }
0162 }
0163
0164 long use_count() const
0165 {
0166 return const_cast< uint32_t const volatile & >( use_count_ );
0167 }
0168 };
0169
0170 }
0171
0172 }
0173
0174 #endif