1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
10 // detail/sp_counted_base_clang.hpp - __c11 clang intrinsics
12 // Copyright (c) 2007, 2013, 2015 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/smart_ptr/detail/sp_noexcept.hpp>
20 #include <boost/config.hpp>
21 #include <boost/cstdint.hpp>
29 typedef _Atomic( boost::int_least32_t ) atomic_int_least32_t;
31 inline void atomic_increment( atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
33 __c11_atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
36 inline boost::int_least32_t atomic_decrement( atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
38 return __c11_atomic_fetch_sub( pw, 1, __ATOMIC_ACQ_REL );
41 inline boost::int_least32_t atomic_conditional_increment( atomic_int_least32_t * pw ) BOOST_SP_NOEXCEPT
44 // if( r != 0 ) ++*pw;
47 boost::int_least32_t r = __c11_atomic_load( pw, __ATOMIC_RELAXED );
56 if( __c11_atomic_compare_exchange_weak( pw, &r, r + 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED ) )
63 #if defined(__clang__)
64 # pragma clang diagnostic push
65 # pragma clang diagnostic ignored "-Wweak-vtables"
68 class BOOST_SYMBOL_VISIBLE sp_counted_base
72 sp_counted_base( sp_counted_base const & );
73 sp_counted_base & operator= ( sp_counted_base const & );
75 atomic_int_least32_t use_count_; // #shared
76 atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
80 sp_counted_base() BOOST_SP_NOEXCEPT
82 __c11_atomic_init( &use_count_, 1 );
83 __c11_atomic_init( &weak_count_, 1 );
86 virtual ~sp_counted_base() /*BOOST_SP_NOEXCEPT*/
90 // dispose() is called when use_count_ drops to zero, to release
91 // the resources managed by *this.
93 virtual void dispose() BOOST_SP_NOEXCEPT = 0; // nothrow
95 // destroy() is called when weak_count_ drops to zero.
97 virtual void destroy() BOOST_SP_NOEXCEPT // nothrow
102 virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
103 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT = 0;
104 virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT = 0;
106 void add_ref_copy() BOOST_SP_NOEXCEPT
108 atomic_increment( &use_count_ );
111 bool add_ref_lock() BOOST_SP_NOEXCEPT // true on success
113 return atomic_conditional_increment( &use_count_ ) != 0;
116 void release() BOOST_SP_NOEXCEPT
118 if( atomic_decrement( &use_count_ ) == 1 )
125 void weak_add_ref() BOOST_SP_NOEXCEPT
127 atomic_increment( &weak_count_ );
130 void weak_release() BOOST_SP_NOEXCEPT
132 if( atomic_decrement( &weak_count_ ) == 1 )
138 long use_count() const BOOST_SP_NOEXCEPT
140 return __c11_atomic_load( const_cast< atomic_int_least32_t* >( &use_count_ ), __ATOMIC_ACQUIRE );
144 #if defined(__clang__)
145 # pragma clang diagnostic pop
148 } // namespace detail
152 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED