2 * Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
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 */
28 #include "../all_aligned_atomic_load_store.h"
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). */
37 #include "../ordered_except_wr.h"
39 #include "../test_and_set_t_is_char.h"
42 /* Seems like over-kill, but that's what MSDN recommends. */
43 /* And apparently winbase.h is not always self-contained. */
47 #define _InterlockedIncrement InterlockedIncrement
48 #define _InterlockedDecrement InterlockedDecrement
49 #define _InterlockedExchange InterlockedExchange
50 #define _InterlockedExchangeAdd InterlockedExchangeAdd
51 #define _InterlockedCompareExchange InterlockedCompareExchange
58 #pragma intrinsic (_ReadWriteBarrier)
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);
75 #endif /* _MSC_VER >= 1400 */
77 #pragma intrinsic (_InterlockedIncrement)
78 #pragma intrinsic (_InterlockedDecrement)
79 #pragma intrinsic (_InterlockedExchange)
80 #pragma intrinsic (_InterlockedExchangeAdd)
81 #pragma intrinsic (_InterlockedCompareExchange)
83 #endif /* _MSC_VER < 1310 */
85 /* As far as we can tell, the lfence and sfence instructions are not */
86 /* currently needed or useful for cached memory accesses. */
88 /* Unfortunately mfence doesn't exist everywhere. */
89 /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is */
90 /* probably a conservative test for it? */
92 #if defined(AO_USE_PENTIUM4_INSTRS)
100 #define AO_HAVE_nop_full
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. */
111 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
113 return _InterlockedExchangeAdd((LONG volatile*)p, (LONG)incr);
116 #define AO_HAVE_fetch_and_add_full
119 AO_fetch_and_add1_full (volatile AO_t *p)
121 return _InterlockedIncrement((LONG volatile *)p) - 1;
124 #define AO_HAVE_fetch_and_add1_full
127 AO_fetch_and_sub1_full (volatile AO_t *p)
129 return _InterlockedDecrement((LONG volatile *)p) + 1;
132 #define AO_HAVE_fetch_and_sub1_full
134 AO_INLINE AO_TS_VAL_t
135 AO_test_and_set_full(volatile AO_TS_t *addr)
141 xchg byte ptr [ebx],al ;
145 #define AO_HAVE_test_and_set_full
147 #ifdef AO_ASSUME_WINDOWS98
148 /* Returns nonzero if the comparison succeeded. */
150 AO_compare_and_swap_full(volatile AO_t *addr,
151 AO_t old, AO_t new_val)
153 return _InterlockedCompareExchange((LONG volatile *)addr,
154 (LONG)new_val, (LONG)old)
158 #define AO_HAVE_compare_and_swap_full
159 #endif /* ASSUME_WINDOWS98 */
162 # error wrong architecture
166 /* NEC LE-IT: whenever we run on a pentium class machine we have that
167 * certain function */
169 #include "../standard_ao_double_t.h"
170 #pragma intrinsic (_InterlockedCompareExchange64)
171 /* Returns nonzero if the comparison succeeded. */
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)
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,
182 #define AO_HAVE_compare_double_and_swap_double_full
186 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
190 return _InterlockedCompareExchange64((__int64 volatile *)addr,
191 new_val.AO_whole, old_val.AO_whole) == old_val.AO_whole;
193 #define AO_HAVE_double_compare_and_swap_full
194 #endif // __cplusplus
195 #endif /* ASSUME_VISTA */
197 #include "../ao_t_is_int.h"