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.

919 lines
37 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_machine_H
  24. #define __TBB_machine_H
  25. /** This header provides basic platform abstraction layer by hooking up appropriate
  26. architecture/OS/compiler specific headers from the /include/tbb/machine directory.
  27. If a plug-in header does not implement all the required APIs, it must specify
  28. the missing ones by setting one or more of the following macros:
  29. __TBB_USE_GENERIC_PART_WORD_CAS
  30. __TBB_USE_GENERIC_PART_WORD_FETCH_ADD
  31. __TBB_USE_GENERIC_PART_WORD_FETCH_STORE
  32. __TBB_USE_GENERIC_FETCH_ADD
  33. __TBB_USE_GENERIC_FETCH_STORE
  34. __TBB_USE_GENERIC_DWORD_FETCH_ADD
  35. __TBB_USE_GENERIC_DWORD_FETCH_STORE
  36. __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE
  37. __TBB_USE_GENERIC_FULL_FENCED_LOAD_STORE
  38. __TBB_USE_GENERIC_RELAXED_LOAD_STORE
  39. __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
  40. In this case tbb_machine.h will add missing functionality based on a minimal set
  41. of APIs that are required to be implemented by all plug-n headers as described
  42. further.
  43. Note that these generic implementations may be sub-optimal for a particular
  44. architecture, and thus should be relied upon only after careful evaluation
  45. or as the last resort.
  46. Additionally __TBB_64BIT_ATOMICS can be set to 0 on a 32-bit architecture to
  47. indicate that the port is not going to support double word atomics. It may also
  48. be set to 1 explicitly, though normally this is not necessary as tbb_machine.h
  49. will set it automatically.
  50. __TBB_BIG_ENDIAN macro can be defined by the implementation as well.
  51. It is used only if the __TBB_USE_GENERIC_PART_WORD_CAS is set.
  52. Possible values are:
  53. - 1 if the system is big endian,
  54. - 0 if it is little endian,
  55. - or -1 to explicitly state that __TBB_USE_GENERIC_PART_WORD_CAS can not be used.
  56. -1 should be used when it is known in advance that endianness can change in run time
  57. or it is not simple big or little but something more complex.
  58. The system will try to detect it in run time if it is not set(in assumption that it
  59. is either a big or little one).
  60. Prerequisites for each architecture port
  61. ----------------------------------------
  62. The following functions and macros have no generic implementation. Therefore they must be
  63. implemented in each machine architecture specific header either as a conventional
  64. function or as a functional macro.
  65. __TBB_WORDSIZE
  66. This is the size of machine word in bytes, i.e. for 32 bit systems it
  67. should be defined to 4.
  68. __TBB_Yield()
  69. Signals OS that the current thread is willing to relinquish the remainder
  70. of its time quantum.
  71. __TBB_full_memory_fence()
  72. Must prevent all memory operations from being reordered across it (both
  73. by hardware and compiler). All such fences must be totally ordered (or
  74. sequentially consistent).
  75. __TBB_machine_cmpswp4( volatile void *ptr, int32_t value, int32_t comparand )
  76. Must be provided if __TBB_USE_FENCED_ATOMICS is not set.
  77. __TBB_machine_cmpswp8( volatile void *ptr, int32_t value, int64_t comparand )
  78. Must be provided for 64-bit architectures if __TBB_USE_FENCED_ATOMICS is not set,
  79. and for 32-bit architectures if __TBB_64BIT_ATOMICS is set
  80. __TBB_machine_<op><S><fence>(...), where
  81. <op> = {cmpswp, fetchadd, fetchstore}
  82. <S> = {1, 2, 4, 8}
  83. <fence> = {full_fence, acquire, release, relaxed}
  84. Must be provided if __TBB_USE_FENCED_ATOMICS is set.
  85. __TBB_control_consistency_helper()
  86. Bridges the memory-semantics gap between architectures providing only
  87. implicit C++0x "consume" semantics (like Power Architecture) and those
  88. also implicitly obeying control dependencies (like IA-64).
  89. It must be used only in conditional code where the condition is itself
  90. data-dependent, and will then make subsequent code behave as if the
  91. original data dependency were acquired.
  92. It needs only a compiler fence where implied by the architecture
  93. either specifically (like IA-64) or because generally stronger "acquire"
  94. semantics are enforced (like x86).
  95. It is always valid, though potentially suboptimal, to replace
  96. control with acquire on the load and then remove the helper.
  97. __TBB_acquire_consistency_helper(), __TBB_release_consistency_helper()
  98. Must be provided if __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE is set.
  99. Enforce acquire and release semantics in generic implementations of fenced
  100. store and load operations. Depending on the particular architecture/compiler
  101. combination they may be a hardware fence, a compiler fence, both or nothing.
  102. **/
  103. #include "tbb_stddef.h"
  104. namespace tbb {
  105. namespace internal {
  106. ////////////////////////////////////////////////////////////////////////////////
  107. // Overridable helpers declarations
  108. //
  109. // A machine/*.h file may choose to define these templates, otherwise it must
  110. // request default implementation by setting appropriate __TBB_USE_GENERIC_XXX macro(s).
  111. //
  112. template <typename T, std::size_t S>
  113. struct machine_load_store;
  114. template <typename T, std::size_t S>
  115. struct machine_load_store_relaxed;
  116. template <typename T, std::size_t S>
  117. struct machine_load_store_seq_cst;
  118. //
  119. // End of overridable helpers declarations
  120. ////////////////////////////////////////////////////////////////////////////////
  121. template<size_t S> struct atomic_selector;
  122. template<> struct atomic_selector<1> {
  123. typedef int8_t word;
  124. inline static word fetch_store ( volatile void* location, word value );
  125. };
  126. template<> struct atomic_selector<2> {
  127. typedef int16_t word;
  128. inline static word fetch_store ( volatile void* location, word value );
  129. };
  130. template<> struct atomic_selector<4> {
  131. #if _MSC_VER && !_WIN64
  132. // Work-around that avoids spurious /Wp64 warnings
  133. typedef intptr_t word;
  134. #else
  135. typedef int32_t word;
  136. #endif
  137. inline static word fetch_store ( volatile void* location, word value );
  138. };
  139. template<> struct atomic_selector<8> {
  140. typedef int64_t word;
  141. inline static word fetch_store ( volatile void* location, word value );
  142. };
  143. }} // namespaces internal, tbb
  144. #define __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(M) \
  145. inline void __TBB_machine_generic_store8##M(volatile void *ptr, int64_t value) { \
  146. for(;;) { \
  147. int64_t result = *(int64_t *)ptr; \
  148. if( __TBB_machine_cmpswp8##M(ptr,value,result)==result ) break; \
  149. } \
  150. } \
  151. #define __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(M) \
  152. inline int64_t __TBB_machine_generic_load8##M(const volatile void *ptr) { \
  153. /* Comparand and new value may be anything, they only must be equal, and */ \
  154. /* the value should have a low probability to be actually found in 'location'.*/ \
  155. const int64_t anyvalue = 2305843009213693951LL; \
  156. return __TBB_machine_cmpswp8##M(const_cast<volatile void *>(ptr),anyvalue,anyvalue); \
  157. } \
  158. #if _WIN32||_WIN64
  159. #ifdef _MANAGED
  160. #pragma managed(push, off)
  161. #endif
  162. #if __MINGW64__ || __MINGW32__
  163. extern "C" __declspec(dllimport) int __stdcall SwitchToThread( void );
  164. #define __TBB_Yield() SwitchToThread()
  165. #if (TBB_USE_GCC_BUILTINS && __TBB_GCC_BUILTIN_ATOMICS_PRESENT)
  166. #include "machine/gcc_generic.h"
  167. #elif __MINGW64__
  168. #include "machine/linux_intel64.h"
  169. #elif __MINGW32__
  170. #include "machine/linux_ia32.h"
  171. #endif
  172. #elif (TBB_USE_ICC_BUILTINS && __TBB_ICC_BUILTIN_ATOMICS_PRESENT)
  173. #include "machine/icc_generic.h"
  174. #elif defined(_M_IX86)
  175. #include "machine/windows_ia32.h"
  176. #elif defined(_M_X64)
  177. #include "machine/windows_intel64.h"
  178. #elif _XBOX
  179. #include "machine/xbox360_ppc.h"
  180. #endif
  181. #ifdef _MANAGED
  182. #pragma managed(pop)
  183. #endif
  184. #elif __TBB_DEFINE_MIC
  185. #include "machine/mic_common.h"
  186. //TODO: check if ICC atomic intrinsics are available for MIC
  187. #include "machine/linux_intel64.h"
  188. #elif __linux__ || __FreeBSD__ || __NetBSD__
  189. #if (TBB_USE_GCC_BUILTINS && __TBB_GCC_BUILTIN_ATOMICS_PRESENT)
  190. #include "machine/gcc_generic.h"
  191. #elif (TBB_USE_ICC_BUILTINS && __TBB_ICC_BUILTIN_ATOMICS_PRESENT)
  192. #include "machine/icc_generic.h"
  193. #elif __i386__
  194. #include "machine/linux_ia32.h"
  195. #elif __x86_64__
  196. #include "machine/linux_intel64.h"
  197. #elif __ia64__
  198. #include "machine/linux_ia64.h"
  199. #elif __powerpc__
  200. #include "machine/mac_ppc.h"
  201. #elif __arm__
  202. #include "machine/gcc_armv7.h"
  203. #elif __TBB_GCC_BUILTIN_ATOMICS_PRESENT
  204. #include "machine/gcc_generic.h"
  205. #endif
  206. #include "machine/linux_common.h"
  207. #elif __APPLE__
  208. //TODO: TBB_USE_GCC_BUILTINS is not used for Mac, Sun, Aix
  209. #if (TBB_USE_ICC_BUILTINS && __TBB_ICC_BUILTIN_ATOMICS_PRESENT)
  210. #include "machine/icc_generic.h"
  211. #elif __i386__
  212. #include "machine/linux_ia32.h"
  213. #elif __x86_64__
  214. #include "machine/linux_intel64.h"
  215. #elif __POWERPC__
  216. #include "machine/mac_ppc.h"
  217. #endif
  218. #include "machine/macos_common.h"
  219. #elif _AIX
  220. #include "machine/ibm_aix51.h"
  221. #elif __sun || __SUNPRO_CC
  222. #define __asm__ asm
  223. #define __volatile__ volatile
  224. #if __i386 || __i386__
  225. #include "machine/linux_ia32.h"
  226. #elif __x86_64__
  227. #include "machine/linux_intel64.h"
  228. #elif __sparc
  229. #include "machine/sunos_sparc.h"
  230. #endif
  231. #include <sched.h>
  232. #define __TBB_Yield() sched_yield()
  233. #endif /* OS selection */
  234. #ifndef __TBB_64BIT_ATOMICS
  235. #define __TBB_64BIT_ATOMICS 1
  236. #endif
  237. //TODO: replace usage of these functions with usage of tbb::atomic, and then remove them
  238. //TODO: map functions with W suffix to use cast to tbb::atomic and according op, i.e. as_atomic().op()
  239. // Special atomic functions
  240. #if __TBB_USE_FENCED_ATOMICS
  241. #define __TBB_machine_cmpswp1 __TBB_machine_cmpswp1full_fence
  242. #define __TBB_machine_cmpswp2 __TBB_machine_cmpswp2full_fence
  243. #define __TBB_machine_cmpswp4 __TBB_machine_cmpswp4full_fence
  244. #define __TBB_machine_cmpswp8 __TBB_machine_cmpswp8full_fence
  245. #if __TBB_WORDSIZE==8
  246. #define __TBB_machine_fetchadd8 __TBB_machine_fetchadd8full_fence
  247. #define __TBB_machine_fetchstore8 __TBB_machine_fetchstore8full_fence
  248. #define __TBB_FetchAndAddWrelease(P,V) __TBB_machine_fetchadd8release(P,V)
  249. #define __TBB_FetchAndIncrementWacquire(P) __TBB_machine_fetchadd8acquire(P,1)
  250. #define __TBB_FetchAndDecrementWrelease(P) __TBB_machine_fetchadd8release(P,(-1))
  251. #else
  252. #define __TBB_machine_fetchadd4 __TBB_machine_fetchadd4full_fence
  253. #define __TBB_machine_fetchstore4 __TBB_machine_fetchstore4full_fence
  254. #define __TBB_FetchAndAddWrelease(P,V) __TBB_machine_fetchadd4release(P,V)
  255. #define __TBB_FetchAndIncrementWacquire(P) __TBB_machine_fetchadd4acquire(P,1)
  256. #define __TBB_FetchAndDecrementWrelease(P) __TBB_machine_fetchadd4release(P,(-1))
  257. #endif /* __TBB_WORDSIZE==4 */
  258. #else /* !__TBB_USE_FENCED_ATOMICS */
  259. #define __TBB_FetchAndAddWrelease(P,V) __TBB_FetchAndAddW(P,V)
  260. #define __TBB_FetchAndIncrementWacquire(P) __TBB_FetchAndAddW(P,1)
  261. #define __TBB_FetchAndDecrementWrelease(P) __TBB_FetchAndAddW(P,(-1))
  262. #endif /* !__TBB_USE_FENCED_ATOMICS */
  263. #if __TBB_WORDSIZE==4
  264. #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp4(P,V,C)
  265. #define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchadd4(P,V)
  266. #define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstore4(P,V)
  267. #elif __TBB_WORDSIZE==8
  268. #if __TBB_USE_GENERIC_DWORD_LOAD_STORE || __TBB_USE_GENERIC_DWORD_FETCH_ADD || __TBB_USE_GENERIC_DWORD_FETCH_STORE
  269. #error These macros should only be used on 32-bit platforms.
  270. #endif
  271. #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswp8(P,V,C)
  272. #define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchadd8(P,V)
  273. #define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstore8(P,V)
  274. #else /* __TBB_WORDSIZE != 8 */
  275. #error Unsupported machine word size.
  276. #endif /* __TBB_WORDSIZE */
  277. #ifndef __TBB_Pause
  278. inline void __TBB_Pause(int32_t) {
  279. __TBB_Yield();
  280. }
  281. #endif
  282. namespace tbb {
  283. //! Sequentially consistent full memory fence.
  284. inline void atomic_fence () { __TBB_full_memory_fence(); }
  285. namespace internal {
  286. //! Class that implements exponential backoff.
  287. /** See implementation of spin_wait_while_eq for an example. */
  288. class atomic_backoff : no_copy {
  289. //! Time delay, in units of "pause" instructions.
  290. /** Should be equal to approximately the number of "pause" instructions
  291. that take the same time as an context switch. */
  292. static const int32_t LOOPS_BEFORE_YIELD = 16;
  293. int32_t count;
  294. public:
  295. atomic_backoff() : count(1) {}
  296. //! Pause for a while.
  297. void pause() {
  298. if( count<=LOOPS_BEFORE_YIELD ) {
  299. __TBB_Pause(count);
  300. // Pause twice as long the next time.
  301. count*=2;
  302. } else {
  303. // Pause is so long that we might as well yield CPU to scheduler.
  304. __TBB_Yield();
  305. }
  306. }
  307. // pause for a few times and then return false immediately.
  308. bool bounded_pause() {
  309. if( count<=LOOPS_BEFORE_YIELD ) {
  310. __TBB_Pause(count);
  311. // Pause twice as long the next time.
  312. count*=2;
  313. return true;
  314. } else {
  315. return false;
  316. }
  317. }
  318. void reset() {
  319. count = 1;
  320. }
  321. };
  322. //! Spin WHILE the value of the variable is equal to a given value
  323. /** T and U should be comparable types. */
  324. template<typename T, typename U>
  325. void spin_wait_while_eq( const volatile T& location, U value ) {
  326. atomic_backoff backoff;
  327. while( location==value ) backoff.pause();
  328. }
  329. //! Spin UNTIL the value of the variable is equal to a given value
  330. /** T and U should be comparable types. */
  331. template<typename T, typename U>
  332. void spin_wait_until_eq( const volatile T& location, const U value ) {
  333. atomic_backoff backoff;
  334. while( location!=value ) backoff.pause();
  335. }
  336. #if (__TBB_USE_GENERIC_PART_WORD_CAS && ( __TBB_BIG_ENDIAN==-1))
  337. #error generic implementation of part-word CAS was explicitly disabled for this configuration
  338. #endif
  339. #if (__TBB_BIG_ENDIAN!=-1)
  340. // there are following restrictions/limitations for this operation:
  341. // - T should be unsigned, otherwise sign propagation will break correctness of bit manipulations.
  342. // - T should be integer type of at most 4 bytes, for the casts and calculations to work.
  343. // (Together, these rules limit applicability of Masked CAS to uint8_t and uint16_t only,
  344. // as it does nothing useful for 4 bytes).
  345. // - The operation assumes that the architecture consistently uses either little-endian or big-endian:
  346. // it does not support mixed-endian or page-specific bi-endian architectures.
  347. // This function is the only use of __TBB_BIG_ENDIAN.
  348. //
  349. //TODO: add static_assert for the requirements stated above
  350. //TODO: check if it works with signed types
  351. template<typename T>
  352. inline T __TBB_MaskedCompareAndSwap (volatile T * const ptr, const T value, const T comparand ) {
  353. struct endianness{ static bool is_big_endian(){
  354. #ifndef __TBB_BIG_ENDIAN
  355. const uint32_t probe = 0x03020100;
  356. return (((const char*)(&probe))[0]==0x03);
  357. #elif (__TBB_BIG_ENDIAN==0) || (__TBB_BIG_ENDIAN==1)
  358. return __TBB_BIG_ENDIAN;
  359. #else
  360. #error unexpected value of __TBB_BIG_ENDIAN
  361. #endif
  362. }};
  363. const uint32_t byte_offset = (uint32_t) ((uintptr_t)ptr & 0x3);
  364. volatile uint32_t * const aligned_ptr = (uint32_t*)((uintptr_t)ptr - byte_offset );
  365. // location of T within uint32_t for a C++ shift operation
  366. const uint32_t bits_to_shift = 8*(endianness::is_big_endian() ? (4 - sizeof(T) - (byte_offset)) : byte_offset);
  367. const uint32_t mask = (((uint32_t)1<<(sizeof(T)*8)) - 1 )<<bits_to_shift;
  368. const uint32_t shifted_comparand = ((uint32_t)comparand << bits_to_shift)&mask;
  369. const uint32_t shifted_value = ((uint32_t)value << bits_to_shift)&mask;
  370. for(atomic_backoff b;;b.pause()) {
  371. const uint32_t surroundings = *aligned_ptr & ~mask ; // reload the aligned_ptr value which might change during the pause
  372. const uint32_t big_comparand = surroundings | shifted_comparand ;
  373. const uint32_t big_value = surroundings | shifted_value ;
  374. // __TBB_machine_cmpswp4 presumed to have full fence.
  375. // Cast shuts up /Wp64 warning
  376. const uint32_t big_result = (uint32_t)__TBB_machine_cmpswp4( aligned_ptr, big_value, big_comparand );
  377. if( big_result == big_comparand // CAS succeeded
  378. || ((big_result ^ big_comparand) & mask) != 0) // CAS failed and the bits of interest have changed
  379. {
  380. return T((big_result & mask) >> bits_to_shift);
  381. }
  382. else continue; // CAS failed but the bits of interest left unchanged
  383. }
  384. }
  385. #endif //__TBB_BIG_ENDIAN!=-1
  386. template<size_t S, typename T>
  387. inline T __TBB_CompareAndSwapGeneric (volatile void *ptr, T value, T comparand );
  388. template<>
  389. inline uint8_t __TBB_CompareAndSwapGeneric <1,uint8_t> (volatile void *ptr, uint8_t value, uint8_t comparand ) {
  390. #if __TBB_USE_GENERIC_PART_WORD_CAS
  391. return __TBB_MaskedCompareAndSwap<uint8_t>((volatile uint8_t *)ptr,value,comparand);
  392. #else
  393. return __TBB_machine_cmpswp1(ptr,value,comparand);
  394. #endif
  395. }
  396. template<>
  397. inline uint16_t __TBB_CompareAndSwapGeneric <2,uint16_t> (volatile void *ptr, uint16_t value, uint16_t comparand ) {
  398. #if __TBB_USE_GENERIC_PART_WORD_CAS
  399. return __TBB_MaskedCompareAndSwap<uint16_t>((volatile uint16_t *)ptr,value,comparand);
  400. #else
  401. return __TBB_machine_cmpswp2(ptr,value,comparand);
  402. #endif
  403. }
  404. template<>
  405. inline uint32_t __TBB_CompareAndSwapGeneric <4,uint32_t> (volatile void *ptr, uint32_t value, uint32_t comparand ) {
  406. // Cast shuts up /Wp64 warning
  407. return (uint32_t)__TBB_machine_cmpswp4(ptr,value,comparand);
  408. }
  409. #if __TBB_64BIT_ATOMICS
  410. template<>
  411. inline uint64_t __TBB_CompareAndSwapGeneric <8,uint64_t> (volatile void *ptr, uint64_t value, uint64_t comparand ) {
  412. return __TBB_machine_cmpswp8(ptr,value,comparand);
  413. }
  414. #endif
  415. template<size_t S, typename T>
  416. inline T __TBB_FetchAndAddGeneric (volatile void *ptr, T addend) {
  417. atomic_backoff b;
  418. T result;
  419. for(;;) {
  420. result = *reinterpret_cast<volatile T *>(ptr);
  421. // __TBB_CompareAndSwapGeneric presumed to have full fence.
  422. if( __TBB_CompareAndSwapGeneric<S,T> ( ptr, result+addend, result )==result )
  423. break;
  424. b.pause();
  425. }
  426. return result;
  427. }
  428. template<size_t S, typename T>
  429. inline T __TBB_FetchAndStoreGeneric (volatile void *ptr, T value) {
  430. atomic_backoff b;
  431. T result;
  432. for(;;) {
  433. result = *reinterpret_cast<volatile T *>(ptr);
  434. // __TBB_CompareAndSwapGeneric presumed to have full fence.
  435. if( __TBB_CompareAndSwapGeneric<S,T> ( ptr, value, result )==result )
  436. break;
  437. b.pause();
  438. }
  439. return result;
  440. }
  441. #if __TBB_USE_GENERIC_PART_WORD_CAS
  442. #define __TBB_machine_cmpswp1 tbb::internal::__TBB_CompareAndSwapGeneric<1,uint8_t>
  443. #define __TBB_machine_cmpswp2 tbb::internal::__TBB_CompareAndSwapGeneric<2,uint16_t>
  444. #endif
  445. #if __TBB_USE_GENERIC_FETCH_ADD || __TBB_USE_GENERIC_PART_WORD_FETCH_ADD
  446. #define __TBB_machine_fetchadd1 tbb::internal::__TBB_FetchAndAddGeneric<1,uint8_t>
  447. #define __TBB_machine_fetchadd2 tbb::internal::__TBB_FetchAndAddGeneric<2,uint16_t>
  448. #endif
  449. #if __TBB_USE_GENERIC_FETCH_ADD
  450. #define __TBB_machine_fetchadd4 tbb::internal::__TBB_FetchAndAddGeneric<4,uint32_t>
  451. #endif
  452. #if __TBB_USE_GENERIC_FETCH_ADD || __TBB_USE_GENERIC_DWORD_FETCH_ADD
  453. #define __TBB_machine_fetchadd8 tbb::internal::__TBB_FetchAndAddGeneric<8,uint64_t>
  454. #endif
  455. #if __TBB_USE_GENERIC_FETCH_STORE || __TBB_USE_GENERIC_PART_WORD_FETCH_STORE
  456. #define __TBB_machine_fetchstore1 tbb::internal::__TBB_FetchAndStoreGeneric<1,uint8_t>
  457. #define __TBB_machine_fetchstore2 tbb::internal::__TBB_FetchAndStoreGeneric<2,uint16_t>
  458. #endif
  459. #if __TBB_USE_GENERIC_FETCH_STORE
  460. #define __TBB_machine_fetchstore4 tbb::internal::__TBB_FetchAndStoreGeneric<4,uint32_t>
  461. #endif
  462. #if __TBB_USE_GENERIC_FETCH_STORE || __TBB_USE_GENERIC_DWORD_FETCH_STORE
  463. #define __TBB_machine_fetchstore8 tbb::internal::__TBB_FetchAndStoreGeneric<8,uint64_t>
  464. #endif
  465. #if __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
  466. #define __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(S) \
  467. atomic_selector<S>::word atomic_selector<S>::fetch_store ( volatile void* location, word value ) { \
  468. return __TBB_machine_fetchstore##S( location, value ); \
  469. }
  470. __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(1)
  471. __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(2)
  472. __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(4)
  473. __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE(8)
  474. #undef __TBB_MACHINE_DEFINE_ATOMIC_SELECTOR_FETCH_STORE
  475. #endif /* __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE */
  476. #if __TBB_USE_GENERIC_DWORD_LOAD_STORE
  477. /*TODO: find a more elegant way to handle function names difference*/
  478. #if ! __TBB_USE_FENCED_ATOMICS
  479. /* This name forwarding is needed for generic implementation of
  480. * load8/store8 defined below (via macro) to pick the right CAS function*/
  481. #define __TBB_machine_cmpswp8full_fence __TBB_machine_cmpswp8
  482. #endif
  483. __TBB_MACHINE_DEFINE_LOAD8_GENERIC_FENCED(full_fence)
  484. __TBB_MACHINE_DEFINE_STORE8_GENERIC_FENCED(full_fence)
  485. #if ! __TBB_USE_FENCED_ATOMICS
  486. #undef __TBB_machine_cmpswp8full_fence
  487. #endif
  488. #define __TBB_machine_store8 tbb::internal::__TBB_machine_generic_store8full_fence
  489. #define __TBB_machine_load8 tbb::internal::__TBB_machine_generic_load8full_fence
  490. #endif /* __TBB_USE_GENERIC_DWORD_LOAD_STORE */
  491. #if __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE
  492. /** Fenced operations use volatile qualifier to prevent compiler from optimizing
  493. them out, and on on architectures with weak memory ordering to induce compiler
  494. to generate code with appropriate acquire/release semantics.
  495. On architectures like IA32, Intel64 (and likely and Sparc TSO) volatile has
  496. no effect on code gen, and consistency helpers serve as a compiler fence (the
  497. latter being true for IA64/gcc as well to fix a bug in some gcc versions). **/
  498. template <typename T, size_t S>
  499. struct machine_load_store {
  500. static T load_with_acquire ( const volatile T& location ) {
  501. T to_return = location;
  502. __TBB_acquire_consistency_helper();
  503. return to_return;
  504. }
  505. static void store_with_release ( volatile T &location, T value ) {
  506. __TBB_release_consistency_helper();
  507. location = value;
  508. }
  509. };
  510. //in general, plain load and store of 32bit compiler is not atomic for 64bit types
  511. #if __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS
  512. template <typename T>
  513. struct machine_load_store<T,8> {
  514. static T load_with_acquire ( const volatile T& location ) {
  515. return (T)__TBB_machine_load8( (const volatile void*)&location );
  516. }
  517. static void store_with_release ( volatile T& location, T value ) {
  518. __TBB_machine_store8( (volatile void*)&location, (int64_t)value );
  519. }
  520. };
  521. #endif /* __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS */
  522. #endif /* __TBB_USE_GENERIC_HALF_FENCED_LOAD_STORE */
  523. #if __TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE
  524. template <typename T, size_t S>
  525. struct machine_load_store_seq_cst {
  526. static T load ( const volatile T& location ) {
  527. __TBB_full_memory_fence();
  528. return machine_load_store<T,S>::load_with_acquire( location );
  529. }
  530. #if __TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE
  531. static void store ( volatile T &location, T value ) {
  532. atomic_selector<S>::fetch_store( (volatile void*)&location, (typename atomic_selector<S>::word)value );
  533. }
  534. #else /* !__TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE */
  535. static void store ( volatile T &location, T value ) {
  536. machine_load_store<T,S>::store_with_release( location, value );
  537. __TBB_full_memory_fence();
  538. }
  539. #endif /* !__TBB_USE_FETCHSTORE_AS_FULL_FENCED_STORE */
  540. };
  541. #if __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS
  542. /** The implementation does not use functions __TBB_machine_load8/store8 as they
  543. are not required to be sequentially consistent. **/
  544. template <typename T>
  545. struct machine_load_store_seq_cst<T,8> {
  546. static T load ( const volatile T& location ) {
  547. // Comparand and new value may be anything, they only must be equal, and
  548. // the value should have a low probability to be actually found in 'location'.
  549. const int64_t anyvalue = 2305843009213693951LL;
  550. return __TBB_machine_cmpswp8( (volatile void*)const_cast<volatile T*>(&location), anyvalue, anyvalue );
  551. }
  552. static void store ( volatile T &location, T value ) {
  553. int64_t result = (volatile int64_t&)location;
  554. while ( __TBB_machine_cmpswp8((volatile void*)&location, (int64_t)value, result) != result )
  555. result = (volatile int64_t&)location;
  556. }
  557. };
  558. #endif /* __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS */
  559. #endif /*__TBB_USE_GENERIC_SEQUENTIAL_CONSISTENCY_LOAD_STORE */
  560. #if __TBB_USE_GENERIC_RELAXED_LOAD_STORE
  561. // Relaxed operations add volatile qualifier to prevent compiler from optimizing them out.
  562. /** Volatile should not incur any additional cost on IA32, Intel64, and Sparc TSO
  563. architectures. However on architectures with weak memory ordering compiler may
  564. generate code with acquire/release semantics for operations on volatile data. **/
  565. template <typename T, size_t S>
  566. struct machine_load_store_relaxed {
  567. static inline T load ( const volatile T& location ) {
  568. return location;
  569. }
  570. static inline void store ( volatile T& location, T value ) {
  571. location = value;
  572. }
  573. };
  574. #if __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS
  575. template <typename T>
  576. struct machine_load_store_relaxed<T,8> {
  577. static inline T load ( const volatile T& location ) {
  578. return (T)__TBB_machine_load8( (const volatile void*)&location );
  579. }
  580. static inline void store ( volatile T& location, T value ) {
  581. __TBB_machine_store8( (volatile void*)&location, (int64_t)value );
  582. }
  583. };
  584. #endif /* __TBB_WORDSIZE==4 && __TBB_64BIT_ATOMICS */
  585. #endif /* __TBB_USE_GENERIC_RELAXED_LOAD_STORE */
  586. #undef __TBB_WORDSIZE //this macro is forbidden to use outside of atomic machinery
  587. template<typename T>
  588. inline T __TBB_load_with_acquire(const volatile T &location) {
  589. return machine_load_store<T,sizeof(T)>::load_with_acquire( location );
  590. }
  591. template<typename T, typename V>
  592. inline void __TBB_store_with_release(volatile T& location, V value) {
  593. machine_load_store<T,sizeof(T)>::store_with_release( location, T(value) );
  594. }
  595. //! Overload that exists solely to avoid /Wp64 warnings.
  596. inline void __TBB_store_with_release(volatile size_t& location, size_t value) {
  597. machine_load_store<size_t,sizeof(size_t)>::store_with_release( location, value );
  598. }
  599. template<typename T>
  600. inline T __TBB_load_full_fence(const volatile T &location) {
  601. return machine_load_store_seq_cst<T,sizeof(T)>::load( location );
  602. }
  603. template<typename T, typename V>
  604. inline void __TBB_store_full_fence(volatile T& location, V value) {
  605. machine_load_store_seq_cst<T,sizeof(T)>::store( location, T(value) );
  606. }
  607. //! Overload that exists solely to avoid /Wp64 warnings.
  608. inline void __TBB_store_full_fence(volatile size_t& location, size_t value) {
  609. machine_load_store_seq_cst<size_t,sizeof(size_t)>::store( location, value );
  610. }
  611. template<typename T>
  612. inline T __TBB_load_relaxed (const volatile T& location) {
  613. return machine_load_store_relaxed<T,sizeof(T)>::load( const_cast<T&>(location) );
  614. }
  615. template<typename T, typename V>
  616. inline void __TBB_store_relaxed ( volatile T& location, V value ) {
  617. machine_load_store_relaxed<T,sizeof(T)>::store( const_cast<T&>(location), T(value) );
  618. }
  619. //! Overload that exists solely to avoid /Wp64 warnings.
  620. inline void __TBB_store_relaxed ( volatile size_t& location, size_t value ) {
  621. machine_load_store_relaxed<size_t,sizeof(size_t)>::store( const_cast<size_t&>(location), value );
  622. }
  623. // Macro __TBB_TypeWithAlignmentAtLeastAsStrict(T) should be a type with alignment at least as
  624. // strict as type T. The type should have a trivial default constructor and destructor, so that
  625. // arrays of that type can be declared without initializers.
  626. // It is correct (but perhaps a waste of space) if __TBB_TypeWithAlignmentAtLeastAsStrict(T) expands
  627. // to a type bigger than T.
  628. // The default definition here works on machines where integers are naturally aligned and the
  629. // strictest alignment is 64.
  630. #ifndef __TBB_TypeWithAlignmentAtLeastAsStrict
  631. #if __TBB_ATTRIBUTE_ALIGNED_PRESENT
  632. #define __TBB_DefineTypeWithAlignment(PowerOf2) \
  633. struct __TBB_machine_type_with_alignment_##PowerOf2 { \
  634. uint32_t member[PowerOf2/sizeof(uint32_t)]; \
  635. } __attribute__((aligned(PowerOf2)));
  636. #define __TBB_alignof(T) __alignof__(T)
  637. #elif __TBB_DECLSPEC_ALIGN_PRESENT
  638. #define __TBB_DefineTypeWithAlignment(PowerOf2) \
  639. __declspec(align(PowerOf2)) \
  640. struct __TBB_machine_type_with_alignment_##PowerOf2 { \
  641. uint32_t member[PowerOf2/sizeof(uint32_t)]; \
  642. };
  643. #define __TBB_alignof(T) __alignof(T)
  644. #else /* A compiler with unknown syntax for data alignment */
  645. #error Must define __TBB_TypeWithAlignmentAtLeastAsStrict(T)
  646. #endif
  647. /* Now declare types aligned to useful powers of two */
  648. // TODO: Is __TBB_DefineTypeWithAlignment(8) needed on 32 bit platforms?
  649. __TBB_DefineTypeWithAlignment(16)
  650. __TBB_DefineTypeWithAlignment(32)
  651. __TBB_DefineTypeWithAlignment(64)
  652. typedef __TBB_machine_type_with_alignment_64 __TBB_machine_type_with_strictest_alignment;
  653. // Primary template is a declaration of incomplete type so that it fails with unknown alignments
  654. template<size_t N> struct type_with_alignment;
  655. // Specializations for allowed alignments
  656. template<> struct type_with_alignment<1> { char member; };
  657. template<> struct type_with_alignment<2> { uint16_t member; };
  658. template<> struct type_with_alignment<4> { uint32_t member; };
  659. template<> struct type_with_alignment<8> { uint64_t member; };
  660. template<> struct type_with_alignment<16> {__TBB_machine_type_with_alignment_16 member; };
  661. template<> struct type_with_alignment<32> {__TBB_machine_type_with_alignment_32 member; };
  662. template<> struct type_with_alignment<64> {__TBB_machine_type_with_alignment_64 member; };
  663. #if __TBB_ALIGNOF_NOT_INSTANTIATED_TYPES_BROKEN
  664. //! Work around for bug in GNU 3.2 and MSVC compilers.
  665. /** Bug is that compiler sometimes returns 0 for __alignof(T) when T has not yet been instantiated.
  666. The work-around forces instantiation by forcing computation of sizeof(T) before __alignof(T). */
  667. template<size_t Size, typename T>
  668. struct work_around_alignment_bug {
  669. static const size_t alignment = __TBB_alignof(T);
  670. };
  671. #define __TBB_TypeWithAlignmentAtLeastAsStrict(T) tbb::internal::type_with_alignment<tbb::internal::work_around_alignment_bug<sizeof(T),T>::alignment>
  672. #else
  673. #define __TBB_TypeWithAlignmentAtLeastAsStrict(T) tbb::internal::type_with_alignment<__TBB_alignof(T)>
  674. #endif /* __TBB_ALIGNOF_NOT_INSTANTIATED_TYPES_BROKEN */
  675. #endif /* __TBB_TypeWithAlignmentAtLeastAsStrict */
  676. // Template class here is to avoid instantiation of the static data for modules that don't use it
  677. template<typename T>
  678. struct reverse {
  679. static const T byte_table[256];
  680. };
  681. // An efficient implementation of the reverse function utilizes a 2^8 lookup table holding the bit-reversed
  682. // values of [0..2^8 - 1]. Those values can also be computed on the fly at a slightly higher cost.
  683. template<typename T>
  684. const T reverse<T>::byte_table[256] = {
  685. 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  686. 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  687. 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  688. 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  689. 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  690. 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  691. 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  692. 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  693. 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  694. 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  695. 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  696. 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  697. 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  698. 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  699. 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  700. 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  701. };
  702. } // namespace internal
  703. } // namespace tbb
  704. // Preserving access to legacy APIs
  705. using tbb::internal::__TBB_load_with_acquire;
  706. using tbb::internal::__TBB_store_with_release;
  707. // Mapping historically used names to the ones expected by atomic_load_store_traits
  708. #define __TBB_load_acquire __TBB_load_with_acquire
  709. #define __TBB_store_release __TBB_store_with_release
  710. #ifndef __TBB_Log2
  711. inline intptr_t __TBB_Log2( uintptr_t x ) {
  712. if( x==0 ) return -1;
  713. intptr_t result = 0;
  714. uintptr_t tmp;
  715. if( sizeof(x)>4 && (tmp = ((uint64_t)x)>>32)) { x=tmp; result += 32; }
  716. if( (tmp = x>>16) ) { x=tmp; result += 16; }
  717. if( (tmp = x>>8) ) { x=tmp; result += 8; }
  718. if( (tmp = x>>4) ) { x=tmp; result += 4; }
  719. if( (tmp = x>>2) ) { x=tmp; result += 2; }
  720. return (x&2)? result+1: result;
  721. }
  722. #endif
  723. #ifndef __TBB_AtomicOR
  724. inline void __TBB_AtomicOR( volatile void *operand, uintptr_t addend ) {
  725. tbb::internal::atomic_backoff b;
  726. for(;;) {
  727. uintptr_t tmp = *(volatile uintptr_t *)operand;
  728. uintptr_t result = __TBB_CompareAndSwapW(operand, tmp|addend, tmp);
  729. if( result==tmp ) break;
  730. b.pause();
  731. }
  732. }
  733. #endif
  734. #ifndef __TBB_AtomicAND
  735. inline void __TBB_AtomicAND( volatile void *operand, uintptr_t addend ) {
  736. tbb::internal::atomic_backoff b;
  737. for(;;) {
  738. uintptr_t tmp = *(volatile uintptr_t *)operand;
  739. uintptr_t result = __TBB_CompareAndSwapW(operand, tmp&addend, tmp);
  740. if( result==tmp ) break;
  741. b.pause();
  742. }
  743. }
  744. #endif
  745. #if __TBB_PREFETCHING
  746. #ifndef __TBB_cl_prefetch
  747. #error This platform does not define cache management primitives required for __TBB_PREFETCHING
  748. #endif
  749. #ifndef __TBB_cl_evict
  750. #define __TBB_cl_evict(p)
  751. #endif
  752. #endif
  753. #ifndef __TBB_Flag
  754. typedef unsigned char __TBB_Flag;
  755. #endif
  756. typedef __TBB_atomic __TBB_Flag __TBB_atomic_flag;
  757. #ifndef __TBB_TryLockByte
  758. inline bool __TBB_TryLockByte( __TBB_atomic_flag &flag ) {
  759. return __TBB_machine_cmpswp1(&flag,1,0)==0;
  760. }
  761. #endif
  762. #ifndef __TBB_LockByte
  763. inline __TBB_Flag __TBB_LockByte( __TBB_atomic_flag& flag ) {
  764. if ( !__TBB_TryLockByte(flag) ) {
  765. tbb::internal::atomic_backoff b;
  766. do {
  767. b.pause();
  768. } while ( !__TBB_TryLockByte(flag) );
  769. }
  770. return 0;
  771. }
  772. #endif
  773. #ifndef __TBB_UnlockByte
  774. #define __TBB_UnlockByte __TBB_store_with_release
  775. #endif
  776. #ifndef __TBB_ReverseByte
  777. inline unsigned char __TBB_ReverseByte(unsigned char src) {
  778. return tbb::internal::reverse<unsigned char>::byte_table[src];
  779. }
  780. #endif
  781. template<typename T>
  782. T __TBB_ReverseBits(T src) {
  783. T dst;
  784. unsigned char *original = (unsigned char *) &src;
  785. unsigned char *reversed = (unsigned char *) &dst;
  786. for( int i = sizeof(T)-1; i >= 0; i-- )
  787. reversed[i] = __TBB_ReverseByte( original[sizeof(T)-i-1] );
  788. return dst;
  789. }
  790. #endif /* __TBB_machine_H */