1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // detail/sp_counted_base_gcc_sparc.hpp - g++ on Sparc V8+
12 // Copyright (c) 2006 Piotr Wyderski
13 // Copyright (c) 2006 Tomas Puverle
14 // Copyright (c) 2006 Peter Dimov
16 // Distributed under the Boost Software License, Version 1.0.
17 // See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt
20 // Thanks to Michael van der Westhuizen
22 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
23 #include <boost/config.hpp>
24 #include <inttypes.h> // int32_t
32 inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
34 __asm__ __volatile__( "cas [%1], %2, %0"
36 : "r" (dest_), "r" (compare_)
42 inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
52 if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
59 inline void atomic_increment( int32_t * pw )
61 atomic_fetch_and_add( pw, 1 );
64 inline int32_t atomic_decrement( int32_t * pw )
66 return atomic_fetch_and_add( pw, -1 );
69 inline int32_t atomic_conditional_increment( int32_t * pw )
72 // if( r != 0 ) ++*pw;
84 if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
91 class BOOST_SYMBOL_VISIBLE sp_counted_base
95 sp_counted_base( sp_counted_base const & );
96 sp_counted_base & operator= ( sp_counted_base const & );
98 int32_t use_count_; // #shared
99 int32_t weak_count_; // #weak + (#shared != 0)
103 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
107 virtual ~sp_counted_base() // nothrow
111 // dispose() is called when use_count_ drops to zero, to release
112 // the resources managed by *this.
114 virtual void dispose() = 0; // nothrow
116 // destroy() is called when weak_count_ drops to zero.
118 virtual void destroy() // nothrow
123 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
124 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
125 virtual void * get_untyped_deleter() = 0;
129 atomic_increment( &use_count_ );
132 bool add_ref_lock() // true on success
134 return atomic_conditional_increment( &use_count_ ) != 0;
137 void release() // nothrow
139 if( atomic_decrement( &use_count_ ) == 1 )
146 void weak_add_ref() // nothrow
148 atomic_increment( &weak_count_ );
151 void weak_release() // nothrow
153 if( atomic_decrement( &weak_count_ ) == 1 )
159 long use_count() const // nothrow
161 return const_cast< int32_t const volatile & >( use_count_ );
165 } // namespace detail
169 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED