File indexing completed on 2025-10-31 08:42:37
0001 #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
0002 #define BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
0003 
0004 #ifndef BOOST_CONFIG_HPP
0005 #  include <boost/config.hpp>
0006 #endif
0007 #
0008 #if defined(BOOST_HAS_PRAGMA_ONCE)
0009 # pragma once
0010 #endif
0011 
0012 
0013 
0014 
0015 
0016 
0017 
0018 
0019 
0020 
0021 
0022 
0023 
0024 
0025 
0026 
0027 #include <boost/interprocess/detail/config_begin.hpp>
0028 #include <boost/interprocess/detail/workaround.hpp>
0029 
0030 #include <boost/interprocess/detail/atomic.hpp>
0031 #include <typeinfo>
0032 
0033 namespace boost {
0034 
0035 namespace interprocess {
0036 
0037 namespace ipcdetail {
0038 
0039 class sp_counted_base
0040 {
0041 private:
0042 
0043     sp_counted_base( sp_counted_base const & );
0044     sp_counted_base & operator= ( sp_counted_base const & );
0045 
0046     boost::uint32_t use_count_;        
0047     boost::uint32_t weak_count_;       
0048 
0049 public:
0050 
0051     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0052     {}
0053 
0054     ~sp_counted_base() 
0055     {}
0056 
0057     void add_ref_copy()
0058     {
0059         ipcdetail::atomic_inc32( &use_count_ );
0060     }
0061 
0062     bool add_ref_lock() 
0063     {
0064         for( ;; )
0065         {
0066             boost::uint32_t tmp = static_cast< boost::uint32_t const volatile& >( use_count_ );
0067             if( tmp == 0 ) return false;
0068             if( ipcdetail::atomic_cas32( &use_count_, tmp + 1, tmp ) == tmp )
0069                return true;
0070         }
0071     }
0072 
0073    bool ref_release() 
0074    { return 1 == ipcdetail::atomic_dec32( &use_count_ );  }
0075 
0076    void weak_add_ref() 
0077    { ipcdetail::atomic_inc32( &weak_count_ ); }
0078 
0079    bool weak_release() 
0080    { return 1 == ipcdetail::atomic_dec32( &weak_count_ ); }
0081 
0082    long use_count() const 
0083    { return (long)static_cast<boost::uint32_t const volatile &>( use_count_ ); }
0084 };
0085 
0086 } 
0087 
0088 } 
0089 
0090 } 
0091 
0092 #include <boost/interprocess/detail/config_end.hpp>
0093 
0094 #endif