2 Copyright 2005-2010 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_machine_H
30 #error Do not include this file directly; include tbb_machine.h instead
33 #if defined(__INTEL_COMPILER)
34 #define __TBB_release_consistency_helper() __asm { __asm nop }
35 #elif _MSC_VER >= 1300
36 extern "C" void _ReadWriteBarrier();
37 #pragma intrinsic(_ReadWriteBarrier)
38 #define __TBB_release_consistency_helper() _ReadWriteBarrier()
40 #error Unsupported compiler - need to define __TBB_release_consistency_helper to support it
43 inline void __TBB_rel_acq_fence() { __asm { __asm mfence } }
45 #define __TBB_WORDSIZE 4
46 #define __TBB_BIG_ENDIAN 0
48 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
49 // Workaround for overzealous compiler warnings in /Wp64 mode
50 #pragma warning (push)
51 #pragma warning (disable: 4244 4267)
55 __int64 __TBB_EXPORTED_FUNC __TBB_machine_cmpswp8 (volatile void *ptr, __int64 value, __int64 comparand );
56 __int64 __TBB_EXPORTED_FUNC __TBB_machine_fetchadd8 (volatile void *ptr, __int64 addend );
57 __int64 __TBB_EXPORTED_FUNC __TBB_machine_fetchstore8 (volatile void *ptr, __int64 value );
58 void __TBB_EXPORTED_FUNC __TBB_machine_store8 (volatile void *ptr, __int64 value );
59 __int64 __TBB_EXPORTED_FUNC __TBB_machine_load8 (const volatile void *ptr);
62 template <typename T, size_t S>
63 struct __TBB_machine_load_store {
64 static inline T load_with_acquire(const volatile T& location) {
65 T to_return = location;
66 __TBB_release_consistency_helper();
70 static inline void store_with_release(volatile T &location, T value) {
71 __TBB_release_consistency_helper();
77 struct __TBB_machine_load_store<T,8> {
78 static inline T load_with_acquire(const volatile T& location) {
79 return __TBB_machine_load8((volatile void *)&location);
82 static inline void store_with_release(T &location, T value) {
83 __TBB_machine_store8((volatile void *)&location,(__int64)value);
88 inline T __TBB_machine_load_with_acquire(const volatile T &location) {
89 return __TBB_machine_load_store<T,sizeof(T)>::load_with_acquire(location);
92 template<typename T, typename V>
93 inline void __TBB_machine_store_with_release(T& location, V value) {
94 __TBB_machine_load_store<T,sizeof(T)>::store_with_release(location,value);
97 //! Overload that exists solely to avoid /Wp64 warnings.
98 inline void __TBB_machine_store_with_release(size_t& location, size_t value) {
99 __TBB_machine_load_store<size_t,sizeof(size_t)>::store_with_release(location,value);
102 #define __TBB_load_with_acquire(L) __TBB_machine_load_with_acquire((L))
103 #define __TBB_store_with_release(L,V) __TBB_machine_store_with_release((L),(V))
105 #define __TBB_DEFINE_ATOMICS(S,T,U,A,C) \
106 static inline T __TBB_machine_cmpswp##S ( volatile void * ptr, U value, U comparand ) { \
108 volatile T *p = (T *)ptr; \
109 __TBB_release_consistency_helper(); \
113 __asm mov C , value \
114 __asm mov A , comparand \
115 __asm lock cmpxchg [edx], C \
116 __asm mov result, A \
118 __TBB_release_consistency_helper(); \
122 static inline T __TBB_machine_fetchadd##S ( volatile void * ptr, U addend ) { \
124 volatile T *p = (T *)ptr; \
125 __TBB_release_consistency_helper(); \
129 __asm mov A, addend \
130 __asm lock xadd [edx], A \
131 __asm mov result, A \
133 __TBB_release_consistency_helper(); \
137 static inline T __TBB_machine_fetchstore##S ( volatile void * ptr, U value ) { \
139 volatile T *p = (T *)ptr; \
140 __TBB_release_consistency_helper(); \
145 __asm lock xchg [edx], A \
146 __asm mov result, A \
148 __TBB_release_consistency_helper(); \
152 __TBB_DEFINE_ATOMICS(1, __int8, __int8, al, cl)
153 __TBB_DEFINE_ATOMICS(2, __int16, __int16, ax, cx)
154 __TBB_DEFINE_ATOMICS(4, __int32, __int32, eax, ecx)
155 __TBB_DEFINE_ATOMICS(W, ptrdiff_t, ptrdiff_t, eax, ecx)
157 static inline __int32 __TBB_machine_lg( unsigned __int64 i ) {
167 static inline void __TBB_machine_OR( volatile void *operand, __int32 addend ) {
176 static inline void __TBB_machine_AND( volatile void *operand, __int32 addend ) {
185 static inline void __TBB_machine_pause (__int32 delay ) {
197 #define __TBB_CompareAndSwap1(P,V,C) __TBB_machine_cmpswp1(P,V,C)
198 #define __TBB_CompareAndSwap2(P,V,C) __TBB_machine_cmpswp2(P,V,C)
199 #define __TBB_CompareAndSwap4(P,V,C) __TBB_machine_cmpswp4(P,V,C)
200 #define __TBB_CompareAndSwap8(P,V,C) __TBB_machine_cmpswp8(P,V,C)
201 #define __TBB_CompareAndSwapW(P,V,C) __TBB_machine_cmpswpW(P,V,C)
203 #define __TBB_FetchAndAdd1(P,V) __TBB_machine_fetchadd1(P,V)
204 #define __TBB_FetchAndAdd2(P,V) __TBB_machine_fetchadd2(P,V)
205 #define __TBB_FetchAndAdd4(P,V) __TBB_machine_fetchadd4(P,V)
206 #define __TBB_FetchAndAdd8(P,V) __TBB_machine_fetchadd8(P,V)
207 #define __TBB_FetchAndAddW(P,V) __TBB_machine_fetchaddW(P,V)
209 #define __TBB_FetchAndStore1(P,V) __TBB_machine_fetchstore1(P,V)
210 #define __TBB_FetchAndStore2(P,V) __TBB_machine_fetchstore2(P,V)
211 #define __TBB_FetchAndStore4(P,V) __TBB_machine_fetchstore4(P,V)
212 #define __TBB_FetchAndStore8(P,V) __TBB_machine_fetchstore8(P,V)
213 #define __TBB_FetchAndStoreW(P,V) __TBB_machine_fetchstoreW(P,V)
215 // Should define this:
216 #define __TBB_Store8(P,V) __TBB_machine_store8(P,V)
217 #define __TBB_Load8(P) __TBB_machine_load8(P)
218 #define __TBB_AtomicOR(P,V) __TBB_machine_OR(P,V)
219 #define __TBB_AtomicAND(P,V) __TBB_machine_AND(P,V)
221 // Definition of other functions
222 extern "C" __declspec(dllimport) int __stdcall SwitchToThread( void );
223 #define __TBB_Yield() SwitchToThread()
224 #define __TBB_Pause(V) __TBB_machine_pause(V)
225 #define __TBB_Log2(V) __TBB_machine_lg(V)
227 // Use generic definitions from tbb_machine.h
228 #undef __TBB_TryLockByte
229 #undef __TBB_LockByte
231 #if defined(_MSC_VER)&&_MSC_VER<1400
232 static inline void* __TBB_machine_get_current_teb () {
234 __asm mov eax, fs:[0x18]
240 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
241 #pragma warning (pop)
242 #endif // warnings 4244, 4267 are back