81c67a115932fb717e083fa5e3c26c30d5a83ca1
[cacao.git] / src / mm / boehm-gc / libatomic_ops-1.2 / src / atomic_ops / sysdeps / msftc / x86.h
1 /*
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
3  * 
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE. 
21  */
22
23 /* The following really assume we have a 486 or better.                 */
24 /* If ASSUME_WINDOWS98 is defined, we assume Windows 98 or newer.       */
25 /* If ASSUME_VISTA is defined, we assume Windows Server 2003, Vista     */
26 /* or later.                                                            */
27
28 #include "../all_aligned_atomic_load_store.h"
29
30 /* Real X86 implementations, except for some old WinChips, appear       */
31 /* to enforce ordering between memory operations, EXCEPT that a later   */
32 /* read can pass earlier writes, presumably due to the visible          */
33 /* presence of store buffers.                                           */
34 /* We ignore both the WinChips, and the fact that the official specs    */
35 /* seem to be much weaker (and arguably too weak to be usable).         */
36
37 #include "../ordered_except_wr.h"
38
39 #include "../test_and_set_t_is_char.h"
40
41 #include <windows.h>
42         /* Seems like over-kill, but that's what MSDN recommends.       */
43         /* And apparently winbase.h is not always self-contained.       */
44
45 #if _MSC_VER < 1310
46
47 #define _InterlockedIncrement       InterlockedIncrement
48 #define _InterlockedDecrement       InterlockedDecrement
49 #define _InterlockedExchange        InterlockedExchange 
50 #define _InterlockedExchangeAdd     InterlockedExchangeAdd
51 #define _InterlockedCompareExchange InterlockedCompareExchange
52
53 #else
54
55 #if _MSC_VER >= 1400
56 #include <intrin.h>
57
58 #pragma intrinsic (_ReadWriteBarrier)
59
60 #else
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 LONG __cdecl _InterlockedIncrement(LONG volatile *Addend);
66 LONG __cdecl _InterlockedDecrement(LONG volatile *Addend);
67 LONG __cdecl _InterlockedExchangeAdd(LONG volatile* Target, LONG Addend);
68 LONG __cdecl _InterlockedExchange(LONG volatile* Target, LONG Value);
69 LONG __cdecl _InterlockedCompareExchange(LONG volatile* Dest,
70                                          LONG Exchange, LONG Comp);
71
72 #ifdef __cplusplus
73 }
74 #endif
75 #endif /* _MSC_VER >= 1400 */
76
77 #pragma intrinsic (_InterlockedIncrement)
78 #pragma intrinsic (_InterlockedDecrement)
79 #pragma intrinsic (_InterlockedExchange)
80 #pragma intrinsic (_InterlockedExchangeAdd)
81 #pragma intrinsic (_InterlockedCompareExchange)
82
83 #endif /* _MSC_VER < 1310 */
84
85 /* As far as we can tell, the lfence and sfence instructions are not    */
86 /* currently needed or useful for cached memory accesses.               */
87
88 /* Unfortunately mfence doesn't exist everywhere.               */
89 /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is         */
90 /* probably a conservative test for it?                         */
91
92 #if defined(AO_USE_PENTIUM4_INSTRS)
93
94 AO_INLINE void
95 AO_nop_full()
96 {
97   __asm { mfence }
98 }
99
100 #define AO_HAVE_nop_full
101
102 #else
103
104 /* We could use the cpuid instruction.  But that seems to be slower     */
105 /* than the default implementation based on test_and_set_full.  Thus    */
106 /* we omit that bit of misinformation here.                             */
107
108 #endif
109
110 AO_INLINE AO_t
111 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
112 {
113   return _InterlockedExchangeAdd((LONG volatile*)p, (LONG)incr);
114 }
115
116 #define AO_HAVE_fetch_and_add_full
117
118 AO_INLINE AO_t
119 AO_fetch_and_add1_full (volatile AO_t *p)
120 {
121   return _InterlockedIncrement((LONG volatile *)p) - 1;
122 }
123
124 #define AO_HAVE_fetch_and_add1_full
125
126 AO_INLINE AO_t
127 AO_fetch_and_sub1_full (volatile AO_t *p)
128 {
129   return _InterlockedDecrement((LONG volatile *)p) + 1;
130 }
131
132 #define AO_HAVE_fetch_and_sub1_full
133
134 AO_INLINE AO_TS_VAL_t
135 AO_test_and_set_full(volatile AO_TS_t *addr)
136 {
137     __asm
138     {
139         mov     eax,AO_TS_SET           ;
140         mov     ebx,addr                ;
141         xchg    byte ptr [ebx],al       ;
142     }
143 }
144
145 #define AO_HAVE_test_and_set_full
146
147 #ifdef AO_ASSUME_WINDOWS98
148 /* Returns nonzero if the comparison succeeded. */
149 AO_INLINE int
150 AO_compare_and_swap_full(volatile AO_t *addr,
151                          AO_t old, AO_t new_val) 
152 {
153     return _InterlockedCompareExchange((LONG volatile *)addr,
154                                        (LONG)new_val, (LONG)old)
155            == (LONG)old;
156 }
157
158 #define AO_HAVE_compare_and_swap_full
159 #endif /* ASSUME_WINDOWS98 */
160
161 #ifdef _WIN64
162 #  error wrong architecture
163 #endif
164
165 #ifdef ASSUME_VISTA
166 /* NEC LE-IT: whenever we run on a pentium class machine we have that
167  * certain function */
168
169 #include "../standard_ao_double_t.h"
170 #pragma intrinsic (_InterlockedCompareExchange64)
171 /* Returns nonzero if the comparison succeeded. */
172 AO_INLINE int
173 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
174                                        AO_t old_val1, AO_t old_val2,
175                                        AO_t new_val1, AO_t new_val2) 
176 {
177     __int64 oldv = (__int64)old_val2 | ((__int64)old_val1 << 32);
178     __int64 newv = (__int64)new_val2 | ((__int64)new_val1 << 32);
179     return _InterlockedCompareExchange64((__int64 volatile *)addr,
180                                        newv, oldv) == oldv;
181 }
182 #define AO_HAVE_compare_double_and_swap_double_full
183
184 #ifdef __cplusplus
185 AO_INLINE int
186 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
187                                        AO_double_t old_val,
188                                        AO_double_t new_val) 
189 {
190     return _InterlockedCompareExchange64((__int64 volatile *)addr,
191                 new_val.AO_whole, old_val.AO_whole) == old_val.AO_whole;
192 }
193 #define AO_HAVE_double_compare_and_swap_full
194 #endif // __cplusplus
195 #endif /* ASSUME_VISTA */
196
197 #include "../ao_t_is_int.h"