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/smart_ptr/detail/sp_obsolete.hpp>
24 #include <boost/config.hpp>
25 #include <inttypes.h> // int32_t
27 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
29 #include <boost/config/pragma_message.hpp>
30 BOOST_PRAGMA_MESSAGE("Using g++/Sparc sp_counted_base")
42 inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
44 __asm__ __volatile__( "cas [%1], %2, %0"
46 : "r" (dest_), "r" (compare_)
52 inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
62 if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
69 inline void atomic_increment( int32_t * pw )
71 atomic_fetch_and_add( pw, 1 );
74 inline int32_t atomic_decrement( int32_t * pw )
76 return atomic_fetch_and_add( pw, -1 );
79 inline int32_t atomic_conditional_increment( int32_t * pw )
82 // if( r != 0 ) ++*pw;
94 if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
101 class BOOST_SYMBOL_VISIBLE sp_counted_base
105 sp_counted_base( sp_counted_base const & );
106 sp_counted_base & operator= ( sp_counted_base const & );
108 int32_t use_count_; // #shared
109 int32_t weak_count_; // #weak + (#shared != 0)
113 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
117 virtual ~sp_counted_base() // nothrow
121 // dispose() is called when use_count_ drops to zero, to release
122 // the resources managed by *this.
124 virtual void dispose() = 0; // nothrow
126 // destroy() is called when weak_count_ drops to zero.
128 virtual void destroy() // nothrow
133 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
134 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
135 virtual void * get_untyped_deleter() = 0;
139 atomic_increment( &use_count_ );
142 bool add_ref_lock() // true on success
144 return atomic_conditional_increment( &use_count_ ) != 0;
147 void release() // nothrow
149 if( atomic_decrement( &use_count_ ) == 1 )
156 void weak_add_ref() // nothrow
158 atomic_increment( &weak_count_ );
161 void weak_release() // nothrow
163 if( atomic_decrement( &weak_count_ ) == 1 )
169 long use_count() const // nothrow
171 return const_cast< int32_t const volatile & >( use_count_ );
175 } // namespace detail
179 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED