/* Copyright (c) 2005-2017 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifndef __TBB_atomic_H #define __TBB_atomic_H #include #if _MSC_VER #define __TBB_LONG_LONG __int64 #else #define __TBB_LONG_LONG long long #endif /* _MSC_VER */ #include "tbb_machine.h" #if _MSC_VER && !__INTEL_COMPILER // Suppress overzealous compiler warnings till the end of the file #pragma warning (push) #pragma warning (disable: 4244 4267 4512) #endif namespace tbb { //! Specifies memory semantics. enum memory_semantics { //! Sequential consistency full_fence, //! Acquire acquire, //! Release release, //! No ordering relaxed }; //! @cond INTERNAL namespace internal { #if __TBB_ALIGNAS_PRESENT #define __TBB_DECL_ATOMIC_FIELD(t,f,a) alignas(a) t f; #elif __TBB_ATTRIBUTE_ALIGNED_PRESENT #define __TBB_DECL_ATOMIC_FIELD(t,f,a) t f __attribute__ ((aligned(a))); #elif __TBB_DECLSPEC_ALIGN_PRESENT #define __TBB_DECL_ATOMIC_FIELD(t,f,a) __declspec(align(a)) t f; #else #error Do not know syntax for forcing alignment. #endif template struct atomic_rep; // Primary template declared, but never defined. template<> struct atomic_rep<1> { // Specialization typedef int8_t word; }; template<> struct atomic_rep<2> { // Specialization typedef int16_t word; }; template<> struct atomic_rep<4> { // Specialization #if _MSC_VER && !_WIN64 // Work-around that avoids spurious /Wp64 warnings typedef intptr_t word; #else typedef int32_t word; #endif }; #if __TBB_64BIT_ATOMICS template<> struct atomic_rep<8> { // Specialization typedef int64_t word; }; #endif template struct aligned_storage; //the specializations are needed to please MSVC syntax of __declspec(align()) which accept _literal_ constants only #if __TBB_ATOMIC_CTORS #define ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(S) \ template \ struct aligned_storage { \ __TBB_DECL_ATOMIC_FIELD(value_type,my_value,S) \ aligned_storage() = default ; \ constexpr aligned_storage(value_type value):my_value(value){} \ }; \ #else #define ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(S) \ template \ struct aligned_storage { \ __TBB_DECL_ATOMIC_FIELD(value_type,my_value,S) \ }; \ #endif template struct aligned_storage { value_type my_value; #if __TBB_ATOMIC_CTORS aligned_storage() = default ; constexpr aligned_storage(value_type value):my_value(value){} #endif }; ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(2) ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(4) #if __TBB_64BIT_ATOMICS ATOMIC_STORAGE_PARTIAL_SPECIALIZATION(8) #endif template struct atomic_traits; // Primary template declared, but not defined. #define __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(S,M) \ template<> struct atomic_traits { \ typedef atomic_rep::word word; \ inline static word compare_and_swap( volatile void* location, word new_value, word comparand ) { \ return __TBB_machine_cmpswp##S##M(location,new_value,comparand); \ } \ inline static word fetch_and_add( volatile void* location, word addend ) { \ return __TBB_machine_fetchadd##S##M(location,addend); \ } \ inline static word fetch_and_store( volatile void* location, word value ) { \ return __TBB_machine_fetchstore##S##M(location,value); \ } \ }; #define __TBB_DECL_ATOMIC_PRIMITIVES(S) \ template \ struct atomic_traits { \ typedef atomic_rep::word word; \ inline static word compare_and_swap( volatile void* location, word new_value, word comparand ) { \ return __TBB_machine_cmpswp##S(location,new_value,comparand); \ } \ inline static word fetch_and_add( volatile void* location, word addend ) { \ return __TBB_machine_fetchadd##S(location,addend); \ } \ inline static word fetch_and_store( volatile void* location, word value ) { \ return __TBB_machine_fetchstore##S(location,value); \ } \ }; template struct atomic_load_store_traits; // Primary template declaration #define __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(M) \ template<> struct atomic_load_store_traits { \ template \ inline static T load( const volatile T& location ) { \ return __TBB_load_##M( location ); \ } \ template \ inline static void store( volatile T& location, T value ) { \ __TBB_store_##M( location, value ); \ } \ } #if __TBB_USE_FENCED_ATOMICS __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,full_fence) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,full_fence) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,full_fence) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,acquire) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,acquire) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,acquire) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,release) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,release) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,release) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(1,relaxed) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(2,relaxed) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(4,relaxed) #if __TBB_64BIT_ATOMICS __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,full_fence) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,acquire) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,release) __TBB_DECL_FENCED_ATOMIC_PRIMITIVES(8,relaxed) #endif #else /* !__TBB_USE_FENCED_ATOMICS */ __TBB_DECL_ATOMIC_PRIMITIVES(1) __TBB_DECL_ATOMIC_PRIMITIVES(2) __TBB_DECL_ATOMIC_PRIMITIVES(4) #if __TBB_64BIT_ATOMICS __TBB_DECL_ATOMIC_PRIMITIVES(8) #endif #endif /* !__TBB_USE_FENCED_ATOMICS */ __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(full_fence); __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(acquire); __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(release); __TBB_DECL_ATOMIC_LOAD_STORE_PRIMITIVES(relaxed); //! Additive inverse of 1 for type T. /** Various compilers issue various warnings if -1 is used with various integer types. The baroque expression below avoids all the warnings (we hope). */ #define __TBB_MINUS_ONE(T) (T(T(0)-T(1))) //! Base class that provides basic functionality for atomic without fetch_and_add. /** Works for any type T that has the same size as an integral type, has a trivial constructor/destructor, and can be copied/compared by memcpy/memcmp. */ template struct atomic_impl { protected: aligned_storage my_storage; private: //TODO: rechecks on recent versions of gcc if union is still the _only_ way to do a conversion without warnings //! Union type used to convert type T to underlying integral type. template union converter { typedef typename atomic_rep::word bits_type; converter(){} converter(value_type a_value) : value(a_value) {} value_type value; bits_type bits; }; template static typename converter::bits_type to_bits(value_t value){ return converter(value).bits; } template static value_t to_value(typename converter::bits_type bits){ converter u; u.bits = bits; return u.value; } template union ptr_converter; //Primary template declared, but never defined. template union ptr_converter { ptr_converter(){} ptr_converter(value_t* a_value) : value(a_value) {} value_t* value; uintptr_t bits; }; //TODO: check if making to_bits accepting reference (thus unifying it with to_bits_ref) //does not hurt performance template static typename converter::bits_type & to_bits_ref(value_t& value){ //TODO: this #ifdef is temporary workaround, as union conversion seems to fail //on suncc for 64 bit types for 32 bit target #if !__SUNPRO_CC return *(typename converter::bits_type*)ptr_converter(&value).bits; #else return *(typename converter::bits_type*)(&value); #endif } public: typedef T value_type; #if __TBB_ATOMIC_CTORS atomic_impl() = default ; constexpr atomic_impl(value_type value):my_storage(value){} #endif template value_type fetch_and_store( value_type value ) { return to_value( internal::atomic_traits::fetch_and_store( &my_storage.my_value, to_bits(value) ) ); } value_type fetch_and_store( value_type value ) { return fetch_and_store(value); } template value_type compare_and_swap( value_type value, value_type comparand ) { return to_value( internal::atomic_traits::compare_and_swap( &my_storage.my_value, to_bits(value), to_bits(comparand) ) ); } value_type compare_and_swap( value_type value, value_type comparand ) { return compare_and_swap(value,comparand); } operator value_type() const volatile { // volatile qualifier here for backwards compatibility return to_value( __TBB_load_with_acquire( to_bits_ref(my_storage.my_value) ) ); } template value_type load () const { return to_value( internal::atomic_load_store_traits::load( to_bits_ref(my_storage.my_value) ) ); } value_type load () const { return load(); } template void store ( value_type value ) { internal::atomic_load_store_traits::store( to_bits_ref(my_storage.my_value), to_bits(value)); } void store ( value_type value ) { store( value ); } protected: value_type store_with_release( value_type rhs ) { //TODO: unify with store __TBB_store_with_release( to_bits_ref(my_storage.my_value), to_bits(rhs) ); return rhs; } }; //! Base class that provides basic functionality for atomic with fetch_and_add. /** I is the underlying type. D is the difference type. StepType should be char if I is an integral type, and T if I is a T*. */ template struct atomic_impl_with_arithmetic: atomic_impl { public: typedef I value_type; #if __TBB_ATOMIC_CTORS atomic_impl_with_arithmetic() = default ; constexpr atomic_impl_with_arithmetic(value_type value): atomic_impl(value){} #endif template value_type fetch_and_add( D addend ) { return value_type(internal::atomic_traits::fetch_and_add( &this->my_storage.my_value, addend*sizeof(StepType) )); } value_type fetch_and_add( D addend ) { return fetch_and_add(addend); } template value_type fetch_and_increment() { return fetch_and_add(1); } value_type fetch_and_increment() { return fetch_and_add(1); } template value_type fetch_and_decrement() { return fetch_and_add(__TBB_MINUS_ONE(D)); } value_type fetch_and_decrement() { return fetch_and_add(__TBB_MINUS_ONE(D)); } public: value_type operator+=( D value ) { return fetch_and_add(value)+value; } value_type operator-=( D value ) { // Additive inverse of value computed using binary minus, // instead of unary minus, for sake of avoiding compiler warnings. return operator+=(D(0)-value); } value_type operator++() { return fetch_and_add(1)+1; } value_type operator--() { return fetch_and_add(__TBB_MINUS_ONE(D))-1; } value_type operator++(int) { return fetch_and_add(1); } value_type operator--(int) { return fetch_and_add(__TBB_MINUS_ONE(D)); } }; } /* Internal */ //! @endcond //! Primary template for atomic. /** See the Reference for details. @ingroup synchronization */ template struct atomic: internal::atomic_impl { #if __TBB_ATOMIC_CTORS atomic() = default; constexpr atomic(T arg): internal::atomic_impl(arg) {} #endif T operator=( T rhs ) { // "this" required here in strict ISO C++ because store_with_release is a dependent name return this->store_with_release(rhs); } atomic& operator=( const atomic& rhs ) {this->store_with_release(rhs); return *this;} }; #if __TBB_ATOMIC_CTORS #define __TBB_DECL_ATOMIC(T) \ template<> struct atomic: internal::atomic_impl_with_arithmetic { \ atomic() = default; \ constexpr atomic(T arg): internal::atomic_impl_with_arithmetic(arg) {} \ \ T operator=( T rhs ) {return store_with_release(rhs);} \ atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \ }; #else #define __TBB_DECL_ATOMIC(T) \ template<> struct atomic: internal::atomic_impl_with_arithmetic { \ T operator=( T rhs ) {return store_with_release(rhs);} \ atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \ }; #endif #if __TBB_64BIT_ATOMICS //TODO: consider adding non-default (and atomic) copy constructor for 32bit platform __TBB_DECL_ATOMIC(__TBB_LONG_LONG) __TBB_DECL_ATOMIC(unsigned __TBB_LONG_LONG) #else // test_atomic will verify that sizeof(long long)==8 #endif __TBB_DECL_ATOMIC(long) __TBB_DECL_ATOMIC(unsigned long) #if _MSC_VER && !_WIN64 #if __TBB_ATOMIC_CTORS /* Special version of __TBB_DECL_ATOMIC that avoids gratuitous warnings from cl /Wp64 option. It is identical to __TBB_DECL_ATOMIC(unsigned) except that it replaces operator=(T) with an operator=(U) that explicitly converts the U to a T. Types T and U should be type synonyms on the platform. Type U should be the wider variant of T from the perspective of /Wp64. */ #define __TBB_DECL_ATOMIC_ALT(T,U) \ template<> struct atomic: internal::atomic_impl_with_arithmetic { \ atomic() = default ; \ constexpr atomic(T arg): internal::atomic_impl_with_arithmetic(arg) {} \ T operator=( U rhs ) {return store_with_release(T(rhs));} \ atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \ }; #else #define __TBB_DECL_ATOMIC_ALT(T,U) \ template<> struct atomic: internal::atomic_impl_with_arithmetic { \ T operator=( U rhs ) {return store_with_release(T(rhs));} \ atomic& operator=( const atomic& rhs ) {store_with_release(rhs); return *this;} \ }; #endif __TBB_DECL_ATOMIC_ALT(unsigned,size_t) __TBB_DECL_ATOMIC_ALT(int,ptrdiff_t) #else __TBB_DECL_ATOMIC(unsigned) __TBB_DECL_ATOMIC(int) #endif /* _MSC_VER && !_WIN64 */ __TBB_DECL_ATOMIC(unsigned short) __TBB_DECL_ATOMIC(short) __TBB_DECL_ATOMIC(char) __TBB_DECL_ATOMIC(signed char) __TBB_DECL_ATOMIC(unsigned char) #if !_MSC_VER || defined(_NATIVE_WCHAR_T_DEFINED) __TBB_DECL_ATOMIC(wchar_t) #endif /* _MSC_VER||!defined(_NATIVE_WCHAR_T_DEFINED) */ //! Specialization for atomic with arithmetic and operator->. template struct atomic: internal::atomic_impl_with_arithmetic { #if __TBB_ATOMIC_CTORS atomic() = default ; constexpr atomic(T* arg): internal::atomic_impl_with_arithmetic(arg) {} #endif T* operator=( T* rhs ) { // "this" required here in strict ISO C++ because store_with_release is a dependent name return this->store_with_release(rhs); } atomic& operator=( const atomic& rhs ) { this->store_with_release(rhs); return *this; } T* operator->() const { return (*this); } }; //! Specialization for atomic, for sake of not allowing arithmetic or operator->. template<> struct atomic: internal::atomic_impl { #if __TBB_ATOMIC_CTORS atomic() = default ; constexpr atomic(void* arg): internal::atomic_impl(arg) {} #endif void* operator=( void* rhs ) { // "this" required here in strict ISO C++ because store_with_release is a dependent name return this->store_with_release(rhs); } atomic& operator=( const atomic& rhs ) { this->store_with_release(rhs); return *this; } }; // Helpers to workaround ugly syntax of calling template member function of a // template class with template argument dependent on template parameters. template T load ( const atomic& a ) { return a.template load(); } template void store ( atomic& a, T value ) { a.template store(value); } namespace interface6{ //! Make an atomic for use in an initialization (list), as an alternative to zero-initialization or normal assignment. template atomic make_atomic(T t) { atomic a; store(a,t); return a; } } using interface6::make_atomic; namespace internal { template void swap(atomic & lhs, atomic & rhs){ T tmp = load(lhs); store(lhs,load(rhs)); store(rhs,tmp); } // only to aid in the gradual conversion of ordinary variables to proper atomics template inline atomic& as_atomic( T& t ) { return (atomic&)t; } } // namespace tbb::internal } // namespace tbb #if _MSC_VER && !__INTEL_COMPILER #pragma warning (pop) #endif // warnings are restored #endif /* __TBB_atomic_H */