tbb_machine.h 40 KB

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