Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-29 07:53:55

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 ">#
0009 #if defined(BOOST_HAS_PRAGMA_ONCE)
0010 # pragma once
0011 #endif
0012 
0013 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
0014 //  Copyright 2004-2005 Peter Dimov
0015 //  Copyright 2007-2012 Ion Gaztanaga
0016 //
0017 // Distributed under the Boost Software License, Version 1.0. (See
0018 // accompanying file LICENSE_1_0.txt or copy at
0019 // http://www.boost.org/LICENSE_1_0.txt)
0020 //
0021 //
0022 //  Lock-free algorithm by Alexander Terekhov
0023 //
0024 //  Thanks to Ben Hitchings for the #weak + (#shared != 0)
0025 //  formulation
0026 //
0027 
0028 #include <boost/interprocess/detail/config_begin.hpp>
0029 #include <boost/interprocess/detail/workaround.hpp>
0030 
0031 #include <boost/interprocess/detail/atomic.hpp>
0032 #include <typeinfo>
0033 
0034 namespace boost {
0035 
0036 namespace interprocess {
0037 
0038 namespace ipcdetail {
0039 
0040 class sp_counted_base
0041 {
0042 private:
0043 
0044     sp_counted_base( sp_counted_base const & );
0045     sp_counted_base & operator= ( sp_counted_base const & );
0046 
0047     boost::uint32_t use_count_;        // #shared
0048     boost::uint32_t weak_count_;       // #weak + (#shared != 0)
0049 
0050 public:
0051 
0052     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0053     {}
0054 
0055     ~sp_counted_base() // nothrow
0056     {}
0057 
0058     void add_ref_copy()
0059     {
0060         ipcdetail::atomic_inc32( &use_count_ );
0061     }
0062 
0063     bool add_ref_lock() // true on success
0064     {
0065         for( ;; )
0066         {
0067             boost::uint32_t tmp = static_cast< boost::uint32_t const volatile& >( use_count_ );
0068             if( tmp == 0 ) return false;
0069             if( ipcdetail::atomic_cas32( &use_count_, tmp + 1, tmp ) == tmp )
0070                return true;
0071         }
0072     }
0073 
0074    bool ref_release() // nothrow
0075    { return 1 == ipcdetail::atomic_dec32( &use_count_ );  }
0076 
0077    void weak_add_ref() // nothrow
0078    { ipcdetail::atomic_inc32( &weak_count_ ); }
0079 
0080    bool weak_release() // nothrow
0081    { return 1 == ipcdetail::atomic_dec32( &weak_count_ ); }
0082 
0083    long use_count() const // nothrow
0084    { return (long)static_cast<boost::uint32_t const volatile &>( use_count_ ); }
0085 };
0086 
0087 } // namespace ipcdetail
0088 
0089 } // namespace interprocess
0090 
0091 } // namespace boost
0092 
0093 #include <boost/interprocess/detail/config_end.hpp>
0094 
0095 #endif  // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED