Imported and merged Boehm GC 7.0
[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
26 #include "../all_aligned_atomic_load_store.h"
27
28 /* Real X86 implementations, except for some old WinChips, appear       */
29 /* to enforce ordering between memory operations, EXCEPT that a later   */
30 /* read can pass earlier writes, presumably due to the visible          */
31 /* presence of store buffers.                                           */
32 /* We ignore both the WinChips, and the fact that the official specs    */
33 /* seem to be much weaker (and arguably too weak to be usable).         */
34
35 #include "../ordered_except_wr.h"
36
37 #include "../test_and_set_t_is_char.h"
38
39 #include <windows.h>
40         /* Seems like over-kill, but that's what MSDN recommends.       */
41         /* And apparently winbase.h is not always self-contained.       */
42
43 #if _MSC_VER < 1310
44
45 #define _InterlockedIncrement       InterlockedIncrement
46 #define _InterlockedDecrement       InterlockedDecrement
47 #define _InterlockedExchange        InterlockedExchange 
48 #define _InterlockedCompareExchange InterlockedCompareExchange
49
50 #else
51
52 #if _MSC_VER >= 1400
53 #include <intrin.h>
54
55 #pragma intrinsic (_ReadWriteBarrier)
56
57 #else
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 LONG __cdecl _InterlockedIncrement(LONG volatile *Addend);
63 LONG __cdecl _InterlockedDecrement(LONG volatile *Addend);
64 LONG __cdecl _InterlockedExchangeAdd(LONG volatile* Target, LONG Addend);
65 LONG __cdecl _InterlockedExchange(LONG volatile* Target, LONG Value);
66 LONG __cdecl _InterlockedCompareExchange(LONG volatile* Dest,
67                                          LONG Exchange, LONG Comp);
68
69 #ifdef __cplusplus
70 }
71 #endif
72 #endif /* _MSC_VER >= 1400 */
73
74 #pragma intrinsic (_InterlockedIncrement)
75 #pragma intrinsic (_InterlockedDecrement)
76 #pragma intrinsic (_InterlockedExchange)
77 #pragma intrinsic (_InterlockedExchangeAdd)
78 #pragma intrinsic (_InterlockedCompareExchange)
79
80 #endif /* _MSC_VER < 1310 */
81
82 /* As far as we can tell, the lfence and sfence instructions are not    */
83 /* currently needed or useful for cached memory accesses.               */
84
85 /* Unfortunately mfence doesn't exist everywhere.               */
86 /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is         */
87 /* probably a conservative test for it?                         */
88
89 #if defined(AO_USE_PENTIUM4_INSTRS)
90
91 AO_INLINE void
92 AO_nop_full()
93 {
94   __asm { mfence }
95 }
96
97 #define AO_HAVE_nop_full
98
99 #else
100
101 /* We could use the cpuid instruction.  But that seems to be slower     */
102 /* than the default implementation based on test_and_set_full.  Thus    */
103 /* we omit that bit of misinformation here.                             */
104
105 #endif
106
107 AO_INLINE AO_t
108 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
109 {
110   return _InterlockedExchangeAdd((LONG volatile*)p, (LONG)incr);
111 }
112
113 #define AO_HAVE_fetch_and_add_full
114
115 AO_INLINE AO_t
116 AO_fetch_and_add1_full (volatile AO_t *p)
117 {
118   return _InterlockedIncrement((LONG volatile *)p) - 1;
119 }
120
121 #define AO_HAVE_fetch_and_add1_full
122
123 AO_INLINE AO_t
124 AO_fetch_and_sub1_full (volatile AO_t *p)
125 {
126   return _InterlockedDecrement((LONG volatile *)p) + 1;
127 }
128
129 #define AO_HAVE_fetch_and_sub1_full
130
131 AO_INLINE AO_TS_VAL_t
132 AO_test_and_set_full(volatile AO_TS_t *addr)
133 {
134     __asm
135     {
136         mov     eax,AO_TS_SET           ;
137         mov     ebx,addr                ;
138         xchg    byte ptr [ebx],al       ;
139     }
140 }
141
142 #define AO_HAVE_test_and_set_full
143
144 #ifdef AO_ASSUME_WINDOWS98
145 /* Returns nonzero if the comparison succeeded. */
146 AO_INLINE int
147 AO_compare_and_swap_full(volatile AO_t *addr,
148                          AO_t old, AO_t new_val) 
149 {
150     return _InterlockedCompareExchange((LONG volatile *)addr,
151                                        (LONG)new_val, (LONG)old)
152            == (LONG)old;
153 }
154
155 #define AO_HAVE_compare_and_swap_full
156 #endif /* ASSUME_WINDOWS98 */
157
158 #ifdef _WIN64
159 #  error wrong architecture
160 #endif
161
162 #include "../ao_t_is_int.h"