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/detail/sp_typeinfo.hpp>
19 #include <boost/cstdint.hpp>
27 typedef _Atomic( boost::int_least32_t ) atomic_int_least32_t;
29 inline void atomic_increment( atomic_int_least32_t * pw )
31 __c11_atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
34 inline boost::int_least32_t atomic_decrement( atomic_int_least32_t * pw )
36 return __c11_atomic_fetch_sub( pw, 1, __ATOMIC_ACQ_REL );
39 inline boost::int_least32_t atomic_conditional_increment( atomic_int_least32_t * pw )
42 // if( r != 0 ) ++*pw;
45 boost::int_least32_t r = __c11_atomic_load( pw, __ATOMIC_RELAXED );
54 if( __c11_atomic_compare_exchange_weak( pw, &r, r + 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED ) )
61 #if defined(__clang__)
62 # pragma clang diagnostic push
63 # pragma clang diagnostic ignored "-Wweak-vtables"
70 sp_counted_base( sp_counted_base const & );
71 sp_counted_base & operator= ( sp_counted_base const & );
73 atomic_int_least32_t use_count_; // #shared
74 atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
80 __c11_atomic_init( &use_count_, 1 );
81 __c11_atomic_init( &weak_count_, 1 );
84 virtual ~sp_counted_base() // nothrow
88 // dispose() is called when use_count_ drops to zero, to release
89 // the resources managed by *this.
91 virtual void dispose() = 0; // nothrow
93 // destroy() is called when weak_count_ drops to zero.
95 virtual void destroy() // nothrow
100 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
101 virtual void * get_untyped_deleter() = 0;
105 atomic_increment( &use_count_ );
108 bool add_ref_lock() // true on success
110 return atomic_conditional_increment( &use_count_ ) != 0;
113 void release() // nothrow
115 if( atomic_decrement( &use_count_ ) == 1 )
122 void weak_add_ref() // nothrow
124 atomic_increment( &weak_count_ );
127 void weak_release() // nothrow
129 if( atomic_decrement( &weak_count_ ) == 1 )
135 long use_count() const // nothrow
137 return __c11_atomic_load( const_cast< atomic_int_least32_t* >( &use_count_ ), __ATOMIC_ACQUIRE );
141 #if defined(__clang__)
142 # pragma clang diagnostic pop
145 } // namespace detail
149 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED