atomic.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "internal/_deprecated_header_message_guard.h"
  14. #if defined(__TBB_show_deprecation_message_atomic_H) && defined(__TBB_show_deprecated_header_message)
  15. #define __TBB_show_deprecation_message_atomic_H
  16. #pragma message("TBB Warning: tbb/atomic.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
  17. #endif
  18. #if defined(__TBB_show_deprecated_header_message)
  19. #undef __TBB_show_deprecated_header_message
  20. #endif
  21. #ifndef __TBB_atomic_H
  22. #define __TBB_atomic_H
  23. #define __TBB_atomic_H_include_area
  24. #include "internal/_warning_suppress_enable_notice.h"
  25. #include <cstddef>
  26. #if _MSC_VER
  27. #define __TBB_LONG_LONG __int64
  28. #else
  29. #define __TBB_LONG_LONG long long
  30. #endif /* _MSC_VER */
  31. #include "tbb_machine.h"
  32. #if _MSC_VER && !__INTEL_COMPILER
  33. // Suppress overzealous compiler warnings till the end of the file
  34. #pragma warning (push)
  35. #pragma warning (disable: 4244 4267 4512)
  36. #endif
  37. namespace tbb {
  38. //! Specifies memory semantics.
  39. enum memory_semantics {
  40. //! Sequential consistency
  41. full_fence,
  42. //! Acquire
  43. acquire,
  44. //! Release
  45. release,
  46. //! No ordering
  47. relaxed
  48. };
  49. //! @cond INTERNAL
  50. namespace internal {
  51. #if __TBB_ALIGNAS_PRESENT
  52. #define __TBB_DECL_ATOMIC_FIELD(t,f,a) alignas(a) t f;
  53. #elif __TBB_ATTRIBUTE_ALIGNED_PRESENT
  54. #define __TBB_DECL_ATOMIC_FIELD(t,f,a) t f __attribute__ ((aligned(a)));
  55. #elif __TBB_DECLSPEC_ALIGN_PRESENT
  56. #define __TBB_DECL_ATOMIC_FIELD(t,f,a) __declspec(align(a)) t f;
  57. #else
  58. #error Do not know syntax for forcing alignment.
  59. #endif
  60. template<size_t S>
  61. struct atomic_rep; // Primary template declared, but never defined.
  62. template<>
  63. struct atomic_rep<1> { // Specialization
  64. typedef int8_t word;
  65. };
  66. template<>
  67. struct atomic_rep<2> { // Specialization
  68. typedef int16_t word;
  69. };
  70. template<>
  71. struct atomic_rep<4> { // Specialization
  72. #if _MSC_VER && !_WIN64
  73. // Work-around that avoids spurious /Wp64 warnings
  74. typedef intptr_t word;
  75. #else
  76. typedef int32_t word;
  77. #endif
  78. };
  79. #if __TBB_64BIT_ATOMICS
  80. template<>
  81. struct atomic_rep<8> { // Specialization
  82. typedef int64_t word;
  83. };
  84. #endif
  85. template<typename value_type, size_t size>
  86. struct aligned_storage;
  87. //the specializations are needed to please MSVC syntax of __declspec(align()) which accept _literal_ constants only
  88. #if __TBB_ATOMIC_CTORS
  89. #define ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(S) \
  90. template<typename value_type> \
  91. struct aligned_storage<value_type,S> { \
  92. __TBB_DECL_ATOMIC_FIELD(value_type,my_value,S) \
  93. aligned_storage() = default ; \
  94. constexpr aligned_storage(value_type value):my_value(value){} \
  95. }; \
  96. #else
  97. #define ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(S) \
  98. template<typename value_type> \
  99. struct aligned_storage<value_type,S> { \
  100. __TBB_DECL_ATOMIC_FIELD(value_type,my_value,S) \
  101. }; \
  102. #endif
  103. template<typename value_type>
  104. struct aligned_storage<value_type,1> {
  105. value_type my_value;
  106. #if __TBB_ATOMIC_CTORS
  107. aligned_storage() = default ;
  108. constexpr aligned_storage(value_type value):my_value(value){}
  109. #endif
  110. };
  111. ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(2)
  112. ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(4)
  113. #if __TBB_64BIT_ATOMICS
  114. ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(8)
  115. #endif
  116. template<size_t Size, memory_semantics M>
  117. struct atomic_traits; // Primary template declared, but not defined.
  118. #define __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(S,M) \
  119. template<> struct atomic_traits<S,M> { \
  120. typedef atomic_rep<S>::word word; \
  121. inline static word compare_and_swap( volatile void* location, word new_value, word comparand ) { \
  122. return __TBB_machine_cmpswp##S##M(location,new_value,comparand); \
  123. } \
  124. inline static word fetch_and_add( volatile void* location, word addend ) { \
  125. return __TBB_machine_fetchadd##S##M(location,addend); \
  126. } \
  127. inline static word fetch_and_store( volatile void* location, word value ) { \
  128. return __TBB_machine_fetchstore##S##M(location,value); \
  129. } \
  130. };
  131. #define __TBB_DECL_ATOMIC_PRIMITIVES(S) \
  132. template<memory_semantics M> \
  133. struct atomic_traits<S,M> { \
  134. typedef atomic_rep<S>::word word; \
  135. inline static word compare_and_swap( volatile void* location, word new_value, word comparand ) { \
  136. return __TBB_machine_cmpswp##S(location,new_value,comparand); \
  137. } \
  138. inline static word fetch_and_add( volatile void* location, word addend ) { \
  139. return __TBB_machine_fetchadd##S(location,addend); \
  140. } \
  141. inline static word fetch_and_store( volatile void* location, word value ) { \
  142. return __TBB_machine_fetchstore##S(location,value); \
  143. } \
  144. };
  145. template<memory_semantics M>
  146. struct atomic_load_store_traits; // Primary template declaration
  147. #define __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(M) \
  148. template<> struct atomic_load_store_traits<M> { \
  149. template <typename T> \
  150. inline static T load( const volatile T& location ) { \
  151. return __TBB_load_##M( location ); \
  152. } \
  153. template <typename T> \
  154. inline static void store( volatile T& location, T value ) { \
  155. __TBB_store_##M( location, value ); \
  156. } \
  157. }
  158. #if __TBB_USE_FENCED_ATOMICS
  159. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,full_fence)
  160. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,full_fence)
  161. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,full_fence)
  162. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,acquire)
  163. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,acquire)
  164. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,acquire)
  165. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,release)
  166. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,release)
  167. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,release)
  168. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,relaxed)
  169. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,relaxed)
  170. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,relaxed)
  171. #if __TBB_64BIT_ATOMICS
  172. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,full_fence)
  173. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,acquire)
  174. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,release)
  175. __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,relaxed)
  176. #endif
  177. #else /* !__TBB_USE_FENCED_ATOMICS */
  178. __TBB_DECL_ATOMIC_PRIMITIVES(1)
  179. __TBB_DECL_ATOMIC_PRIMITIVES(2)
  180. __TBB_DECL_ATOMIC_PRIMITIVES(4)
  181. #if __TBB_64BIT_ATOMICS
  182. __TBB_DECL_ATOMIC_PRIMITIVES(8)
  183. #endif
  184. #endif /* !__TBB_USE_FENCED_ATOMICS */
  185. __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(full_fence);
  186. __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(acquire);
  187. __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(release);
  188. __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(relaxed);
  189. //! Additive inverse of 1 for type T.
  190. /** Various compilers issue various warnings if -1 is used with various integer types.
  191. The baroque expression below avoids all the warnings (we hope). */
  192. #define __TBB_MINUS_ONE(T) (T(T(0)-T(1)))
  193. //! Base class that provides basic functionality for atomic<T> without fetch_and_add.
  194. /** Works for any type T that has the same size as an integral type, has a trivial constructor/destructor,
  195. and can be copied/compared by memcpy/memcmp. */
  196. template<typename T>
  197. struct atomic_impl {
  198. protected:
  199. aligned_storage<T,sizeof(T)> my_storage;
  200. private:
  201. //TODO: rechecks on recent versions of gcc if union is still the _only_ way to do a conversion without warnings
  202. //! Union type used to convert type T to underlying integral type.
  203. template<typename value_type>
  204. union converter {
  205. typedef typename atomic_rep<sizeof(value_type)>::word bits_type;
  206. converter(){}
  207. converter(value_type a_value) : value(a_value) {}
  208. value_type value;
  209. bits_type bits;
  210. };
  211. template<typename value_t>
  212. static typename converter<value_t>::bits_type to_bits(value_t value){
  213. return converter<value_t>(value).bits;
  214. }
  215. template<typename value_t>
  216. static value_t to_value(typename converter<value_t>::bits_type bits){
  217. converter<value_t> u;
  218. u.bits = bits;
  219. return u.value;
  220. }
  221. template<typename value_t>
  222. union ptr_converter; //Primary template declared, but never defined.
  223. template<typename value_t>
  224. union ptr_converter<value_t *> {
  225. ptr_converter(){}
  226. ptr_converter(value_t* a_value) : value(a_value) {}
  227. value_t* value;
  228. uintptr_t bits;
  229. };
  230. //TODO: check if making to_bits accepting reference (thus unifying it with to_bits_ref)
  231. //does not hurt performance
  232. template<typename value_t>
  233. static typename converter<value_t>::bits_type & to_bits_ref(value_t& value){
  234. //TODO: this #ifdef is temporary workaround, as union conversion seems to fail
  235. //on suncc for 64 bit types for 32 bit target
  236. #if !__SUNPRO_CC
  237. return *(typename converter<value_t>::bits_type*)ptr_converter<value_t*>(&value).bits;
  238. #else
  239. return *(typename converter<value_t>::bits_type*)(&value);
  240. #endif
  241. }
  242. public:
  243. typedef T value_type;
  244. #if __TBB_ATOMIC_CTORS
  245. atomic_impl() = default ;
  246. constexpr atomic_impl(value_type value):my_storage(value){}
  247. #endif
  248. template<memory_semantics M>
  249. value_type fetch_and_store( value_type value ) {
  250. return to_value<value_type>(
  251. internal::atomic_traits<sizeof(value_type),M>::fetch_and_store( &my_storage.my_value, to_bits(value) )
  252. );
  253. }
  254. value_type fetch_and_store( value_type value ) {
  255. return fetch_and_store<full_fence>(value);
  256. }
  257. template<memory_semantics M>
  258. value_type compare_and_swap( value_type value, value_type comparand ) {
  259. return to_value<value_type>(
  260. internal::atomic_traits<sizeof(value_type),M>::compare_and_swap( &my_storage.my_value, to_bits(value), to_bits(comparand) )
  261. );
  262. }
  263. value_type compare_and_swap( value_type value, value_type comparand ) {
  264. return compare_and_swap<full_fence>(value,comparand);
  265. }
  266. operator value_type() const volatile { // volatile qualifier here for backwards compatibility
  267. return to_value<value_type>(
  268. __TBB_load_with_acquire( to_bits_ref(my_storage.my_value) )
  269. );
  270. }
  271. template<memory_semantics M>
  272. value_type load () const {
  273. return to_value<value_type>(
  274. internal::atomic_load_store_traits<M>::load( to_bits_ref(my_storage.my_value) )
  275. );
  276. }
  277. value_type load () const {
  278. return load<acquire>();
  279. }
  280. template<memory_semantics M>
  281. void store ( value_type value ) {
  282. internal::atomic_load_store_traits<M>::store( to_bits_ref(my_storage.my_value), to_bits(value));
  283. }
  284. void store ( value_type value ) {
  285. store<release>( value );
  286. }
  287. protected:
  288. value_type store_with_release( value_type rhs ) {
  289. //TODO: unify with store<release>
  290. __TBB_store_with_release( to_bits_ref(my_storage.my_value), to_bits(rhs) );
  291. return rhs;
  292. }
  293. };
  294. //! Base class that provides basic functionality for atomic<T> with fetch_and_add.
  295. /** I is the underlying type.
  296. D is the difference type.
  297. StepType should be char if I is an integral type, and T if I is a T*. */
  298. template<typename I, typename D, typename StepType>
  299. struct atomic_impl_with_arithmetic: atomic_impl<I> {
  300. public:
  301. typedef I value_type;
  302. #if __TBB_ATOMIC_CTORS
  303. atomic_impl_with_arithmetic() = default ;
  304. constexpr atomic_impl_with_arithmetic(value_type value): atomic_impl<I>(value){}
  305. #endif
  306. template<memory_semantics M>
  307. value_type fetch_and_add( D addend ) {
  308. return value_type(internal::atomic_traits<sizeof(value_type),M>::fetch_and_add( &this->my_storage.my_value, addend*sizeof(StepType) ));
  309. }
  310. value_type fetch_and_add( D addend ) {
  311. return fetch_and_add<full_fence>(addend);
  312. }
  313. template<memory_semantics M>
  314. value_type fetch_and_increment() {
  315. return fetch_and_add<M>(1);
  316. }
  317. value_type fetch_and_increment() {
  318. return fetch_and_add(1);
  319. }
  320. template<memory_semantics M>
  321. value_type fetch_and_decrement() {
  322. return fetch_and_add<M>(__TBB_MINUS_ONE(D));
  323. }
  324. value_type fetch_and_decrement() {
  325. return fetch_and_add(__TBB_MINUS_ONE(D));
  326. }
  327. public:
  328. value_type operator+=( D value ) {
  329. return fetch_and_add(value)+value;
  330. }
  331. value_type operator-=( D value ) {
  332. // Additive inverse of value computed using binary minus,
  333. // instead of unary minus, for sake of avoiding compiler warnings.
  334. return operator+=(D(0)-value);
  335. }
  336. value_type operator++() {
  337. return fetch_and_add(1)+1;
  338. }
  339. value_type operator--() {
  340. return fetch_and_add(__TBB_MINUS_ONE(D))-1;
  341. }
  342. value_type operator++(int) {
  343. return fetch_and_add(1);
  344. }
  345. value_type operator--(int) {
  346. return fetch_and_add(__TBB_MINUS_ONE(D));
  347. }
  348. };
  349. } /* Internal */
  350. //! @endcond
  351. //! Primary template for atomic.
  352. /** See the Reference for details.
  353. @ingroup synchronization */
  354. template<typename T>
  355. struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic")
  356. atomic: internal::atomic_impl<T> {
  357. #if __TBB_ATOMIC_CTORS
  358. atomic() = default;
  359. constexpr atomic(T arg): internal::atomic_impl<T>(arg) {}
  360. constexpr atomic<T>(const atomic<T>& rhs): internal::atomic_impl<T>(rhs) {}
  361. #endif
  362. T operator=( T rhs ) {
  363. // "this" required here in strict ISO C++ because store_with_release is a dependent name
  364. return this->store_with_release(rhs);
  365. }
  366. atomic<T>& operator=( const atomic<T>& rhs ) {this->store_with_release(rhs); return *this;}
  367. };
  368. #if __TBB_ATOMIC_CTORS
  369. #define __TBB_DECL_ATOMIC(T) \
  370. template<> struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic") \
  371. atomic<T>: internal::atomic_impl_with_arithmetic<T,T,char> { \
  372. atomic() = default; \
  373. constexpr atomic(T arg): internal::atomic_impl_with_arithmetic<T,T,char>(arg) {} \
  374. constexpr atomic<T>(const atomic<T>& rhs): \
  375. internal::atomic_impl_with_arithmetic<T,T,char>(rhs) {} \
  376. \
  377. T operator=( T rhs ) {return store_with_release(rhs);} \
  378. atomic<T>& operator=( const atomic<T>& rhs ) {store_with_release(rhs); return *this;} \
  379. };
  380. #else
  381. #define __TBB_DECL_ATOMIC(T) \
  382. template<> struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic") \
  383. atomic<T>: internal::atomic_impl_with_arithmetic<T,T,char> { \
  384. T operator=( T rhs ) {return store_with_release(rhs);} \
  385. atomic<T>& operator=( const atomic<T>& rhs ) {store_with_release(rhs); return *this;} \
  386. };
  387. #endif
  388. #if __TBB_64BIT_ATOMICS
  389. //TODO: consider adding non-default (and atomic) copy constructor for 32bit platform
  390. __TBB_DECL_ATOMIC(__TBB_LONG_LONG)
  391. __TBB_DECL_ATOMIC(unsigned __TBB_LONG_LONG)
  392. #else
  393. // test_atomic will verify that sizeof(long long)==8
  394. #endif
  395. __TBB_DECL_ATOMIC(long)
  396. __TBB_DECL_ATOMIC(unsigned long)
  397. #if _MSC_VER && !_WIN64
  398. #if __TBB_ATOMIC_CTORS
  399. /* Special version of __TBB_DECL_ATOMIC that avoids gratuitous warnings from cl /Wp64 option.
  400. It is identical to __TBB_DECL_ATOMIC(unsigned) except that it replaces operator=(T)
  401. with an operator=(U) that explicitly converts the U to a T. Types T and U should be
  402. type synonyms on the platform. Type U should be the wider variant of T from the
  403. perspective of /Wp64. */
  404. #define __TBB_DECL_ATOMIC_ALT(T,U) \
  405. template<> struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic") \
  406. atomic<T>: internal::atomic_impl_with_arithmetic<T,T,char> { \
  407. atomic() = default ; \
  408. constexpr atomic(T arg): internal::atomic_impl_with_arithmetic<T,T,char>(arg) {} \
  409. constexpr atomic<T>(const atomic<T>& rhs): \
  410. internal::atomic_impl_with_arithmetic<T,T,char>(rhs) {} \
  411. \
  412. T operator=( U rhs ) {return store_with_release(T(rhs));} \
  413. atomic<T>& operator=( const atomic<T>& rhs ) {store_with_release(rhs); return *this;} \
  414. };
  415. #else
  416. #define __TBB_DECL_ATOMIC_ALT(T,U) \
  417. template<> struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic") \
  418. atomic<T>: internal::atomic_impl_with_arithmetic<T,T,char> { \
  419. T operator=( U rhs ) {return store_with_release(T(rhs));} \
  420. atomic<T>& operator=( const atomic<T>& rhs ) {store_with_release(rhs); return *this;} \
  421. };
  422. #endif
  423. __TBB_DECL_ATOMIC_ALT(unsigned,size_t)
  424. __TBB_DECL_ATOMIC_ALT(int,ptrdiff_t)
  425. #else
  426. __TBB_DECL_ATOMIC(unsigned)
  427. __TBB_DECL_ATOMIC(int)
  428. #endif /* _MSC_VER && !_WIN64 */
  429. __TBB_DECL_ATOMIC(unsigned short)
  430. __TBB_DECL_ATOMIC(short)
  431. __TBB_DECL_ATOMIC(char)
  432. __TBB_DECL_ATOMIC(signed char)
  433. __TBB_DECL_ATOMIC(unsigned char)
  434. #if !_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED)
  435. __TBB_DECL_ATOMIC(wchar_t)
  436. #endif /* _MSC_VER||!defined(_NATIVE_WCHAR_T_DEFINED) */
  437. //! Specialization for atomic<T*> with arithmetic and operator->.
  438. template<typename T> struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic")
  439. atomic<T*>: internal::atomic_impl_with_arithmetic<T*,ptrdiff_t,T> {
  440. #if __TBB_ATOMIC_CTORS
  441. atomic() = default ;
  442. constexpr atomic(T* arg): internal::atomic_impl_with_arithmetic<T*,ptrdiff_t,T>(arg) {}
  443. constexpr atomic(const atomic<T*>& rhs): internal::atomic_impl_with_arithmetic<T*,ptrdiff_t,T>(rhs) {}
  444. #endif
  445. T* operator=( T* rhs ) {
  446. // "this" required here in strict ISO C++ because store_with_release is a dependent name
  447. return this->store_with_release(rhs);
  448. }
  449. atomic<T*>& operator=( const atomic<T*>& rhs ) {
  450. this->store_with_release(rhs); return *this;
  451. }
  452. T* operator->() const {
  453. return (*this);
  454. }
  455. };
  456. //! Specialization for atomic<void*>, for sake of not allowing arithmetic or operator->.
  457. template<> struct __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::atomic is deprecated, use std::atomic")
  458. atomic<void*>: internal::atomic_impl<void*> {
  459. #if __TBB_ATOMIC_CTORS
  460. atomic() = default ;
  461. constexpr atomic(void* arg): internal::atomic_impl<void*>(arg) {}
  462. constexpr atomic(const atomic<void*>& rhs): internal::atomic_impl<void*>(rhs) {}
  463. #endif
  464. void* operator=( void* rhs ) {
  465. // "this" required here in strict ISO C++ because store_with_release is a dependent name
  466. return this->store_with_release(rhs);
  467. }
  468. atomic<void*>& operator=( const atomic<void*>& rhs ) {
  469. this->store_with_release(rhs); return *this;
  470. }
  471. };
  472. // Helpers to workaround ugly syntax of calling template member function of a
  473. // template class with template argument dependent on template parameters.
  474. template <memory_semantics M, typename T>
  475. T load ( const atomic<T>& a ) { return a.template load<M>(); }
  476. template <memory_semantics M, typename T>
  477. void store ( atomic<T>& a, T value ) { a.template store<M>(value); }
  478. namespace interface6{
  479. //! Make an atomic for use in an initialization (list), as an alternative to zero-initialization or normal assignment.
  480. template<typename T>
  481. atomic<T> make_atomic(T t) {
  482. atomic<T> a;
  483. store<relaxed>(a,t);
  484. return a;
  485. }
  486. }
  487. using interface6::make_atomic;
  488. namespace internal {
  489. template<memory_semantics M, typename T >
  490. void swap(atomic<T> & lhs, atomic<T> & rhs){
  491. T tmp = load<M>(lhs);
  492. store<M>(lhs,load<M>(rhs));
  493. store<M>(rhs,tmp);
  494. }
  495. // only to aid in the gradual conversion of ordinary variables to proper atomics
  496. template<typename T>
  497. inline atomic<T>& as_atomic( T& t ) {
  498. return (atomic<T>&)t;
  499. }
  500. } // namespace tbb::internal
  501. } // namespace tbb
  502. #if _MSC_VER && !__INTEL_COMPILER
  503. #pragma warning (pop)
  504. #endif // warnings are restored
  505. #include "internal/_warning_suppress_disable_notice.h"
  506. #undef __TBB_atomic_H_include_area
  507. #endif /* __TBB_atomic_H */