atomic 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. // -*- C++ -*- header.
  2. // Copyright (C) 2008-2021 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 include/atomic
  21. * This is a Standard C++ Library header.
  22. */
  23. // Based on "C++ Atomic Types and Operations" by Hans Boehm and Lawrence Crowl.
  24. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2427.html
  25. #ifndef _GLIBCXX_ATOMIC
  26. #define _GLIBCXX_ATOMIC 1
  27. #pragma GCC system_header
  28. #if __cplusplus < 201103L
  29. # include <bits/c++0x_warning.h>
  30. #else
  31. #include <bits/atomic_base.h>
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @addtogroup atomics
  37. * @{
  38. */
  39. #if __cplusplus >= 201703L
  40. # define __cpp_lib_atomic_is_always_lock_free 201603
  41. #endif
  42. template<typename _Tp>
  43. struct atomic;
  44. /// atomic<bool>
  45. // NB: No operators or fetch-operations for this type.
  46. template<>
  47. struct atomic<bool>
  48. {
  49. using value_type = bool;
  50. private:
  51. __atomic_base<bool> _M_base;
  52. public:
  53. atomic() noexcept = default;
  54. ~atomic() noexcept = default;
  55. atomic(const atomic&) = delete;
  56. atomic& operator=(const atomic&) = delete;
  57. atomic& operator=(const atomic&) volatile = delete;
  58. constexpr atomic(bool __i) noexcept : _M_base(__i) { }
  59. bool
  60. operator=(bool __i) noexcept
  61. { return _M_base.operator=(__i); }
  62. bool
  63. operator=(bool __i) volatile noexcept
  64. { return _M_base.operator=(__i); }
  65. operator bool() const noexcept
  66. { return _M_base.load(); }
  67. operator bool() const volatile noexcept
  68. { return _M_base.load(); }
  69. bool
  70. is_lock_free() const noexcept { return _M_base.is_lock_free(); }
  71. bool
  72. is_lock_free() const volatile noexcept { return _M_base.is_lock_free(); }
  73. #if __cplusplus >= 201703L
  74. static constexpr bool is_always_lock_free = ATOMIC_BOOL_LOCK_FREE == 2;
  75. #endif
  76. void
  77. store(bool __i, memory_order __m = memory_order_seq_cst) noexcept
  78. { _M_base.store(__i, __m); }
  79. void
  80. store(bool __i, memory_order __m = memory_order_seq_cst) volatile noexcept
  81. { _M_base.store(__i, __m); }
  82. bool
  83. load(memory_order __m = memory_order_seq_cst) const noexcept
  84. { return _M_base.load(__m); }
  85. bool
  86. load(memory_order __m = memory_order_seq_cst) const volatile noexcept
  87. { return _M_base.load(__m); }
  88. bool
  89. exchange(bool __i, memory_order __m = memory_order_seq_cst) noexcept
  90. { return _M_base.exchange(__i, __m); }
  91. bool
  92. exchange(bool __i,
  93. memory_order __m = memory_order_seq_cst) volatile noexcept
  94. { return _M_base.exchange(__i, __m); }
  95. bool
  96. compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
  97. memory_order __m2) noexcept
  98. { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
  99. bool
  100. compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
  101. memory_order __m2) volatile noexcept
  102. { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
  103. bool
  104. compare_exchange_weak(bool& __i1, bool __i2,
  105. memory_order __m = memory_order_seq_cst) noexcept
  106. { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
  107. bool
  108. compare_exchange_weak(bool& __i1, bool __i2,
  109. memory_order __m = memory_order_seq_cst) volatile noexcept
  110. { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
  111. bool
  112. compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
  113. memory_order __m2) noexcept
  114. { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
  115. bool
  116. compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
  117. memory_order __m2) volatile noexcept
  118. { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
  119. bool
  120. compare_exchange_strong(bool& __i1, bool __i2,
  121. memory_order __m = memory_order_seq_cst) noexcept
  122. { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
  123. bool
  124. compare_exchange_strong(bool& __i1, bool __i2,
  125. memory_order __m = memory_order_seq_cst) volatile noexcept
  126. { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
  127. #if __cpp_lib_atomic_wait
  128. void
  129. wait(bool __old, memory_order __m = memory_order_seq_cst) const noexcept
  130. { _M_base.wait(__old, __m); }
  131. // TODO add const volatile overload
  132. void
  133. notify_one() const noexcept
  134. { _M_base.notify_one(); }
  135. void
  136. notify_all() const noexcept
  137. { _M_base.notify_all(); }
  138. #endif // __cpp_lib_atomic_wait
  139. };
  140. #if __cplusplus <= 201703L
  141. # define _GLIBCXX20_INIT(I)
  142. #else
  143. # define _GLIBCXX20_INIT(I) = I
  144. #endif
  145. /**
  146. * @brief Generic atomic type, primary class template.
  147. *
  148. * @tparam _Tp Type to be made atomic, must be trivially copyable.
  149. */
  150. template<typename _Tp>
  151. struct atomic
  152. {
  153. using value_type = _Tp;
  154. private:
  155. // Align 1/2/4/8/16-byte types to at least their size.
  156. static constexpr int _S_min_alignment
  157. = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || sizeof(_Tp) > 16
  158. ? 0 : sizeof(_Tp);
  159. static constexpr int _S_alignment
  160. = _S_min_alignment > alignof(_Tp) ? _S_min_alignment : alignof(_Tp);
  161. alignas(_S_alignment) _Tp _M_i _GLIBCXX20_INIT(_Tp());
  162. static_assert(__is_trivially_copyable(_Tp),
  163. "std::atomic requires a trivially copyable type");
  164. static_assert(sizeof(_Tp) > 0,
  165. "Incomplete or zero-sized types are not supported");
  166. #if __cplusplus > 201703L
  167. static_assert(is_copy_constructible_v<_Tp>);
  168. static_assert(is_move_constructible_v<_Tp>);
  169. static_assert(is_copy_assignable_v<_Tp>);
  170. static_assert(is_move_assignable_v<_Tp>);
  171. #endif
  172. public:
  173. atomic() = default;
  174. ~atomic() noexcept = default;
  175. atomic(const atomic&) = delete;
  176. atomic& operator=(const atomic&) = delete;
  177. atomic& operator=(const atomic&) volatile = delete;
  178. constexpr atomic(_Tp __i) noexcept : _M_i(__i) { }
  179. operator _Tp() const noexcept
  180. { return load(); }
  181. operator _Tp() const volatile noexcept
  182. { return load(); }
  183. _Tp
  184. operator=(_Tp __i) noexcept
  185. { store(__i); return __i; }
  186. _Tp
  187. operator=(_Tp __i) volatile noexcept
  188. { store(__i); return __i; }
  189. bool
  190. is_lock_free() const noexcept
  191. {
  192. // Produce a fake, minimally aligned pointer.
  193. return __atomic_is_lock_free(sizeof(_M_i),
  194. reinterpret_cast<void *>(-_S_alignment));
  195. }
  196. bool
  197. is_lock_free() const volatile noexcept
  198. {
  199. // Produce a fake, minimally aligned pointer.
  200. return __atomic_is_lock_free(sizeof(_M_i),
  201. reinterpret_cast<void *>(-_S_alignment));
  202. }
  203. #if __cplusplus >= 201703L
  204. static constexpr bool is_always_lock_free
  205. = __atomic_always_lock_free(sizeof(_M_i), 0);
  206. #endif
  207. void
  208. store(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept
  209. {
  210. __atomic_store(std::__addressof(_M_i), std::__addressof(__i), int(__m));
  211. }
  212. void
  213. store(_Tp __i, memory_order __m = memory_order_seq_cst) volatile noexcept
  214. {
  215. __atomic_store(std::__addressof(_M_i), std::__addressof(__i), int(__m));
  216. }
  217. _Tp
  218. load(memory_order __m = memory_order_seq_cst) const noexcept
  219. {
  220. alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
  221. _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
  222. __atomic_load(std::__addressof(_M_i), __ptr, int(__m));
  223. return *__ptr;
  224. }
  225. _Tp
  226. load(memory_order __m = memory_order_seq_cst) const volatile noexcept
  227. {
  228. alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
  229. _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
  230. __atomic_load(std::__addressof(_M_i), __ptr, int(__m));
  231. return *__ptr;
  232. }
  233. _Tp
  234. exchange(_Tp __i, memory_order __m = memory_order_seq_cst) noexcept
  235. {
  236. alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
  237. _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
  238. __atomic_exchange(std::__addressof(_M_i), std::__addressof(__i),
  239. __ptr, int(__m));
  240. return *__ptr;
  241. }
  242. _Tp
  243. exchange(_Tp __i,
  244. memory_order __m = memory_order_seq_cst) volatile noexcept
  245. {
  246. alignas(_Tp) unsigned char __buf[sizeof(_Tp)];
  247. _Tp* __ptr = reinterpret_cast<_Tp*>(__buf);
  248. __atomic_exchange(std::__addressof(_M_i), std::__addressof(__i),
  249. __ptr, int(__m));
  250. return *__ptr;
  251. }
  252. bool
  253. compare_exchange_weak(_Tp& __e, _Tp __i, memory_order __s,
  254. memory_order __f) noexcept
  255. {
  256. return __atomic_compare_exchange(std::__addressof(_M_i),
  257. std::__addressof(__e),
  258. std::__addressof(__i),
  259. true, int(__s), int(__f));
  260. }
  261. bool
  262. compare_exchange_weak(_Tp& __e, _Tp __i, memory_order __s,
  263. memory_order __f) volatile noexcept
  264. {
  265. return __atomic_compare_exchange(std::__addressof(_M_i),
  266. std::__addressof(__e),
  267. std::__addressof(__i),
  268. true, int(__s), int(__f));
  269. }
  270. bool
  271. compare_exchange_weak(_Tp& __e, _Tp __i,
  272. memory_order __m = memory_order_seq_cst) noexcept
  273. { return compare_exchange_weak(__e, __i, __m,
  274. __cmpexch_failure_order(__m)); }
  275. bool
  276. compare_exchange_weak(_Tp& __e, _Tp __i,
  277. memory_order __m = memory_order_seq_cst) volatile noexcept
  278. { return compare_exchange_weak(__e, __i, __m,
  279. __cmpexch_failure_order(__m)); }
  280. bool
  281. compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s,
  282. memory_order __f) noexcept
  283. {
  284. return __atomic_compare_exchange(std::__addressof(_M_i),
  285. std::__addressof(__e),
  286. std::__addressof(__i),
  287. false, int(__s), int(__f));
  288. }
  289. bool
  290. compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s,
  291. memory_order __f) volatile noexcept
  292. {
  293. return __atomic_compare_exchange(std::__addressof(_M_i),
  294. std::__addressof(__e),
  295. std::__addressof(__i),
  296. false, int(__s), int(__f));
  297. }
  298. bool
  299. compare_exchange_strong(_Tp& __e, _Tp __i,
  300. memory_order __m = memory_order_seq_cst) noexcept
  301. { return compare_exchange_strong(__e, __i, __m,
  302. __cmpexch_failure_order(__m)); }
  303. bool
  304. compare_exchange_strong(_Tp& __e, _Tp __i,
  305. memory_order __m = memory_order_seq_cst) volatile noexcept
  306. { return compare_exchange_strong(__e, __i, __m,
  307. __cmpexch_failure_order(__m)); }
  308. #if __cpp_lib_atomic_wait
  309. void
  310. wait(_Tp __old, memory_order __m = memory_order_seq_cst) const noexcept
  311. {
  312. std::__atomic_wait_address_v(&_M_i, __old,
  313. [__m, this] { return this->load(__m); });
  314. }
  315. // TODO add const volatile overload
  316. void
  317. notify_one() const noexcept
  318. { std::__atomic_notify_address(&_M_i, false); }
  319. void
  320. notify_all() const noexcept
  321. { std::__atomic_notify_address(&_M_i, true); }
  322. #endif // __cpp_lib_atomic_wait
  323. };
  324. #undef _GLIBCXX20_INIT
  325. /// Partial specialization for pointer types.
  326. template<typename _Tp>
  327. struct atomic<_Tp*>
  328. {
  329. using value_type = _Tp*;
  330. using difference_type = ptrdiff_t;
  331. typedef _Tp* __pointer_type;
  332. typedef __atomic_base<_Tp*> __base_type;
  333. __base_type _M_b;
  334. atomic() noexcept = default;
  335. ~atomic() noexcept = default;
  336. atomic(const atomic&) = delete;
  337. atomic& operator=(const atomic&) = delete;
  338. atomic& operator=(const atomic&) volatile = delete;
  339. constexpr atomic(__pointer_type __p) noexcept : _M_b(__p) { }
  340. operator __pointer_type() const noexcept
  341. { return __pointer_type(_M_b); }
  342. operator __pointer_type() const volatile noexcept
  343. { return __pointer_type(_M_b); }
  344. __pointer_type
  345. operator=(__pointer_type __p) noexcept
  346. { return _M_b.operator=(__p); }
  347. __pointer_type
  348. operator=(__pointer_type __p) volatile noexcept
  349. { return _M_b.operator=(__p); }
  350. __pointer_type
  351. operator++(int) noexcept
  352. {
  353. #if __cplusplus >= 201703L
  354. static_assert( is_object<_Tp>::value, "pointer to object type" );
  355. #endif
  356. return _M_b++;
  357. }
  358. __pointer_type
  359. operator++(int) volatile noexcept
  360. {
  361. #if __cplusplus >= 201703L
  362. static_assert( is_object<_Tp>::value, "pointer to object type" );
  363. #endif
  364. return _M_b++;
  365. }
  366. __pointer_type
  367. operator--(int) noexcept
  368. {
  369. #if __cplusplus >= 201703L
  370. static_assert( is_object<_Tp>::value, "pointer to object type" );
  371. #endif
  372. return _M_b--;
  373. }
  374. __pointer_type
  375. operator--(int) volatile noexcept
  376. {
  377. #if __cplusplus >= 201703L
  378. static_assert( is_object<_Tp>::value, "pointer to object type" );
  379. #endif
  380. return _M_b--;
  381. }
  382. __pointer_type
  383. operator++() noexcept
  384. {
  385. #if __cplusplus >= 201703L
  386. static_assert( is_object<_Tp>::value, "pointer to object type" );
  387. #endif
  388. return ++_M_b;
  389. }
  390. __pointer_type
  391. operator++() volatile noexcept
  392. {
  393. #if __cplusplus >= 201703L
  394. static_assert( is_object<_Tp>::value, "pointer to object type" );
  395. #endif
  396. return ++_M_b;
  397. }
  398. __pointer_type
  399. operator--() noexcept
  400. {
  401. #if __cplusplus >= 201703L
  402. static_assert( is_object<_Tp>::value, "pointer to object type" );
  403. #endif
  404. return --_M_b;
  405. }
  406. __pointer_type
  407. operator--() volatile noexcept
  408. {
  409. #if __cplusplus >= 201703L
  410. static_assert( is_object<_Tp>::value, "pointer to object type" );
  411. #endif
  412. return --_M_b;
  413. }
  414. __pointer_type
  415. operator+=(ptrdiff_t __d) noexcept
  416. {
  417. #if __cplusplus >= 201703L
  418. static_assert( is_object<_Tp>::value, "pointer to object type" );
  419. #endif
  420. return _M_b.operator+=(__d);
  421. }
  422. __pointer_type
  423. operator+=(ptrdiff_t __d) volatile noexcept
  424. {
  425. #if __cplusplus >= 201703L
  426. static_assert( is_object<_Tp>::value, "pointer to object type" );
  427. #endif
  428. return _M_b.operator+=(__d);
  429. }
  430. __pointer_type
  431. operator-=(ptrdiff_t __d) noexcept
  432. {
  433. #if __cplusplus >= 201703L
  434. static_assert( is_object<_Tp>::value, "pointer to object type" );
  435. #endif
  436. return _M_b.operator-=(__d);
  437. }
  438. __pointer_type
  439. operator-=(ptrdiff_t __d) volatile noexcept
  440. {
  441. #if __cplusplus >= 201703L
  442. static_assert( is_object<_Tp>::value, "pointer to object type" );
  443. #endif
  444. return _M_b.operator-=(__d);
  445. }
  446. bool
  447. is_lock_free() const noexcept
  448. { return _M_b.is_lock_free(); }
  449. bool
  450. is_lock_free() const volatile noexcept
  451. { return _M_b.is_lock_free(); }
  452. #if __cplusplus >= 201703L
  453. static constexpr bool is_always_lock_free = ATOMIC_POINTER_LOCK_FREE == 2;
  454. #endif
  455. void
  456. store(__pointer_type __p,
  457. memory_order __m = memory_order_seq_cst) noexcept
  458. { return _M_b.store(__p, __m); }
  459. void
  460. store(__pointer_type __p,
  461. memory_order __m = memory_order_seq_cst) volatile noexcept
  462. { return _M_b.store(__p, __m); }
  463. __pointer_type
  464. load(memory_order __m = memory_order_seq_cst) const noexcept
  465. { return _M_b.load(__m); }
  466. __pointer_type
  467. load(memory_order __m = memory_order_seq_cst) const volatile noexcept
  468. { return _M_b.load(__m); }
  469. __pointer_type
  470. exchange(__pointer_type __p,
  471. memory_order __m = memory_order_seq_cst) noexcept
  472. { return _M_b.exchange(__p, __m); }
  473. __pointer_type
  474. exchange(__pointer_type __p,
  475. memory_order __m = memory_order_seq_cst) volatile noexcept
  476. { return _M_b.exchange(__p, __m); }
  477. bool
  478. compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
  479. memory_order __m1, memory_order __m2) noexcept
  480. { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
  481. bool
  482. compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
  483. memory_order __m1,
  484. memory_order __m2) volatile noexcept
  485. { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
  486. bool
  487. compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
  488. memory_order __m = memory_order_seq_cst) noexcept
  489. {
  490. return compare_exchange_weak(__p1, __p2, __m,
  491. __cmpexch_failure_order(__m));
  492. }
  493. bool
  494. compare_exchange_weak(__pointer_type& __p1, __pointer_type __p2,
  495. memory_order __m = memory_order_seq_cst) volatile noexcept
  496. {
  497. return compare_exchange_weak(__p1, __p2, __m,
  498. __cmpexch_failure_order(__m));
  499. }
  500. bool
  501. compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
  502. memory_order __m1, memory_order __m2) noexcept
  503. { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
  504. bool
  505. compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
  506. memory_order __m1,
  507. memory_order __m2) volatile noexcept
  508. { return _M_b.compare_exchange_strong(__p1, __p2, __m1, __m2); }
  509. bool
  510. compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
  511. memory_order __m = memory_order_seq_cst) noexcept
  512. {
  513. return _M_b.compare_exchange_strong(__p1, __p2, __m,
  514. __cmpexch_failure_order(__m));
  515. }
  516. bool
  517. compare_exchange_strong(__pointer_type& __p1, __pointer_type __p2,
  518. memory_order __m = memory_order_seq_cst) volatile noexcept
  519. {
  520. return _M_b.compare_exchange_strong(__p1, __p2, __m,
  521. __cmpexch_failure_order(__m));
  522. }
  523. #if __cpp_lib_atomic_wait
  524. void
  525. wait(__pointer_type __old, memory_order __m = memory_order_seq_cst) noexcept
  526. { _M_b.wait(__old, __m); }
  527. // TODO add const volatile overload
  528. void
  529. notify_one() const noexcept
  530. { _M_b.notify_one(); }
  531. void
  532. notify_all() const noexcept
  533. { _M_b.notify_all(); }
  534. #endif // __cpp_lib_atomic_wait
  535. __pointer_type
  536. fetch_add(ptrdiff_t __d,
  537. memory_order __m = memory_order_seq_cst) noexcept
  538. {
  539. #if __cplusplus >= 201703L
  540. static_assert( is_object<_Tp>::value, "pointer to object type" );
  541. #endif
  542. return _M_b.fetch_add(__d, __m);
  543. }
  544. __pointer_type
  545. fetch_add(ptrdiff_t __d,
  546. memory_order __m = memory_order_seq_cst) volatile noexcept
  547. {
  548. #if __cplusplus >= 201703L
  549. static_assert( is_object<_Tp>::value, "pointer to object type" );
  550. #endif
  551. return _M_b.fetch_add(__d, __m);
  552. }
  553. __pointer_type
  554. fetch_sub(ptrdiff_t __d,
  555. memory_order __m = memory_order_seq_cst) noexcept
  556. {
  557. #if __cplusplus >= 201703L
  558. static_assert( is_object<_Tp>::value, "pointer to object type" );
  559. #endif
  560. return _M_b.fetch_sub(__d, __m);
  561. }
  562. __pointer_type
  563. fetch_sub(ptrdiff_t __d,
  564. memory_order __m = memory_order_seq_cst) volatile noexcept
  565. {
  566. #if __cplusplus >= 201703L
  567. static_assert( is_object<_Tp>::value, "pointer to object type" );
  568. #endif
  569. return _M_b.fetch_sub(__d, __m);
  570. }
  571. };
  572. /// Explicit specialization for char.
  573. template<>
  574. struct atomic<char> : __atomic_base<char>
  575. {
  576. typedef char __integral_type;
  577. typedef __atomic_base<char> __base_type;
  578. atomic() noexcept = default;
  579. ~atomic() noexcept = default;
  580. atomic(const atomic&) = delete;
  581. atomic& operator=(const atomic&) = delete;
  582. atomic& operator=(const atomic&) volatile = delete;
  583. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  584. using __base_type::operator __integral_type;
  585. using __base_type::operator=;
  586. #if __cplusplus >= 201703L
  587. static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE == 2;
  588. #endif
  589. };
  590. /// Explicit specialization for signed char.
  591. template<>
  592. struct atomic<signed char> : __atomic_base<signed char>
  593. {
  594. typedef signed char __integral_type;
  595. typedef __atomic_base<signed char> __base_type;
  596. atomic() noexcept= default;
  597. ~atomic() noexcept = default;
  598. atomic(const atomic&) = delete;
  599. atomic& operator=(const atomic&) = delete;
  600. atomic& operator=(const atomic&) volatile = delete;
  601. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  602. using __base_type::operator __integral_type;
  603. using __base_type::operator=;
  604. #if __cplusplus >= 201703L
  605. static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE == 2;
  606. #endif
  607. };
  608. /// Explicit specialization for unsigned char.
  609. template<>
  610. struct atomic<unsigned char> : __atomic_base<unsigned char>
  611. {
  612. typedef unsigned char __integral_type;
  613. typedef __atomic_base<unsigned char> __base_type;
  614. atomic() noexcept= default;
  615. ~atomic() noexcept = default;
  616. atomic(const atomic&) = delete;
  617. atomic& operator=(const atomic&) = delete;
  618. atomic& operator=(const atomic&) volatile = delete;
  619. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  620. using __base_type::operator __integral_type;
  621. using __base_type::operator=;
  622. #if __cplusplus >= 201703L
  623. static constexpr bool is_always_lock_free = ATOMIC_CHAR_LOCK_FREE == 2;
  624. #endif
  625. };
  626. /// Explicit specialization for short.
  627. template<>
  628. struct atomic<short> : __atomic_base<short>
  629. {
  630. typedef short __integral_type;
  631. typedef __atomic_base<short> __base_type;
  632. atomic() noexcept = default;
  633. ~atomic() noexcept = default;
  634. atomic(const atomic&) = delete;
  635. atomic& operator=(const atomic&) = delete;
  636. atomic& operator=(const atomic&) volatile = delete;
  637. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  638. using __base_type::operator __integral_type;
  639. using __base_type::operator=;
  640. #if __cplusplus >= 201703L
  641. static constexpr bool is_always_lock_free = ATOMIC_SHORT_LOCK_FREE == 2;
  642. #endif
  643. };
  644. /// Explicit specialization for unsigned short.
  645. template<>
  646. struct atomic<unsigned short> : __atomic_base<unsigned short>
  647. {
  648. typedef unsigned short __integral_type;
  649. typedef __atomic_base<unsigned short> __base_type;
  650. atomic() noexcept = default;
  651. ~atomic() noexcept = default;
  652. atomic(const atomic&) = delete;
  653. atomic& operator=(const atomic&) = delete;
  654. atomic& operator=(const atomic&) volatile = delete;
  655. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  656. using __base_type::operator __integral_type;
  657. using __base_type::operator=;
  658. #if __cplusplus >= 201703L
  659. static constexpr bool is_always_lock_free = ATOMIC_SHORT_LOCK_FREE == 2;
  660. #endif
  661. };
  662. /// Explicit specialization for int.
  663. template<>
  664. struct atomic<int> : __atomic_base<int>
  665. {
  666. typedef int __integral_type;
  667. typedef __atomic_base<int> __base_type;
  668. atomic() noexcept = default;
  669. ~atomic() noexcept = default;
  670. atomic(const atomic&) = delete;
  671. atomic& operator=(const atomic&) = delete;
  672. atomic& operator=(const atomic&) volatile = delete;
  673. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  674. using __base_type::operator __integral_type;
  675. using __base_type::operator=;
  676. #if __cplusplus >= 201703L
  677. static constexpr bool is_always_lock_free = ATOMIC_INT_LOCK_FREE == 2;
  678. #endif
  679. };
  680. /// Explicit specialization for unsigned int.
  681. template<>
  682. struct atomic<unsigned int> : __atomic_base<unsigned int>
  683. {
  684. typedef unsigned int __integral_type;
  685. typedef __atomic_base<unsigned int> __base_type;
  686. atomic() noexcept = default;
  687. ~atomic() noexcept = default;
  688. atomic(const atomic&) = delete;
  689. atomic& operator=(const atomic&) = delete;
  690. atomic& operator=(const atomic&) volatile = delete;
  691. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  692. using __base_type::operator __integral_type;
  693. using __base_type::operator=;
  694. #if __cplusplus >= 201703L
  695. static constexpr bool is_always_lock_free = ATOMIC_INT_LOCK_FREE == 2;
  696. #endif
  697. };
  698. /// Explicit specialization for long.
  699. template<>
  700. struct atomic<long> : __atomic_base<long>
  701. {
  702. typedef long __integral_type;
  703. typedef __atomic_base<long> __base_type;
  704. atomic() noexcept = default;
  705. ~atomic() noexcept = default;
  706. atomic(const atomic&) = delete;
  707. atomic& operator=(const atomic&) = delete;
  708. atomic& operator=(const atomic&) volatile = delete;
  709. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  710. using __base_type::operator __integral_type;
  711. using __base_type::operator=;
  712. #if __cplusplus >= 201703L
  713. static constexpr bool is_always_lock_free = ATOMIC_LONG_LOCK_FREE == 2;
  714. #endif
  715. };
  716. /// Explicit specialization for unsigned long.
  717. template<>
  718. struct atomic<unsigned long> : __atomic_base<unsigned long>
  719. {
  720. typedef unsigned long __integral_type;
  721. typedef __atomic_base<unsigned long> __base_type;
  722. atomic() noexcept = default;
  723. ~atomic() noexcept = default;
  724. atomic(const atomic&) = delete;
  725. atomic& operator=(const atomic&) = delete;
  726. atomic& operator=(const atomic&) volatile = delete;
  727. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  728. using __base_type::operator __integral_type;
  729. using __base_type::operator=;
  730. #if __cplusplus >= 201703L
  731. static constexpr bool is_always_lock_free = ATOMIC_LONG_LOCK_FREE == 2;
  732. #endif
  733. };
  734. /// Explicit specialization for long long.
  735. template<>
  736. struct atomic<long long> : __atomic_base<long long>
  737. {
  738. typedef long long __integral_type;
  739. typedef __atomic_base<long long> __base_type;
  740. atomic() noexcept = default;
  741. ~atomic() noexcept = default;
  742. atomic(const atomic&) = delete;
  743. atomic& operator=(const atomic&) = delete;
  744. atomic& operator=(const atomic&) volatile = delete;
  745. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  746. using __base_type::operator __integral_type;
  747. using __base_type::operator=;
  748. #if __cplusplus >= 201703L
  749. static constexpr bool is_always_lock_free = ATOMIC_LLONG_LOCK_FREE == 2;
  750. #endif
  751. };
  752. /// Explicit specialization for unsigned long long.
  753. template<>
  754. struct atomic<unsigned long long> : __atomic_base<unsigned long long>
  755. {
  756. typedef unsigned long long __integral_type;
  757. typedef __atomic_base<unsigned long long> __base_type;
  758. atomic() noexcept = default;
  759. ~atomic() noexcept = default;
  760. atomic(const atomic&) = delete;
  761. atomic& operator=(const atomic&) = delete;
  762. atomic& operator=(const atomic&) volatile = delete;
  763. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  764. using __base_type::operator __integral_type;
  765. using __base_type::operator=;
  766. #if __cplusplus >= 201703L
  767. static constexpr bool is_always_lock_free = ATOMIC_LLONG_LOCK_FREE == 2;
  768. #endif
  769. };
  770. /// Explicit specialization for wchar_t.
  771. template<>
  772. struct atomic<wchar_t> : __atomic_base<wchar_t>
  773. {
  774. typedef wchar_t __integral_type;
  775. typedef __atomic_base<wchar_t> __base_type;
  776. atomic() noexcept = default;
  777. ~atomic() noexcept = default;
  778. atomic(const atomic&) = delete;
  779. atomic& operator=(const atomic&) = delete;
  780. atomic& operator=(const atomic&) volatile = delete;
  781. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  782. using __base_type::operator __integral_type;
  783. using __base_type::operator=;
  784. #if __cplusplus >= 201703L
  785. static constexpr bool is_always_lock_free = ATOMIC_WCHAR_T_LOCK_FREE == 2;
  786. #endif
  787. };
  788. #ifdef _GLIBCXX_USE_CHAR8_T
  789. /// Explicit specialization for char8_t.
  790. template<>
  791. struct atomic<char8_t> : __atomic_base<char8_t>
  792. {
  793. typedef char8_t __integral_type;
  794. typedef __atomic_base<char8_t> __base_type;
  795. atomic() noexcept = default;
  796. ~atomic() noexcept = default;
  797. atomic(const atomic&) = delete;
  798. atomic& operator=(const atomic&) = delete;
  799. atomic& operator=(const atomic&) volatile = delete;
  800. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  801. using __base_type::operator __integral_type;
  802. using __base_type::operator=;
  803. #if __cplusplus > 201402L
  804. static constexpr bool is_always_lock_free = ATOMIC_CHAR8_T_LOCK_FREE == 2;
  805. #endif
  806. };
  807. #endif
  808. /// Explicit specialization for char16_t.
  809. template<>
  810. struct atomic<char16_t> : __atomic_base<char16_t>
  811. {
  812. typedef char16_t __integral_type;
  813. typedef __atomic_base<char16_t> __base_type;
  814. atomic() noexcept = default;
  815. ~atomic() noexcept = default;
  816. atomic(const atomic&) = delete;
  817. atomic& operator=(const atomic&) = delete;
  818. atomic& operator=(const atomic&) volatile = delete;
  819. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  820. using __base_type::operator __integral_type;
  821. using __base_type::operator=;
  822. #if __cplusplus >= 201703L
  823. static constexpr bool is_always_lock_free = ATOMIC_CHAR16_T_LOCK_FREE == 2;
  824. #endif
  825. };
  826. /// Explicit specialization for char32_t.
  827. template<>
  828. struct atomic<char32_t> : __atomic_base<char32_t>
  829. {
  830. typedef char32_t __integral_type;
  831. typedef __atomic_base<char32_t> __base_type;
  832. atomic() noexcept = default;
  833. ~atomic() noexcept = default;
  834. atomic(const atomic&) = delete;
  835. atomic& operator=(const atomic&) = delete;
  836. atomic& operator=(const atomic&) volatile = delete;
  837. constexpr atomic(__integral_type __i) noexcept : __base_type(__i) { }
  838. using __base_type::operator __integral_type;
  839. using __base_type::operator=;
  840. #if __cplusplus >= 201703L
  841. static constexpr bool is_always_lock_free = ATOMIC_CHAR32_T_LOCK_FREE == 2;
  842. #endif
  843. };
  844. /// atomic_bool
  845. typedef atomic<bool> atomic_bool;
  846. /// atomic_char
  847. typedef atomic<char> atomic_char;
  848. /// atomic_schar
  849. typedef atomic<signed char> atomic_schar;
  850. /// atomic_uchar
  851. typedef atomic<unsigned char> atomic_uchar;
  852. /// atomic_short
  853. typedef atomic<short> atomic_short;
  854. /// atomic_ushort
  855. typedef atomic<unsigned short> atomic_ushort;
  856. /// atomic_int
  857. typedef atomic<int> atomic_int;
  858. /// atomic_uint
  859. typedef atomic<unsigned int> atomic_uint;
  860. /// atomic_long
  861. typedef atomic<long> atomic_long;
  862. /// atomic_ulong
  863. typedef atomic<unsigned long> atomic_ulong;
  864. /// atomic_llong
  865. typedef atomic<long long> atomic_llong;
  866. /// atomic_ullong
  867. typedef atomic<unsigned long long> atomic_ullong;
  868. /// atomic_wchar_t
  869. typedef atomic<wchar_t> atomic_wchar_t;
  870. #ifdef _GLIBCXX_USE_CHAR8_T
  871. /// atomic_char8_t
  872. typedef atomic<char8_t> atomic_char8_t;
  873. #endif
  874. /// atomic_char16_t
  875. typedef atomic<char16_t> atomic_char16_t;
  876. /// atomic_char32_t
  877. typedef atomic<char32_t> atomic_char32_t;
  878. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  879. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  880. // 2441. Exact-width atomic typedefs should be provided
  881. /// atomic_int8_t
  882. typedef atomic<int8_t> atomic_int8_t;
  883. /// atomic_uint8_t
  884. typedef atomic<uint8_t> atomic_uint8_t;
  885. /// atomic_int16_t
  886. typedef atomic<int16_t> atomic_int16_t;
  887. /// atomic_uint16_t
  888. typedef atomic<uint16_t> atomic_uint16_t;
  889. /// atomic_int32_t
  890. typedef atomic<int32_t> atomic_int32_t;
  891. /// atomic_uint32_t
  892. typedef atomic<uint32_t> atomic_uint32_t;
  893. /// atomic_int64_t
  894. typedef atomic<int64_t> atomic_int64_t;
  895. /// atomic_uint64_t
  896. typedef atomic<uint64_t> atomic_uint64_t;
  897. /// atomic_int_least8_t
  898. typedef atomic<int_least8_t> atomic_int_least8_t;
  899. /// atomic_uint_least8_t
  900. typedef atomic<uint_least8_t> atomic_uint_least8_t;
  901. /// atomic_int_least16_t
  902. typedef atomic<int_least16_t> atomic_int_least16_t;
  903. /// atomic_uint_least16_t
  904. typedef atomic<uint_least16_t> atomic_uint_least16_t;
  905. /// atomic_int_least32_t
  906. typedef atomic<int_least32_t> atomic_int_least32_t;
  907. /// atomic_uint_least32_t
  908. typedef atomic<uint_least32_t> atomic_uint_least32_t;
  909. /// atomic_int_least64_t
  910. typedef atomic<int_least64_t> atomic_int_least64_t;
  911. /// atomic_uint_least64_t
  912. typedef atomic<uint_least64_t> atomic_uint_least64_t;
  913. /// atomic_int_fast8_t
  914. typedef atomic<int_fast8_t> atomic_int_fast8_t;
  915. /// atomic_uint_fast8_t
  916. typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
  917. /// atomic_int_fast16_t
  918. typedef atomic<int_fast16_t> atomic_int_fast16_t;
  919. /// atomic_uint_fast16_t
  920. typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
  921. /// atomic_int_fast32_t
  922. typedef atomic<int_fast32_t> atomic_int_fast32_t;
  923. /// atomic_uint_fast32_t
  924. typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
  925. /// atomic_int_fast64_t
  926. typedef atomic<int_fast64_t> atomic_int_fast64_t;
  927. /// atomic_uint_fast64_t
  928. typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
  929. #endif
  930. /// atomic_intptr_t
  931. typedef atomic<intptr_t> atomic_intptr_t;
  932. /// atomic_uintptr_t
  933. typedef atomic<uintptr_t> atomic_uintptr_t;
  934. /// atomic_size_t
  935. typedef atomic<size_t> atomic_size_t;
  936. /// atomic_ptrdiff_t
  937. typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
  938. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  939. /// atomic_intmax_t
  940. typedef atomic<intmax_t> atomic_intmax_t;
  941. /// atomic_uintmax_t
  942. typedef atomic<uintmax_t> atomic_uintmax_t;
  943. #endif
  944. // Function definitions, atomic_flag operations.
  945. inline bool
  946. atomic_flag_test_and_set_explicit(atomic_flag* __a,
  947. memory_order __m) noexcept
  948. { return __a->test_and_set(__m); }
  949. inline bool
  950. atomic_flag_test_and_set_explicit(volatile atomic_flag* __a,
  951. memory_order __m) noexcept
  952. { return __a->test_and_set(__m); }
  953. inline void
  954. atomic_flag_clear_explicit(atomic_flag* __a, memory_order __m) noexcept
  955. { __a->clear(__m); }
  956. inline void
  957. atomic_flag_clear_explicit(volatile atomic_flag* __a,
  958. memory_order __m) noexcept
  959. { __a->clear(__m); }
  960. inline bool
  961. atomic_flag_test_and_set(atomic_flag* __a) noexcept
  962. { return atomic_flag_test_and_set_explicit(__a, memory_order_seq_cst); }
  963. inline bool
  964. atomic_flag_test_and_set(volatile atomic_flag* __a) noexcept
  965. { return atomic_flag_test_and_set_explicit(__a, memory_order_seq_cst); }
  966. inline void
  967. atomic_flag_clear(atomic_flag* __a) noexcept
  968. { atomic_flag_clear_explicit(__a, memory_order_seq_cst); }
  969. inline void
  970. atomic_flag_clear(volatile atomic_flag* __a) noexcept
  971. { atomic_flag_clear_explicit(__a, memory_order_seq_cst); }
  972. template<typename _Tp>
  973. using __atomic_val_t = typename atomic<_Tp>::value_type;
  974. template<typename _Tp>
  975. using __atomic_diff_t = typename atomic<_Tp>::difference_type;
  976. // [atomics.nonmembers] Non-member functions.
  977. // Function templates generally applicable to atomic types.
  978. template<typename _ITp>
  979. inline bool
  980. atomic_is_lock_free(const atomic<_ITp>* __a) noexcept
  981. { return __a->is_lock_free(); }
  982. template<typename _ITp>
  983. inline bool
  984. atomic_is_lock_free(const volatile atomic<_ITp>* __a) noexcept
  985. { return __a->is_lock_free(); }
  986. template<typename _ITp>
  987. inline void
  988. atomic_init(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
  989. { __a->store(__i, memory_order_relaxed); }
  990. template<typename _ITp>
  991. inline void
  992. atomic_init(volatile atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
  993. { __a->store(__i, memory_order_relaxed); }
  994. template<typename _ITp>
  995. inline void
  996. atomic_store_explicit(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i,
  997. memory_order __m) noexcept
  998. { __a->store(__i, __m); }
  999. template<typename _ITp>
  1000. inline void
  1001. atomic_store_explicit(volatile atomic<_ITp>* __a, __atomic_val_t<_ITp> __i,
  1002. memory_order __m) noexcept
  1003. { __a->store(__i, __m); }
  1004. template<typename _ITp>
  1005. inline _ITp
  1006. atomic_load_explicit(const atomic<_ITp>* __a, memory_order __m) noexcept
  1007. { return __a->load(__m); }
  1008. template<typename _ITp>
  1009. inline _ITp
  1010. atomic_load_explicit(const volatile atomic<_ITp>* __a,
  1011. memory_order __m) noexcept
  1012. { return __a->load(__m); }
  1013. template<typename _ITp>
  1014. inline _ITp
  1015. atomic_exchange_explicit(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i,
  1016. memory_order __m) noexcept
  1017. { return __a->exchange(__i, __m); }
  1018. template<typename _ITp>
  1019. inline _ITp
  1020. atomic_exchange_explicit(volatile atomic<_ITp>* __a,
  1021. __atomic_val_t<_ITp> __i,
  1022. memory_order __m) noexcept
  1023. { return __a->exchange(__i, __m); }
  1024. template<typename _ITp>
  1025. inline bool
  1026. atomic_compare_exchange_weak_explicit(atomic<_ITp>* __a,
  1027. __atomic_val_t<_ITp>* __i1,
  1028. __atomic_val_t<_ITp> __i2,
  1029. memory_order __m1,
  1030. memory_order __m2) noexcept
  1031. { return __a->compare_exchange_weak(*__i1, __i2, __m1, __m2); }
  1032. template<typename _ITp>
  1033. inline bool
  1034. atomic_compare_exchange_weak_explicit(volatile atomic<_ITp>* __a,
  1035. __atomic_val_t<_ITp>* __i1,
  1036. __atomic_val_t<_ITp> __i2,
  1037. memory_order __m1,
  1038. memory_order __m2) noexcept
  1039. { return __a->compare_exchange_weak(*__i1, __i2, __m1, __m2); }
  1040. template<typename _ITp>
  1041. inline bool
  1042. atomic_compare_exchange_strong_explicit(atomic<_ITp>* __a,
  1043. __atomic_val_t<_ITp>* __i1,
  1044. __atomic_val_t<_ITp> __i2,
  1045. memory_order __m1,
  1046. memory_order __m2) noexcept
  1047. { return __a->compare_exchange_strong(*__i1, __i2, __m1, __m2); }
  1048. template<typename _ITp>
  1049. inline bool
  1050. atomic_compare_exchange_strong_explicit(volatile atomic<_ITp>* __a,
  1051. __atomic_val_t<_ITp>* __i1,
  1052. __atomic_val_t<_ITp> __i2,
  1053. memory_order __m1,
  1054. memory_order __m2) noexcept
  1055. { return __a->compare_exchange_strong(*__i1, __i2, __m1, __m2); }
  1056. template<typename _ITp>
  1057. inline void
  1058. atomic_store(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
  1059. { atomic_store_explicit(__a, __i, memory_order_seq_cst); }
  1060. template<typename _ITp>
  1061. inline void
  1062. atomic_store(volatile atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
  1063. { atomic_store_explicit(__a, __i, memory_order_seq_cst); }
  1064. template<typename _ITp>
  1065. inline _ITp
  1066. atomic_load(const atomic<_ITp>* __a) noexcept
  1067. { return atomic_load_explicit(__a, memory_order_seq_cst); }
  1068. template<typename _ITp>
  1069. inline _ITp
  1070. atomic_load(const volatile atomic<_ITp>* __a) noexcept
  1071. { return atomic_load_explicit(__a, memory_order_seq_cst); }
  1072. template<typename _ITp>
  1073. inline _ITp
  1074. atomic_exchange(atomic<_ITp>* __a, __atomic_val_t<_ITp> __i) noexcept
  1075. { return atomic_exchange_explicit(__a, __i, memory_order_seq_cst); }
  1076. template<typename _ITp>
  1077. inline _ITp
  1078. atomic_exchange(volatile atomic<_ITp>* __a,
  1079. __atomic_val_t<_ITp> __i) noexcept
  1080. { return atomic_exchange_explicit(__a, __i, memory_order_seq_cst); }
  1081. template<typename _ITp>
  1082. inline bool
  1083. atomic_compare_exchange_weak(atomic<_ITp>* __a,
  1084. __atomic_val_t<_ITp>* __i1,
  1085. __atomic_val_t<_ITp> __i2) noexcept
  1086. {
  1087. return atomic_compare_exchange_weak_explicit(__a, __i1, __i2,
  1088. memory_order_seq_cst,
  1089. memory_order_seq_cst);
  1090. }
  1091. template<typename _ITp>
  1092. inline bool
  1093. atomic_compare_exchange_weak(volatile atomic<_ITp>* __a,
  1094. __atomic_val_t<_ITp>* __i1,
  1095. __atomic_val_t<_ITp> __i2) noexcept
  1096. {
  1097. return atomic_compare_exchange_weak_explicit(__a, __i1, __i2,
  1098. memory_order_seq_cst,
  1099. memory_order_seq_cst);
  1100. }
  1101. template<typename _ITp>
  1102. inline bool
  1103. atomic_compare_exchange_strong(atomic<_ITp>* __a,
  1104. __atomic_val_t<_ITp>* __i1,
  1105. __atomic_val_t<_ITp> __i2) noexcept
  1106. {
  1107. return atomic_compare_exchange_strong_explicit(__a, __i1, __i2,
  1108. memory_order_seq_cst,
  1109. memory_order_seq_cst);
  1110. }
  1111. template<typename _ITp>
  1112. inline bool
  1113. atomic_compare_exchange_strong(volatile atomic<_ITp>* __a,
  1114. __atomic_val_t<_ITp>* __i1,
  1115. __atomic_val_t<_ITp> __i2) noexcept
  1116. {
  1117. return atomic_compare_exchange_strong_explicit(__a, __i1, __i2,
  1118. memory_order_seq_cst,
  1119. memory_order_seq_cst);
  1120. }
  1121. #if __cpp_lib_atomic_wait
  1122. template<typename _Tp>
  1123. inline void
  1124. atomic_wait(const atomic<_Tp>* __a,
  1125. typename std::atomic<_Tp>::value_type __old) noexcept
  1126. { __a->wait(__old); }
  1127. template<typename _Tp>
  1128. inline void
  1129. atomic_wait_explicit(const atomic<_Tp>* __a,
  1130. typename std::atomic<_Tp>::value_type __old,
  1131. std::memory_order __m) noexcept
  1132. { __a->wait(__old, __m); }
  1133. template<typename _Tp>
  1134. inline void
  1135. atomic_notify_one(atomic<_Tp>* __a) noexcept
  1136. { __a->notify_one(); }
  1137. template<typename _Tp>
  1138. inline void
  1139. atomic_notify_all(atomic<_Tp>* __a) noexcept
  1140. { __a->notify_all(); }
  1141. #endif // __cpp_lib_atomic_wait
  1142. // Function templates for atomic_integral and atomic_pointer operations only.
  1143. // Some operations (and, or, xor) are only available for atomic integrals,
  1144. // which is implemented by taking a parameter of type __atomic_base<_ITp>*.
  1145. template<typename _ITp>
  1146. inline _ITp
  1147. atomic_fetch_add_explicit(atomic<_ITp>* __a,
  1148. __atomic_diff_t<_ITp> __i,
  1149. memory_order __m) noexcept
  1150. { return __a->fetch_add(__i, __m); }
  1151. template<typename _ITp>
  1152. inline _ITp
  1153. atomic_fetch_add_explicit(volatile atomic<_ITp>* __a,
  1154. __atomic_diff_t<_ITp> __i,
  1155. memory_order __m) noexcept
  1156. { return __a->fetch_add(__i, __m); }
  1157. template<typename _ITp>
  1158. inline _ITp
  1159. atomic_fetch_sub_explicit(atomic<_ITp>* __a,
  1160. __atomic_diff_t<_ITp> __i,
  1161. memory_order __m) noexcept
  1162. { return __a->fetch_sub(__i, __m); }
  1163. template<typename _ITp>
  1164. inline _ITp
  1165. atomic_fetch_sub_explicit(volatile atomic<_ITp>* __a,
  1166. __atomic_diff_t<_ITp> __i,
  1167. memory_order __m) noexcept
  1168. { return __a->fetch_sub(__i, __m); }
  1169. template<typename _ITp>
  1170. inline _ITp
  1171. atomic_fetch_and_explicit(__atomic_base<_ITp>* __a,
  1172. __atomic_val_t<_ITp> __i,
  1173. memory_order __m) noexcept
  1174. { return __a->fetch_and(__i, __m); }
  1175. template<typename _ITp>
  1176. inline _ITp
  1177. atomic_fetch_and_explicit(volatile __atomic_base<_ITp>* __a,
  1178. __atomic_val_t<_ITp> __i,
  1179. memory_order __m) noexcept
  1180. { return __a->fetch_and(__i, __m); }
  1181. template<typename _ITp>
  1182. inline _ITp
  1183. atomic_fetch_or_explicit(__atomic_base<_ITp>* __a,
  1184. __atomic_val_t<_ITp> __i,
  1185. memory_order __m) noexcept
  1186. { return __a->fetch_or(__i, __m); }
  1187. template<typename _ITp>
  1188. inline _ITp
  1189. atomic_fetch_or_explicit(volatile __atomic_base<_ITp>* __a,
  1190. __atomic_val_t<_ITp> __i,
  1191. memory_order __m) noexcept
  1192. { return __a->fetch_or(__i, __m); }
  1193. template<typename _ITp>
  1194. inline _ITp
  1195. atomic_fetch_xor_explicit(__atomic_base<_ITp>* __a,
  1196. __atomic_val_t<_ITp> __i,
  1197. memory_order __m) noexcept
  1198. { return __a->fetch_xor(__i, __m); }
  1199. template<typename _ITp>
  1200. inline _ITp
  1201. atomic_fetch_xor_explicit(volatile __atomic_base<_ITp>* __a,
  1202. __atomic_val_t<_ITp> __i,
  1203. memory_order __m) noexcept
  1204. { return __a->fetch_xor(__i, __m); }
  1205. template<typename _ITp>
  1206. inline _ITp
  1207. atomic_fetch_add(atomic<_ITp>* __a,
  1208. __atomic_diff_t<_ITp> __i) noexcept
  1209. { return atomic_fetch_add_explicit(__a, __i, memory_order_seq_cst); }
  1210. template<typename _ITp>
  1211. inline _ITp
  1212. atomic_fetch_add(volatile atomic<_ITp>* __a,
  1213. __atomic_diff_t<_ITp> __i) noexcept
  1214. { return atomic_fetch_add_explicit(__a, __i, memory_order_seq_cst); }
  1215. template<typename _ITp>
  1216. inline _ITp
  1217. atomic_fetch_sub(atomic<_ITp>* __a,
  1218. __atomic_diff_t<_ITp> __i) noexcept
  1219. { return atomic_fetch_sub_explicit(__a, __i, memory_order_seq_cst); }
  1220. template<typename _ITp>
  1221. inline _ITp
  1222. atomic_fetch_sub(volatile atomic<_ITp>* __a,
  1223. __atomic_diff_t<_ITp> __i) noexcept
  1224. { return atomic_fetch_sub_explicit(__a, __i, memory_order_seq_cst); }
  1225. template<typename _ITp>
  1226. inline _ITp
  1227. atomic_fetch_and(__atomic_base<_ITp>* __a,
  1228. __atomic_val_t<_ITp> __i) noexcept
  1229. { return atomic_fetch_and_explicit(__a, __i, memory_order_seq_cst); }
  1230. template<typename _ITp>
  1231. inline _ITp
  1232. atomic_fetch_and(volatile __atomic_base<_ITp>* __a,
  1233. __atomic_val_t<_ITp> __i) noexcept
  1234. { return atomic_fetch_and_explicit(__a, __i, memory_order_seq_cst); }
  1235. template<typename _ITp>
  1236. inline _ITp
  1237. atomic_fetch_or(__atomic_base<_ITp>* __a,
  1238. __atomic_val_t<_ITp> __i) noexcept
  1239. { return atomic_fetch_or_explicit(__a, __i, memory_order_seq_cst); }
  1240. template<typename _ITp>
  1241. inline _ITp
  1242. atomic_fetch_or(volatile __atomic_base<_ITp>* __a,
  1243. __atomic_val_t<_ITp> __i) noexcept
  1244. { return atomic_fetch_or_explicit(__a, __i, memory_order_seq_cst); }
  1245. template<typename _ITp>
  1246. inline _ITp
  1247. atomic_fetch_xor(__atomic_base<_ITp>* __a,
  1248. __atomic_val_t<_ITp> __i) noexcept
  1249. { return atomic_fetch_xor_explicit(__a, __i, memory_order_seq_cst); }
  1250. template<typename _ITp>
  1251. inline _ITp
  1252. atomic_fetch_xor(volatile __atomic_base<_ITp>* __a,
  1253. __atomic_val_t<_ITp> __i) noexcept
  1254. { return atomic_fetch_xor_explicit(__a, __i, memory_order_seq_cst); }
  1255. #if __cplusplus > 201703L
  1256. #define __cpp_lib_atomic_float 201711L
  1257. template<>
  1258. struct atomic<float> : __atomic_float<float>
  1259. {
  1260. atomic() noexcept = default;
  1261. constexpr
  1262. atomic(float __fp) noexcept : __atomic_float<float>(__fp)
  1263. { }
  1264. atomic& operator=(const atomic&) volatile = delete;
  1265. atomic& operator=(const atomic&) = delete;
  1266. using __atomic_float<float>::operator=;
  1267. };
  1268. template<>
  1269. struct atomic<double> : __atomic_float<double>
  1270. {
  1271. atomic() noexcept = default;
  1272. constexpr
  1273. atomic(double __fp) noexcept : __atomic_float<double>(__fp)
  1274. { }
  1275. atomic& operator=(const atomic&) volatile = delete;
  1276. atomic& operator=(const atomic&) = delete;
  1277. using __atomic_float<double>::operator=;
  1278. };
  1279. template<>
  1280. struct atomic<long double> : __atomic_float<long double>
  1281. {
  1282. atomic() noexcept = default;
  1283. constexpr
  1284. atomic(long double __fp) noexcept : __atomic_float<long double>(__fp)
  1285. { }
  1286. atomic& operator=(const atomic&) volatile = delete;
  1287. atomic& operator=(const atomic&) = delete;
  1288. using __atomic_float<long double>::operator=;
  1289. };
  1290. #define __cpp_lib_atomic_ref 201806L
  1291. /// Class template to provide atomic operations on a non-atomic variable.
  1292. template<typename _Tp>
  1293. struct atomic_ref : __atomic_ref<_Tp>
  1294. {
  1295. explicit
  1296. atomic_ref(_Tp& __t) noexcept : __atomic_ref<_Tp>(__t)
  1297. { }
  1298. atomic_ref& operator=(const atomic_ref&) = delete;
  1299. atomic_ref(const atomic_ref&) = default;
  1300. using __atomic_ref<_Tp>::operator=;
  1301. };
  1302. #endif // C++2a
  1303. /// @} group atomics
  1304. _GLIBCXX_END_NAMESPACE_VERSION
  1305. } // namespace
  1306. #endif // C++11
  1307. #endif // _GLIBCXX_ATOMIC