1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // detail/sp_counted_base_sync.hpp - g++ 4.1+ __sync intrinsics
12 // Copyright (c) 2007 Peter Dimov
14 // Distributed under the Boost Software License, Version 1.0.
15 // See accompanying file LICENSE_1_0.txt or copy at
16 // http://www.boost.org/LICENSE_1_0.txt
18 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
19 #include <boost/config.hpp>
22 #if defined( __ia64__ ) && defined( __INTEL_COMPILER )
23 # include <ia64intrin.h>
32 #if INT_MAX >= 2147483647
34 typedef int sp_int32_t;
38 typedef long sp_int32_t;
42 inline void atomic_increment( sp_int32_t * pw )
44 __sync_fetch_and_add( pw, 1 );
47 inline sp_int32_t atomic_decrement( sp_int32_t * pw )
49 return __sync_fetch_and_add( pw, -1 );
52 inline sp_int32_t atomic_conditional_increment( sp_int32_t * pw )
55 // if( r != 0 ) ++*pw;
67 sp_int32_t r2 = __sync_val_compare_and_swap( pw, r, r + 1 );
80 class BOOST_SYMBOL_VISIBLE sp_counted_base
84 sp_counted_base( sp_counted_base const & );
85 sp_counted_base & operator= ( sp_counted_base const & );
87 sp_int32_t use_count_; // #shared
88 sp_int32_t weak_count_; // #weak + (#shared != 0)
92 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
96 virtual ~sp_counted_base() // nothrow
100 // dispose() is called when use_count_ drops to zero, to release
101 // the resources managed by *this.
103 virtual void dispose() = 0; // nothrow
105 // destroy() is called when weak_count_ drops to zero.
107 virtual void destroy() // nothrow
112 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
113 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
114 virtual void * get_untyped_deleter() = 0;
118 atomic_increment( &use_count_ );
121 bool add_ref_lock() // true on success
123 return atomic_conditional_increment( &use_count_ ) != 0;
126 void release() // nothrow
128 if( atomic_decrement( &use_count_ ) == 1 )
135 void weak_add_ref() // nothrow
137 atomic_increment( &weak_count_ );
140 void weak_release() // nothrow
142 if( atomic_decrement( &weak_count_ ) == 1 )
148 long use_count() const // nothrow
150 return const_cast< sp_int32_t const volatile & >( use_count_ );
154 } // namespace detail
158 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_HPP_INCLUDED