1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/sp_counted_base_gcc_ppc.hpp - g++ on PowerPC
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
21 // Lock-free algorithm by Alexander Terekhov
23 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
27 #include <boost/detail/sp_typeinfo.hpp>
35 inline void atomic_increment( int * pw )
46 "stwcx. %1, 0, %2\n\t"
49 "=m"( *pw ), "=&b"( tmp ):
50 "r"( pw ), "m"( *pw ):
55 inline int atomic_decrement( int * pw )
67 "stwcx. %1, 0, %2\n\t"
71 "=m"( *pw ), "=&b"( rv ):
72 "r"( pw ), "m"( *pw ):
79 inline int atomic_conditional_increment( int * pw )
81 // if( *pw != 0 ) ++*pw;
94 "stwcx. %1, 0, %2\n\t"
97 "=m"( *pw ), "=&b"( rv ):
98 "r"( pw ), "m"( *pw ):
105 class sp_counted_base
109 sp_counted_base( sp_counted_base const & );
110 sp_counted_base & operator= ( sp_counted_base const & );
112 int use_count_; // #shared
113 int weak_count_; // #weak + (#shared != 0)
117 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
121 virtual ~sp_counted_base() // nothrow
125 // dispose() is called when use_count_ drops to zero, to release
126 // the resources managed by *this.
128 virtual void dispose() = 0; // nothrow
130 // destroy() is called when weak_count_ drops to zero.
132 virtual void destroy() // nothrow
137 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
141 atomic_increment( &use_count_ );
144 bool add_ref_lock() // true on success
146 return atomic_conditional_increment( &use_count_ ) != 0;
149 void release() // nothrow
151 if( atomic_decrement( &use_count_ ) == 0 )
158 void weak_add_ref() // nothrow
160 atomic_increment( &weak_count_ );
163 void weak_release() // nothrow
165 if( atomic_decrement( &weak_count_ ) == 0 )
171 long use_count() const // nothrow
173 return static_cast<int const volatile &>( use_count_ );
177 } // namespace detail
181 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED