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.

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