You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

558 lines
21 KiB

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