sp_counted_base_acc_ia64.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED
00003 
00004 //
00005 //  detail/sp_counted_base_acc_ia64.hpp - aC++ on HP-UX IA64
00006 //
00007 //  Copyright 2007 Baruch Zilber
00008 //  Copyright 2007 Boris Gubenko
00009 //
00010 //  Distributed under the Boost Software License, Version 1.0. (See
00011 //  accompanying file LICENSE_1_0.txt or copy at
00012 //  http://www.boost.org/LICENSE_1_0.txt)
00013 //
00014 //
00015 //  Lock-free algorithm by Alexander Terekhov
00016 //
00017 
00018 #include <boost/detail/sp_typeinfo.hpp>
00019 #include <machine/sys/inline.h>
00020 
00021 namespace boost
00022 {
00023 
00024 namespace detail
00025 {
00026 
00027 inline void atomic_increment( int * pw )
00028 {
00029     // ++*pw;
00030 
00031     _Asm_fetchadd(_FASZ_W, _SEM_REL, pw, +1, _LDHINT_NONE);
00032 } 
00033 
00034 inline int atomic_decrement( int * pw )
00035 {
00036     // return --*pw;
00037 
00038     int r = static_cast<int>(_Asm_fetchadd(_FASZ_W, _SEM_REL, pw, -1, _LDHINT_NONE));
00039     if (1 == r)
00040     {
00041         _Asm_mf();
00042     }
00043     
00044     return r - 1;
00045 }
00046 
00047 inline int atomic_conditional_increment( int * pw )
00048 {
00049     // if( *pw != 0 ) ++*pw;
00050     // return *pw;
00051 
00052     int v = *pw;
00053     
00054     for (;;)
00055     {
00056         if (0 == v)
00057         {
00058             return 0;
00059         }
00060         
00061         _Asm_mov_to_ar(_AREG_CCV,
00062                        v,
00063                        (_UP_CALL_FENCE | _UP_SYS_FENCE | _DOWN_CALL_FENCE | _DOWN_SYS_FENCE));
00064         int r = static_cast<int>(_Asm_cmpxchg(_SZ_W, _SEM_ACQ, pw, v + 1, _LDHINT_NONE));
00065         if (r == v)
00066         {
00067             return r + 1;
00068         }
00069         
00070         v = r;
00071     }
00072 }
00073 
00074 class sp_counted_base
00075 {
00076 private:
00077 
00078     sp_counted_base( sp_counted_base const & );
00079     sp_counted_base & operator= ( sp_counted_base const & );
00080 
00081     int use_count_;        // #shared
00082     int weak_count_;       // #weak + (#shared != 0)
00083 
00084 public:
00085 
00086     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
00087     {
00088     }
00089 
00090     virtual ~sp_counted_base() // nothrow
00091     {
00092     }
00093 
00094     // dispose() is called when use_count_ drops to zero, to release
00095     // the resources managed by *this.
00096 
00097     virtual void dispose() = 0; // nothrow
00098 
00099     // destroy() is called when weak_count_ drops to zero.
00100 
00101     virtual void destroy() // nothrow
00102     {
00103         delete this;
00104     }
00105 
00106     virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
00107 
00108     void add_ref_copy()
00109     {
00110         atomic_increment( &use_count_ );
00111     }
00112 
00113     bool add_ref_lock() // true on success
00114     {
00115         return atomic_conditional_increment( &use_count_ ) != 0;
00116     }
00117 
00118     void release() // nothrow
00119     {
00120         if( atomic_decrement( &use_count_ ) == 0 )
00121         {
00122             dispose();
00123             weak_release();
00124         }
00125     }
00126 
00127     void weak_add_ref() // nothrow
00128     {
00129         atomic_increment( &weak_count_ );
00130     }
00131 
00132     void weak_release() // nothrow
00133     {
00134         if( atomic_decrement( &weak_count_ ) == 0 )
00135         {
00136             destroy();
00137         }
00138     }
00139 
00140     long use_count() const // nothrow
00141     {
00142         return static_cast<int const volatile &>( use_count_ ); // TODO use ld.acq here
00143     }
00144 };
00145 
00146 } // namespace detail
00147 
00148 } // namespace boost
00149 
00150 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_ACC_IA64_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29