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/smart_ptr/detail/sp_obsolete.hpp>
29 #include <boost/config.hpp>
31 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
33 #include <boost/config/pragma_message.hpp>
34 BOOST_PRAGMA_MESSAGE("Using g++/x86 sp_counted_base")
46 inline int atomic_exchange_and_add( int * pw, int dv )
58 "=m"( *pw ), "=r"( r ): // outputs (%0, %1)
59 "m"( *pw ), "1"( dv ): // inputs (%2, %3 == %1)
60 "memory", "cc" // clobbers
66 inline void atomic_increment( int * pw )
68 //atomic_exchange_and_add( pw, 1 );
74 "=m"( *pw ): // output (%0)
75 "m"( *pw ): // input (%1)
80 inline int atomic_conditional_increment( int * pw )
83 // if( rv != 0 ) ++*pw;
92 "test %%eax, %%eax\n\t"
100 "=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2)
101 "m"( *pw ): // input (%3)
108 class BOOST_SYMBOL_VISIBLE sp_counted_base
112 sp_counted_base( sp_counted_base const & );
113 sp_counted_base & operator= ( sp_counted_base const & );
115 int use_count_; // #shared
116 int weak_count_; // #weak + (#shared != 0)
120 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
124 virtual ~sp_counted_base() // nothrow
128 // dispose() is called when use_count_ drops to zero, to release
129 // the resources managed by *this.
131 virtual void dispose() = 0; // nothrow
133 // destroy() is called when weak_count_ drops to zero.
135 virtual void destroy() // nothrow
140 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
141 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
142 virtual void * get_untyped_deleter() = 0;
146 atomic_increment( &use_count_ );
149 bool add_ref_lock() // true on success
151 return atomic_conditional_increment( &use_count_ ) != 0;
154 void release() // nothrow
156 if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
163 void weak_add_ref() // nothrow
165 atomic_increment( &weak_count_ );
168 void weak_release() // nothrow
170 if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
176 long use_count() const // nothrow
178 return static_cast<int const volatile &>( use_count_ );
182 } // namespace detail
186 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED