1 #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
2 #define BOOST_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/detail/sp_typeinfo.hpp>
23 #include <inttypes.h> // int32_t
31 inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
33 __asm__ __volatile__( "cas %0, %2, %1"
34 : "+m" (*dest_), "+r" (swap_)
41 inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
51 if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
58 inline void atomic_increment( int32_t * pw )
60 atomic_fetch_and_add( pw, 1 );
63 inline int32_t atomic_decrement( int32_t * pw )
65 return atomic_fetch_and_add( pw, -1 );
68 inline int32_t atomic_conditional_increment( int32_t * pw )
71 // if( r != 0 ) ++*pw;
83 if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
94 sp_counted_base( sp_counted_base const & );
95 sp_counted_base & operator= ( sp_counted_base const & );
97 int32_t use_count_; // #shared
98 int32_t weak_count_; // #weak + (#shared != 0)
102 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
106 virtual ~sp_counted_base() // nothrow
110 // dispose() is called when use_count_ drops to zero, to release
111 // the resources managed by *this.
113 virtual void dispose() = 0; // nothrow
115 // destroy() is called when weak_count_ drops to zero.
117 virtual void destroy() // nothrow
122 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
126 atomic_increment( &use_count_ );
129 bool add_ref_lock() // true on success
131 return atomic_conditional_increment( &use_count_ ) != 0;
134 void release() // nothrow
136 if( atomic_decrement( &use_count_ ) == 1 )
143 void weak_add_ref() // nothrow
145 atomic_increment( &weak_count_ );
148 void weak_release() // nothrow
150 if( atomic_decrement( &weak_count_ ) == 1 )
156 long use_count() const // nothrow
158 return const_cast< int32_t const volatile & >( use_count_ );
162 } // namespace detail
166 #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED