sp_counted_base_w32.hpp

Go to the documentation of this file.
00001 #ifndef BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
00002 #define BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  detail/sp_counted_base_w32.hpp
00012 //
00013 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
00014 //  Copyright 2004-2005 Peter Dimov
00015 //
00016 //  Distributed under the Boost Software License, Version 1.0. (See
00017 //  accompanying file LICENSE_1_0.txt or copy at
00018 //  http://www.boost.org/LICENSE_1_0.txt)
00019 //
00020 //
00021 //  Lock-free algorithm by Alexander Terekhov
00022 //
00023 //  Thanks to Ben Hitchings for the #weak + (#shared != 0)
00024 //  formulation
00025 //
00026 
00027 #include <sysc/packages/boost/detail/interlocked.hpp>
00028 #include <typeinfo>
00029 
00030 namespace boost
00031 {
00032 
00033 namespace detail
00034 {
00035 
00036 class sp_counted_base
00037 {
00038 private:
00039 
00040     sp_counted_base( sp_counted_base const & );
00041     sp_counted_base & operator= ( sp_counted_base const & );
00042 
00043     long use_count_;        // #shared
00044     long weak_count_;       // #weak + (#shared != 0)
00045 
00046 public:
00047 
00048     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
00049     {
00050     }
00051 
00052     virtual ~sp_counted_base() // nothrow
00053     {
00054     }
00055 
00056     // dispose() is called when use_count_ drops to zero, to release
00057     // the resources managed by *this.
00058 
00059     virtual void dispose() = 0; // nothrow
00060 
00061     // destroy() is called when weak_count_ drops to zero.
00062 
00063     virtual void destroy() // nothrow
00064     {
00065         delete this;
00066     }
00067 
00068     virtual void * get_deleter( std::type_info const & ti ) = 0;
00069 
00070     void add_ref_copy()
00071     {
00072         BOOST_INTERLOCKED_INCREMENT( &use_count_ );
00073     }
00074 
00075     bool add_ref_lock() // true on success
00076     {
00077         for( ;; )
00078         {
00079             long tmp = static_cast< long const volatile& >( use_count_ );
00080             if( tmp == 0 ) return false;
00081             if( BOOST_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
00082         }
00083     }
00084 
00085     void release() // nothrow
00086     {
00087         if( BOOST_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
00088         {
00089             dispose();
00090             weak_release();
00091         }
00092     }
00093 
00094     void weak_add_ref() // nothrow
00095     {
00096         BOOST_INTERLOCKED_INCREMENT( &weak_count_ );
00097     }
00098 
00099     void weak_release() // nothrow
00100     {
00101         if( BOOST_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
00102         {
00103             destroy();
00104         }
00105     }
00106 
00107     long use_count() const // nothrow
00108     {
00109         return static_cast<long const volatile &>( use_count_ );
00110     }
00111 };
00112 
00113 } // namespace detail
00114 
00115 } // namespace boost
00116 
00117 #endif  // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5