Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:30

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 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
0013 //  Copyright 2004-2005 Peter Dimov
0014 //  Copyright 2007-2012 Ion Gaztanaga
0015 //
0016 // Distributed under the Boost Software License, Version 1.0. (See
0017 // accompanying file LICENSE_1_0.txt or copy at
0018 // http://www.boost.org/LICENSE_1_0.txt)
0019 //
0020 //
0021 //  Lock-free algorithm by Alexander Terekhov
0022 //
0023 //  Thanks to Ben Hitchings for the #weak + (#shared != 0)
0024 //  formulation
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_;        // #shared
0047     boost::uint32_t weak_count_;       // #weak + (#shared != 0)
0048 
0049 public:
0050 
0051     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0052     {}
0053 
0054     ~sp_counted_base() // nothrow
0055     {}
0056 
0057     void add_ref_copy()
0058     {
0059         ipcdetail::atomic_inc32( &use_count_ );
0060     }
0061 
0062     bool add_ref_lock() // true on success
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() // nothrow
0074    { return 1 == ipcdetail::atomic_dec32( &use_count_ );  }
0075 
0076    void weak_add_ref() // nothrow
0077    { ipcdetail::atomic_inc32( &weak_count_ ); }
0078 
0079    bool weak_release() // nothrow
0080    { return 1 == ipcdetail::atomic_dec32( &weak_count_ ); }
0081 
0082    long use_count() const // nothrow
0083    { return (long)static_cast<boost::uint32_t const volatile &>( use_count_ ); }
0084 };
0085 
0086 } // namespace ipcdetail
0087 
0088 } // namespace interprocess
0089 
0090 } // namespace boost
0091 
0092 #include <boost/interprocess/detail/config_end.hpp>
0093 
0094 #endif  // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED