shared_ptr_atomic.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // shared_ptr atomic access -*- C++ -*-
  2. // Copyright (C) 2014-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/shared_ptr_atomic.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{memory}
  23. */
  24. #ifndef _SHARED_PTR_ATOMIC_H
  25. #define _SHARED_PTR_ATOMIC_H 1
  26. #include <bits/atomic_base.h>
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. /**
  31. * @addtogroup pointer_abstractions
  32. * @{
  33. */
  34. struct _Sp_locker
  35. {
  36. _Sp_locker(const _Sp_locker&) = delete;
  37. _Sp_locker& operator=(const _Sp_locker&) = delete;
  38. #ifdef __GTHREADS
  39. explicit
  40. _Sp_locker(const void*) noexcept;
  41. _Sp_locker(const void*, const void*) noexcept;
  42. ~_Sp_locker();
  43. private:
  44. unsigned char _M_key1;
  45. unsigned char _M_key2;
  46. #else
  47. explicit _Sp_locker(const void*, const void* = nullptr) { }
  48. #endif
  49. };
  50. /**
  51. * @brief Report whether shared_ptr atomic operations are lock-free.
  52. * @param __p A non-null pointer to a shared_ptr object.
  53. * @return True if atomic access to @c *__p is lock-free, false otherwise.
  54. * @{
  55. */
  56. template<typename _Tp, _Lock_policy _Lp>
  57. inline bool
  58. atomic_is_lock_free(const __shared_ptr<_Tp, _Lp>* __p)
  59. {
  60. #ifdef __GTHREADS
  61. return __gthread_active_p() == 0;
  62. #else
  63. return true;
  64. #endif
  65. }
  66. template<typename _Tp>
  67. inline bool
  68. atomic_is_lock_free(const shared_ptr<_Tp>* __p)
  69. { return std::atomic_is_lock_free<_Tp, __default_lock_policy>(__p); }
  70. // @}
  71. /**
  72. * @brief Atomic load for shared_ptr objects.
  73. * @param __p A non-null pointer to a shared_ptr object.
  74. * @return @c *__p
  75. *
  76. * The memory order shall not be @c memory_order_release or
  77. * @c memory_order_acq_rel.
  78. * @{
  79. */
  80. template<typename _Tp>
  81. inline shared_ptr<_Tp>
  82. atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
  83. {
  84. _Sp_locker __lock{__p};
  85. return *__p;
  86. }
  87. template<typename _Tp>
  88. inline shared_ptr<_Tp>
  89. atomic_load(const shared_ptr<_Tp>* __p)
  90. { return std::atomic_load_explicit(__p, memory_order_seq_cst); }
  91. template<typename _Tp, _Lock_policy _Lp>
  92. inline __shared_ptr<_Tp, _Lp>
  93. atomic_load_explicit(const __shared_ptr<_Tp, _Lp>* __p, memory_order)
  94. {
  95. _Sp_locker __lock{__p};
  96. return *__p;
  97. }
  98. template<typename _Tp, _Lock_policy _Lp>
  99. inline __shared_ptr<_Tp, _Lp>
  100. atomic_load(const __shared_ptr<_Tp, _Lp>* __p)
  101. { return std::atomic_load_explicit(__p, memory_order_seq_cst); }
  102. // @}
  103. /**
  104. * @brief Atomic store for shared_ptr objects.
  105. * @param __p A non-null pointer to a shared_ptr object.
  106. * @param __r The value to store.
  107. *
  108. * The memory order shall not be @c memory_order_acquire or
  109. * @c memory_order_acq_rel.
  110. * @{
  111. */
  112. template<typename _Tp>
  113. inline void
  114. atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r,
  115. memory_order)
  116. {
  117. _Sp_locker __lock{__p};
  118. __p->swap(__r); // use swap so that **__p not destroyed while lock held
  119. }
  120. template<typename _Tp>
  121. inline void
  122. atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  123. { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
  124. template<typename _Tp, _Lock_policy _Lp>
  125. inline void
  126. atomic_store_explicit(__shared_ptr<_Tp, _Lp>* __p,
  127. __shared_ptr<_Tp, _Lp> __r,
  128. memory_order)
  129. {
  130. _Sp_locker __lock{__p};
  131. __p->swap(__r); // use swap so that **__p not destroyed while lock held
  132. }
  133. template<typename _Tp, _Lock_policy _Lp>
  134. inline void
  135. atomic_store(__shared_ptr<_Tp, _Lp>* __p, __shared_ptr<_Tp, _Lp> __r)
  136. { std::atomic_store_explicit(__p, std::move(__r), memory_order_seq_cst); }
  137. // @}
  138. /**
  139. * @brief Atomic exchange for shared_ptr objects.
  140. * @param __p A non-null pointer to a shared_ptr object.
  141. * @param __r New value to store in @c *__p.
  142. * @return The original value of @c *__p
  143. * @{
  144. */
  145. template<typename _Tp>
  146. inline shared_ptr<_Tp>
  147. atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r,
  148. memory_order)
  149. {
  150. _Sp_locker __lock{__p};
  151. __p->swap(__r);
  152. return __r;
  153. }
  154. template<typename _Tp>
  155. inline shared_ptr<_Tp>
  156. atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  157. {
  158. return std::atomic_exchange_explicit(__p, std::move(__r),
  159. memory_order_seq_cst);
  160. }
  161. template<typename _Tp, _Lock_policy _Lp>
  162. inline __shared_ptr<_Tp, _Lp>
  163. atomic_exchange_explicit(__shared_ptr<_Tp, _Lp>* __p,
  164. __shared_ptr<_Tp, _Lp> __r,
  165. memory_order)
  166. {
  167. _Sp_locker __lock{__p};
  168. __p->swap(__r);
  169. return __r;
  170. }
  171. template<typename _Tp, _Lock_policy _Lp>
  172. inline __shared_ptr<_Tp, _Lp>
  173. atomic_exchange(__shared_ptr<_Tp, _Lp>* __p, __shared_ptr<_Tp, _Lp> __r)
  174. {
  175. return std::atomic_exchange_explicit(__p, std::move(__r),
  176. memory_order_seq_cst);
  177. }
  178. // @}
  179. /**
  180. * @brief Atomic compare-and-swap for shared_ptr objects.
  181. * @param __p A non-null pointer to a shared_ptr object.
  182. * @param __v A non-null pointer to a shared_ptr object.
  183. * @param __w A non-null pointer to a shared_ptr object.
  184. * @return True if @c *__p was equivalent to @c *__v, false otherwise.
  185. *
  186. * The memory order for failure shall not be @c memory_order_release or
  187. * @c memory_order_acq_rel, or stronger than the memory order for success.
  188. * @{
  189. */
  190. template<typename _Tp>
  191. bool
  192. atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p,
  193. shared_ptr<_Tp>* __v,
  194. shared_ptr<_Tp> __w,
  195. memory_order,
  196. memory_order)
  197. {
  198. shared_ptr<_Tp> __x; // goes out of scope after __lock
  199. _Sp_locker __lock{__p, __v};
  200. owner_less<shared_ptr<_Tp>> __less;
  201. if (*__p == *__v && !__less(*__p, *__v) && !__less(*__v, *__p))
  202. {
  203. __x = std::move(*__p);
  204. *__p = std::move(__w);
  205. return true;
  206. }
  207. __x = std::move(*__v);
  208. *__v = *__p;
  209. return false;
  210. }
  211. template<typename _Tp>
  212. inline bool
  213. atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
  214. shared_ptr<_Tp> __w)
  215. {
  216. return std::atomic_compare_exchange_strong_explicit(__p, __v,
  217. std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
  218. }
  219. template<typename _Tp>
  220. inline bool
  221. atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p,
  222. shared_ptr<_Tp>* __v,
  223. shared_ptr<_Tp> __w,
  224. memory_order __success,
  225. memory_order __failure)
  226. {
  227. return std::atomic_compare_exchange_strong_explicit(__p, __v,
  228. std::move(__w), __success, __failure);
  229. }
  230. template<typename _Tp>
  231. inline bool
  232. atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
  233. shared_ptr<_Tp> __w)
  234. {
  235. return std::atomic_compare_exchange_weak_explicit(__p, __v,
  236. std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
  237. }
  238. template<typename _Tp, _Lock_policy _Lp>
  239. bool
  240. atomic_compare_exchange_strong_explicit(__shared_ptr<_Tp, _Lp>* __p,
  241. __shared_ptr<_Tp, _Lp>* __v,
  242. __shared_ptr<_Tp, _Lp> __w,
  243. memory_order,
  244. memory_order)
  245. {
  246. __shared_ptr<_Tp, _Lp> __x; // goes out of scope after __lock
  247. _Sp_locker __lock{__p, __v};
  248. owner_less<__shared_ptr<_Tp, _Lp>> __less;
  249. if (*__p == *__v && !__less(*__p, *__v) && !__less(*__v, *__p))
  250. {
  251. __x = std::move(*__p);
  252. *__p = std::move(__w);
  253. return true;
  254. }
  255. __x = std::move(*__v);
  256. *__v = *__p;
  257. return false;
  258. }
  259. template<typename _Tp, _Lock_policy _Lp>
  260. inline bool
  261. atomic_compare_exchange_strong(__shared_ptr<_Tp, _Lp>* __p,
  262. __shared_ptr<_Tp, _Lp>* __v,
  263. __shared_ptr<_Tp, _Lp> __w)
  264. {
  265. return std::atomic_compare_exchange_strong_explicit(__p, __v,
  266. std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
  267. }
  268. template<typename _Tp, _Lock_policy _Lp>
  269. inline bool
  270. atomic_compare_exchange_weak_explicit(__shared_ptr<_Tp, _Lp>* __p,
  271. __shared_ptr<_Tp, _Lp>* __v,
  272. __shared_ptr<_Tp, _Lp> __w,
  273. memory_order __success,
  274. memory_order __failure)
  275. {
  276. return std::atomic_compare_exchange_strong_explicit(__p, __v,
  277. std::move(__w), __success, __failure);
  278. }
  279. template<typename _Tp, _Lock_policy _Lp>
  280. inline bool
  281. atomic_compare_exchange_weak(__shared_ptr<_Tp, _Lp>* __p,
  282. __shared_ptr<_Tp, _Lp>* __v,
  283. __shared_ptr<_Tp, _Lp> __w)
  284. {
  285. return std::atomic_compare_exchange_weak_explicit(__p, __v,
  286. std::move(__w), memory_order_seq_cst, memory_order_seq_cst);
  287. }
  288. // @}
  289. // @} group pointer_abstractions
  290. _GLIBCXX_END_NAMESPACE_VERSION
  291. } // namespace
  292. #endif // _SHARED_PTR_ATOMIC_H