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