1#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_ATOMIC_HPP_INCLUDED
2#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_ATOMIC_HPP_INCLUDED
3
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10// detail/sp_counted_base_gcc_atomic.hpp - g++ 4.7+ __atomic intrinsics
11//
12// Copyright 2007, 2020 Peter Dimov
13// Distributed under the Boost Software License, Version 1.0.
14// https://www.boost.org/LICENSE_1_0.txt
15
16#include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
17#include <boost/config.hpp>
18#include <boost/cstdint.hpp>
19
20#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
21
22#include <boost/config/pragma_message.hpp>
23BOOST_PRAGMA_MESSAGE("Using __atomic sp_counted_base")
24
25#endif
26
27namespace boost
28{
29
30namespace detail
31{
32
33inline void atomic_increment( boost::uint_least32_t * pw )
34{
35 __atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
36}
37
38inline boost::uint_least32_t atomic_decrement( boost::uint_least32_t * pw )
39{
40 return __atomic_fetch_sub( pw, 1, __ATOMIC_ACQ_REL );
41}
42
43inline boost::uint_least32_t atomic_conditional_increment( boost::uint_least32_t * pw )
44{
45 // long r = *pw;
46 // if( r != 0 ) ++*pw;
47 // return r;
48
49 boost::uint_least32_t r = __atomic_load_n( pw, __ATOMIC_RELAXED );
50
51 for( ;; )
52 {
53 if( r == 0 )
54 {
55 return r;
56 }
57
58 if( __atomic_compare_exchange_n( pw, &r, r + 1, true, __ATOMIC_RELAXED, __ATOMIC_RELAXED ) )
59 {
60 return r;
61 }
62 }
63}
64
65inline boost::uint_least32_t atomic_load( boost::uint_least32_t const * pw )
66{
67 return __atomic_load_n( pw, __ATOMIC_ACQUIRE );
68}
69
70class BOOST_SYMBOL_VISIBLE sp_counted_base
71{
72private:
73
74 sp_counted_base( sp_counted_base const & );
75 sp_counted_base & operator= ( sp_counted_base const & );
76
77 boost::uint_least32_t use_count_; // #shared
78 boost::uint_least32_t weak_count_; // #weak + (#shared != 0)
79
80public:
81
82 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
83 {
84 }
85
86 virtual ~sp_counted_base() // nothrow
87 {
88 }
89
90 // dispose() is called when use_count_ drops to zero, to release
91 // the resources managed by *this.
92
93 virtual void dispose() = 0; // nothrow
94
95 // destroy() is called when weak_count_ drops to zero.
96
97 virtual void destroy() // nothrow
98 {
99 delete this;
100 }
101
102 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
103 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
104 virtual void * get_untyped_deleter() = 0;
105
106 void add_ref_copy()
107 {
108 atomic_increment( pw: &use_count_ );
109 }
110
111 bool add_ref_lock() // true on success
112 {
113 return atomic_conditional_increment( pw: &use_count_ ) != 0;
114 }
115
116 void release() // nothrow
117 {
118 if( atomic_decrement( pw: &use_count_ ) == 1 )
119 {
120 dispose();
121 weak_release();
122 }
123 }
124
125 void weak_add_ref() // nothrow
126 {
127 atomic_increment( pw: &weak_count_ );
128 }
129
130 void weak_release() // nothrow
131 {
132 if( atomic_decrement( pw: &weak_count_ ) == 1 )
133 {
134 destroy();
135 }
136 }
137
138 long use_count() const // nothrow
139 {
140 return atomic_load( pw: &use_count_ );
141 }
142};
143
144} // namespace detail
145
146} // namespace boost
147
148#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SYNC_HPP_INCLUDED
149

source code of include/boost/smart_ptr/detail/sp_counted_base_gcc_atomic.hpp