Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:43

0001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
0003 
0004 // MS compatible compilers support #pragma once
0005 
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009 
0010 //
0011 //  detail/sp_counted_base_w32.hpp
0012 //
0013 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
0014 //  Copyright 2004-2005 Peter Dimov
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/smart_ptr/detail/sp_interlocked.hpp>
0028 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
0029 #include <boost/config/workaround.hpp>
0030 #include <boost/config.hpp>
0031 
0032 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0033 
0034 #include <boost/config/pragma_message.hpp>
0035 BOOST_PRAGMA_MESSAGE("Using Win32 sp_counted_base")
0036 
0037 #endif
0038 
0039 namespace boost
0040 {
0041 
0042 namespace detail
0043 {
0044 
0045 class BOOST_SYMBOL_VISIBLE sp_counted_base
0046 {
0047 private:
0048 
0049     sp_counted_base( sp_counted_base const & );
0050     sp_counted_base & operator= ( sp_counted_base const & );
0051 
0052     long use_count_;        // #shared
0053     long weak_count_;       // #weak + (#shared != 0)
0054 
0055 public:
0056 
0057     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0058     {
0059     }
0060 
0061     virtual ~sp_counted_base() // nothrow
0062     {
0063     }
0064 
0065     // dispose() is called when use_count_ drops to zero, to release
0066     // the resources managed by *this.
0067 
0068     virtual void dispose() = 0; // nothrow
0069 
0070     // destroy() is called when weak_count_ drops to zero.
0071 
0072     virtual void destroy() // nothrow
0073     {
0074         delete this;
0075     }
0076 
0077     virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
0078     virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
0079     virtual void * get_untyped_deleter() = 0;
0080 
0081     void add_ref_copy()
0082     {
0083         BOOST_SP_INTERLOCKED_INCREMENT( &use_count_ );
0084     }
0085 
0086     bool add_ref_lock() // true on success
0087     {
0088         for( ;; )
0089         {
0090             long tmp = static_cast< long const volatile& >( use_count_ );
0091             if( tmp == 0 ) return false;
0092 
0093 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
0094 
0095             // work around a code generation bug
0096 
0097             long tmp2 = tmp + 1;
0098             if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
0099 
0100 #else
0101 
0102             if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
0103 
0104 #endif
0105         }
0106     }
0107 
0108     void release() // nothrow
0109     {
0110         if( BOOST_SP_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
0111         {
0112             dispose();
0113             weak_release();
0114         }
0115     }
0116 
0117     void weak_add_ref() // nothrow
0118     {
0119         BOOST_SP_INTERLOCKED_INCREMENT( &weak_count_ );
0120     }
0121 
0122     void weak_release() // nothrow
0123     {
0124         if( BOOST_SP_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
0125         {
0126             destroy();
0127         }
0128     }
0129 
0130     long use_count() const // nothrow
0131     {
0132         return static_cast<long const volatile &>( use_count_ );
0133     }
0134 };
0135 
0136 } // namespace detail
0137 
0138 } // namespace boost
0139 
0140 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED