1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // detail/sp_counted_base_std_atomic.hpp - C++11 std::atomic
12 // Copyright (c) 2007, 2013 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/detail/sp_typeinfo.hpp>
28 inline void atomic_increment( std::atomic_int_least32_t * pw )
30 pw->fetch_add( 1, std::memory_order_relaxed );
33 inline std::int_least32_t atomic_decrement( std::atomic_int_least32_t * pw )
35 return pw->fetch_sub( 1, std::memory_order_acq_rel );
38 inline std::int_least32_t atomic_conditional_increment( std::atomic_int_least32_t * pw )
41 // if( r != 0 ) ++*pw;
44 std::int_least32_t r = pw->load( std::memory_order_relaxed );
53 if( pw->compare_exchange_weak( r, r + 1, std::memory_order_relaxed, std::memory_order_relaxed ) )
64 sp_counted_base( sp_counted_base const & );
65 sp_counted_base & operator= ( sp_counted_base const & );
67 std::atomic_int_least32_t use_count_; // #shared
68 std::atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
72 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
76 virtual ~sp_counted_base() // nothrow
80 // dispose() is called when use_count_ drops to zero, to release
81 // the resources managed by *this.
83 virtual void dispose() = 0; // nothrow
85 // destroy() is called when weak_count_ drops to zero.
87 virtual void destroy() // nothrow
92 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
93 virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
94 virtual void * get_untyped_deleter() = 0;
98 atomic_increment( &use_count_ );
101 bool add_ref_lock() // true on success
103 return atomic_conditional_increment( &use_count_ ) != 0;
106 void release() // nothrow
108 if( atomic_decrement( &use_count_ ) == 1 )
115 void weak_add_ref() // nothrow
117 atomic_increment( &weak_count_ );
120 void weak_release() // nothrow
122 if( atomic_decrement( &weak_count_ ) == 1 )
128 long use_count() const // nothrow
130 return use_count_.load( std::memory_order_acquire );
134 } // namespace detail
138 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_STD_ATOMIC_HPP_INCLUDED