]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/machine/windows_ia32.h
2.0.2: Updated to boost 1.48.
[casparcg] / tbb30_20100406oss / include / tbb / machine / windows_ia32.h
1 /*
2     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
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.
9
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.
14
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
18
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.
27 */
28
29 #ifndef __TBB_machine_H
30 #error Do not include this file directly; include tbb_machine.h instead
31 #endif
32
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()
39 #else
40 #error Unsupported compiler - need to define __TBB_release_consistency_helper to support it
41 #endif
42
43 inline void __TBB_rel_acq_fence() { __asm { __asm mfence } }
44
45 #define __TBB_WORDSIZE 4
46 #define __TBB_BIG_ENDIAN 0
47
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)
52 #endif
53
54 extern "C" {
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);
60 }
61
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();
67         return to_return;
68     }
69
70     static inline void store_with_release(volatile T &location, T value) {
71         __TBB_release_consistency_helper();
72         location = value;
73     }
74 };
75
76 template <typename T>
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);
80     }
81
82     static inline void store_with_release(T &location, T value) {
83         __TBB_machine_store8((volatile void *)&location,(__int64)value);
84     }
85 };
86
87 template<typename T>
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);
90 }
91
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);
95 }
96
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);
100
101
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))
104
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 ) { \
107     T result; \
108     volatile T *p = (T *)ptr; \
109     __TBB_release_consistency_helper(); \
110     __asm \
111     { \
112        __asm mov edx, p \
113        __asm mov C , value \
114        __asm mov A , comparand \
115        __asm lock cmpxchg [edx], C \
116        __asm mov result, A \
117     } \
118     __TBB_release_consistency_helper(); \
119     return result; \
120 } \
121 \
122 static inline T __TBB_machine_fetchadd##S ( volatile void * ptr, U addend ) { \
123     T result; \
124     volatile T *p = (T *)ptr; \
125     __TBB_release_consistency_helper(); \
126     __asm \
127     { \
128         __asm mov edx, p \
129         __asm mov A, addend \
130         __asm lock xadd [edx], A \
131         __asm mov result, A \
132     } \
133     __TBB_release_consistency_helper(); \
134     return result; \
135 }\
136 \
137 static inline T __TBB_machine_fetchstore##S ( volatile void * ptr, U value ) { \
138     T result; \
139     volatile T *p = (T *)ptr; \
140     __TBB_release_consistency_helper(); \
141     __asm \
142     { \
143         __asm mov edx, p \
144         __asm mov A, value \
145         __asm lock xchg [edx], A \
146         __asm mov result, A \
147     } \
148     __TBB_release_consistency_helper(); \
149     return result; \
150 }
151
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)
156
157 static inline __int32 __TBB_machine_lg( unsigned __int64 i ) {
158     unsigned __int32 j;
159     __asm
160     {
161         bsr eax, i
162         mov j, eax
163     }
164     return j;
165 }
166
167 static inline void __TBB_machine_OR( volatile void *operand, __int32 addend ) {
168    __asm 
169    {
170        mov eax, addend
171        mov edx, [operand]
172        lock or [edx], eax
173    }
174 }
175
176 static inline void __TBB_machine_AND( volatile void *operand, __int32 addend ) {
177    __asm 
178    {
179        mov eax, addend
180        mov edx, [operand]
181        lock and [edx], eax
182    }
183 }
184
185 static inline void __TBB_machine_pause (__int32 delay ) {
186     _asm 
187     {
188         mov eax, delay
189       L1: 
190         pause
191         add eax, -1
192         jne L1  
193     }
194     return;
195 }
196
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)
202
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)
208
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)
214
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)
220
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)
226
227 // Use generic definitions from tbb_machine.h
228 #undef __TBB_TryLockByte
229 #undef __TBB_LockByte
230
231 #if defined(_MSC_VER)&&_MSC_VER<1400
232     static inline void* __TBB_machine_get_current_teb () {
233         void* pteb;
234         __asm mov eax, fs:[0x18]
235         __asm mov pteb, eax
236         return pteb;
237     }
238 #endif
239
240 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
241     #pragma warning (pop)
242 #endif // warnings 4244, 4267 are back
243