1 // shared_ptr atomic access -*- C++ -*-
3 // Copyright (C) 2014-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/shared_ptr_atomic.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
30 #ifndef _SHARED_PTR_ATOMIC_H
31 #define _SHARED_PTR_ATOMIC_H 1
33 #include <bits/atomic_base.h>
35 namespace std _GLIBCXX_VISIBILITY(default)
37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 * @addtogroup pointer_abstractions
46 _Sp_locker(const _Sp_locker&) = delete;
47 _Sp_locker& operator=(const _Sp_locker&) = delete;
51 _Sp_locker(const void*) noexcept;
52 _Sp_locker(const void*, const void*) noexcept;
56 unsigned char _M_key1;
57 unsigned char _M_key2;
59 explicit _Sp_locker(const void*, const void* = nullptr) { }
64 * @brief Report whether shared_ptr atomic operations are lock-free.
65 * @param __p A non-null pointer to a shared_ptr object.
66 * @return True if atomic access to @c *__p is lock-free, false otherwise.
69 template<typename _Tp, _Lock_policy _Lp>
71 atomic_is_lock_free(const __shared_ptr<_Tp, _Lp>* __p)
74 return __gthread_active_p() == 0;
80 template<typename _Tp>
82 atomic_is_lock_free(const shared_ptr<_Tp>* __p)
83 { return std::atomic_is_lock_free<_Tp, __default_lock_policy>(__p); }
88 * @brief Atomic load for shared_ptr objects.
89 * @param __p A non-null pointer to a shared_ptr object.
92 * The memory order shall not be @c memory_order_release or
93 * @c memory_order_acq_rel.
96 template<typename _Tp>
97 inline shared_ptr<_Tp>
98 atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
100 _Sp_locker __lock{__p};
104 template<typename _Tp>
105 inline shared_ptr<_Tp>
106 atomic_load(const shared_ptr<_Tp>* __p)
107 { return std::atomic_load_explicit(__p, memory_order_seq_cst); }
109 template<typename _Tp, _Lock_policy _Lp>
110 inline __shared_ptr<_Tp, _Lp>
111 atomic_load_explicit(const __shared_ptr<_Tp, _Lp>* __p, memory_order)
113 _Sp_locker __lock{__p};
117 template<typename _Tp, _Lock_policy _Lp>
118 inline __shared_ptr<_Tp, _Lp>
119 atomic_load(const __shared_ptr<_Tp, _Lp>* __p)
120 { return std::atomic_load_explicit(__p, memory_order_seq_cst); }
124 * @brief Atomic store for shared_ptr objects.
125 * @param __p A non-null pointer to a shared_ptr object.
126 * @param __r The value to store.
128 * The memory order shall not be @c memory_order_acquire or
129 * @c memory_order_acq_rel.
132 template<typename _Tp>
134 atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r,
137 _Sp_locker __lock{__p};
138 __p->swap(__r); // use swap so that **__p not destroyed while lock held
141 template<typename _Tp>
143 atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
144 { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
146 template<typename _Tp, _Lock_policy _Lp>
148 atomic_store_explicit(__shared_ptr<_Tp, _Lp>* __p,
149 __shared_ptr<_Tp, _Lp> __r,
152 _Sp_locker __lock{__p};
153 __p->swap(__r); // use swap so that **__p not destroyed while lock held
156 template<typename _Tp, _Lock_policy _Lp>
158 atomic_store(__shared_ptr<_Tp, _Lp>* __p, __shared_ptr<_Tp, _Lp> __r)
159 { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
163 * @brief Atomic exchange for shared_ptr objects.
164 * @param __p A non-null pointer to a shared_ptr object.
165 * @param __r New value to store in @c *__p.
166 * @return The original value of @c *__p
169 template<typename _Tp>
170 inline shared_ptr<_Tp>
171 atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r,
174 _Sp_locker __lock{__p};
179 template<typename _Tp>
180 inline shared_ptr<_Tp>
181 atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
183 return std::atomic_exchange_explicit(__p, std::move(__r),
184 memory_order_seq_cst);
187 template<typename _Tp, _Lock_policy _Lp>
188 inline __shared_ptr<_Tp, _Lp>
189 atomic_exchange_explicit(__shared_ptr<_Tp, _Lp>* __p,
190 __shared_ptr<_Tp, _Lp> __r,
193 _Sp_locker __lock{__p};
198 template<typename _Tp, _Lock_policy _Lp>
199 inline __shared_ptr<_Tp, _Lp>
200 atomic_exchange(__shared_ptr<_Tp, _Lp>* __p, __shared_ptr<_Tp, _Lp> __r)
202 return std::atomic_exchange_explicit(__p, std::move(__r),
203 memory_order_seq_cst);
208 * @brief Atomic compare-and-swap for shared_ptr objects.
209 * @param __p A non-null pointer to a shared_ptr object.
210 * @param __v A non-null pointer to a shared_ptr object.
211 * @param __w A non-null pointer to a shared_ptr object.
212 * @return True if @c *__p was equivalent to @c *__v, false otherwise.
214 * The memory order for failure shall not be @c memory_order_release or
215 * @c memory_order_acq_rel, or stronger than the memory order for success.
218 template<typename _Tp>
220 atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p,
221 shared_ptr<_Tp>* __v,
226 shared_ptr<_Tp> __x; // goes out of scope after __lock
227 _Sp_locker __lock{__p, __v};
228 owner_less<shared_ptr<_Tp>> __less;
229 if (*__p == *__v && !__less(*__p, *__v) && !__less(*__v, *__p))
231 __x = std::move(*__p);
232 *__p = std::move(__w);
235 __x = std::move(*__v);
240 template<typename _Tp>
242 atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
245 return std::atomic_compare_exchange_strong_explicit(__p, __v,
246 std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
249 template<typename _Tp>
251 atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p,
252 shared_ptr<_Tp>* __v,
254 memory_order __success,
255 memory_order __failure)
257 return std::atomic_compare_exchange_strong_explicit(__p, __v,
258 std::move(__w), __success, __failure);
261 template<typename _Tp>
263 atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
266 return std::atomic_compare_exchange_weak_explicit(__p, __v,
267 std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
270 template<typename _Tp, _Lock_policy _Lp>
272 atomic_compare_exchange_strong_explicit(__shared_ptr<_Tp, _Lp>* __p,
273 __shared_ptr<_Tp, _Lp>* __v,
274 __shared_ptr<_Tp, _Lp> __w,
278 __shared_ptr<_Tp, _Lp> __x; // goes out of scope after __lock
279 _Sp_locker __lock{__p, __v};
280 owner_less<__shared_ptr<_Tp, _Lp>> __less;
281 if (*__p == *__v && !__less(*__p, *__v) && !__less(*__v, *__p))
283 __x = std::move(*__p);
284 *__p = std::move(__w);
287 __x = std::move(*__v);
292 template<typename _Tp, _Lock_policy _Lp>
294 atomic_compare_exchange_strong(__shared_ptr<_Tp, _Lp>* __p,
295 __shared_ptr<_Tp, _Lp>* __v,
296 __shared_ptr<_Tp, _Lp> __w)
298 return std::atomic_compare_exchange_strong_explicit(__p, __v,
299 std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
302 template<typename _Tp, _Lock_policy _Lp>
304 atomic_compare_exchange_weak_explicit(__shared_ptr<_Tp, _Lp>* __p,
305 __shared_ptr<_Tp, _Lp>* __v,
306 __shared_ptr<_Tp, _Lp> __w,
307 memory_order __success,
308 memory_order __failure)
310 return std::atomic_compare_exchange_strong_explicit(__p, __v,
311 std::move(__w), __success, __failure);
314 template<typename _Tp, _Lock_policy _Lp>
316 atomic_compare_exchange_weak(__shared_ptr<_Tp, _Lp>* __p,
317 __shared_ptr<_Tp, _Lp>* __v,
318 __shared_ptr<_Tp, _Lp> __w)
320 return std::atomic_compare_exchange_weak_explicit(__p, __v,
321 std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
325 // @} group pointer_abstractions
327 _GLIBCXX_END_NAMESPACE_VERSION
330 #endif // _SHARED_PTR_ATOMIC_H