]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/atomic_shared_ptr.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / smart_ptr / atomic_shared_ptr.hpp
1 #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
2 #define BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED
3
4 //
5 // atomic_shared_ptr.hpp
6 //
7 // Copyright 2017 Peter Dimov
8 //
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12 //
13 // See http://www.boost.org/libs/smart_ptr/ for documentation.
14 //
15
16 #include <boost/smart_ptr/shared_ptr.hpp>
17 #include <boost/smart_ptr/detail/spinlock.hpp>
18 #include <cstring>
19
20 namespace boost
21 {
22
23 template<class T> class atomic_shared_ptr
24 {
25 private:
26
27 boost::shared_ptr<T> p_;
28
29 mutable boost::detail::spinlock l_;
30
31 atomic_shared_ptr(const atomic_shared_ptr&);
32 atomic_shared_ptr& operator=(const atomic_shared_ptr&);
33
34 private:
35
36 bool compare_exchange( shared_ptr<T>& v, shared_ptr<T> w ) BOOST_SP_NOEXCEPT
37 {
38 l_.lock();
39
40 if( p_._internal_equiv( v ) )
41 {
42 p_.swap( w );
43
44 l_.unlock();
45 return true;
46 }
47 else
48 {
49 shared_ptr<T> tmp( p_ );
50
51 l_.unlock();
52
53 tmp.swap( v );
54 return false;
55 }
56 }
57
58 public:
59
60 #if !defined( BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX ) && !defined( BOOST_NO_CXX11_CONSTEXPR )
61
62 constexpr atomic_shared_ptr() BOOST_SP_NOEXCEPT: l_ BOOST_DETAIL_SPINLOCK_INIT
63 {
64 }
65
66 #else
67
68 atomic_shared_ptr() BOOST_SP_NOEXCEPT
69 {
70 boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
71 std::memcpy( &l_, &init, sizeof( init ) );
72 }
73
74 #endif
75
76 atomic_shared_ptr( shared_ptr<T> p ) BOOST_SP_NOEXCEPT
77 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
78 : p_( std::move( p ) )
79 #else
80 : p_( p )
81 #endif
82 {
83 boost::detail::spinlock init = BOOST_DETAIL_SPINLOCK_INIT;
84 std::memcpy( &l_, &init, sizeof( init ) );
85 }
86
87 atomic_shared_ptr& operator=( shared_ptr<T> r ) BOOST_SP_NOEXCEPT
88 {
89 boost::detail::spinlock::scoped_lock lock( l_ );
90 p_.swap( r );
91
92 return *this;
93 }
94
95 BOOST_CONSTEXPR bool is_lock_free() const BOOST_SP_NOEXCEPT
96 {
97 return false;
98 }
99
100 shared_ptr<T> load( int = 0 ) const BOOST_SP_NOEXCEPT
101 {
102 boost::detail::spinlock::scoped_lock lock( l_ );
103 return p_;
104 }
105
106 operator shared_ptr<T>() const BOOST_SP_NOEXCEPT
107 {
108 boost::detail::spinlock::scoped_lock lock( l_ );
109 return p_;
110 }
111
112 void store( shared_ptr<T> r, int = 0 ) BOOST_SP_NOEXCEPT
113 {
114 boost::detail::spinlock::scoped_lock lock( l_ );
115 p_.swap( r );
116 }
117
118 shared_ptr<T> exchange( shared_ptr<T> r, int = 0 ) BOOST_SP_NOEXCEPT
119 {
120 {
121 boost::detail::spinlock::scoped_lock lock( l_ );
122 p_.swap( r );
123 }
124
125 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
126
127 return std::move( r );
128
129 #else
130
131 return r;
132
133 #endif
134 }
135
136 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) BOOST_SP_NOEXCEPT
137 {
138 return compare_exchange( v, w );
139 }
140
141 bool compare_exchange_weak( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) BOOST_SP_NOEXCEPT
142 {
143 return compare_exchange( v, w );
144 }
145
146 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int, int ) BOOST_SP_NOEXCEPT
147 {
148 return compare_exchange( v, w );
149 }
150
151 bool compare_exchange_strong( shared_ptr<T>& v, const shared_ptr<T>& w, int = 0 ) BOOST_SP_NOEXCEPT
152 {
153 return compare_exchange( v, w );
154 }
155
156 #if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
157
158 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) BOOST_SP_NOEXCEPT
159 {
160 return compare_exchange( v, std::move( w ) );
161 }
162
163 bool compare_exchange_weak( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) BOOST_SP_NOEXCEPT
164 {
165 return compare_exchange( v, std::move( w ) );
166 }
167
168 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int, int ) BOOST_SP_NOEXCEPT
169 {
170 return compare_exchange( v, std::move( w ) );
171 }
172
173 bool compare_exchange_strong( shared_ptr<T>& v, shared_ptr<T>&& w, int = 0 ) BOOST_SP_NOEXCEPT
174 {
175 return compare_exchange( v, std::move( w ) );
176 }
177
178 #endif
179 };
180
181 } // namespace boost
182
183 #endif // #ifndef BOOST_SMART_PTR_ATOMIC_SHARED_PTR_HPP_INCLUDED