1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/sp_counted_base_gcc_x86.hpp - g++ on 486+ or AMD64
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/smart_ptr/detail/sp_typeinfo_.hpp>
28 #include <boost/config.hpp>
36 inline int atomic_exchange_and_add( int * pw, int dv )
48 "=m"( *pw ), "=r"( r ): // outputs (%0, %1)
49 "m"( *pw ), "1"( dv ): // inputs (%2, %3 == %1)
50 "memory", "cc" // clobbers
56 inline void atomic_increment( int * pw )
58 //atomic_exchange_and_add( pw, 1 );
64 "=m"( *pw ): // output (%0)
65 "m"( *pw ): // input (%1)
70 inline int atomic_conditional_increment( int * pw )
73 // if( rv != 0 ) ++*pw;
82 "test %%eax, %%eax\n\t"
90 "=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2)
91 "m"( *pw ): // input (%3)
98 class BOOST_SYMBOL_VISIBLE sp_counted_base
102 sp_counted_base( sp_counted_base const & );
103 sp_counted_base & operator= ( sp_counted_base const & );
105 int use_count_; // #shared
106 int weak_count_; // #weak + (#shared != 0)
110 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
114 virtual ~sp_counted_base() // nothrow
118 // dispose() is called when use_count_ drops to zero, to release
119 // the resources managed by *this.
121 virtual void dispose() = 0; // nothrow
123 // destroy() is called when weak_count_ drops to zero.
125 virtual void destroy() // nothrow
130 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
131 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
132 virtual void * get_untyped_deleter() = 0;
136 atomic_increment( &use_count_ );
139 bool add_ref_lock() // true on success
141 return atomic_conditional_increment( &use_count_ ) != 0;
144 void release() // nothrow
146 if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
153 void weak_add_ref() // nothrow
155 atomic_increment( &weak_count_ );
158 void weak_release() // nothrow
160 if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
166 long use_count() const // nothrow
168 return static_cast<int const volatile &>( use_count_ );
172 } // namespace detail
176 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED