554 lines
21 KiB

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