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. */
26 #include "../all_aligned_atomic_load_store.h"
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). */
35 #include "../ordered_except_wr.h"
38 FIXME: Need to reimplement testandset
40 #include "../test_and_set_t_is_char.h"
44 #include "../test_and_set_t_is_ao_t.h"
49 /* Seems like over-kill, but that's what MSDN recommends. */
50 /* And apparently winbase.h is not always self-contained. */
55 #pragma intrinsic (_ReadWriteBarrier)
61 LONGLONG __cdecl _InterlockedIncrement64(LONGLONG volatile *Addend);
62 LONGLONG __cdecl _InterlockedDecrement64(LONGLONG volatile *Addend);
63 LONGLONG __cdecl _InterlockedExchangeAdd64(LONGLONG volatile* Target,
65 LONGLONG __cdecl _InterlockedExchange64(LONGLONG volatile* Target,
67 LONGLONG __cdecl _InterlockedCompareExchange64(LONGLONG volatile* Dest,
75 #pragma intrinsic (_InterlockedIncrement64)
76 #pragma intrinsic (_InterlockedDecrement64)
77 #pragma intrinsic (_InterlockedExchange64)
78 #pragma intrinsic (_InterlockedExchangeAdd64)
79 #pragma intrinsic (_InterlockedCompareExchange64)
81 /* As far as we can tell, the lfence and sfence instructions are not */
82 /* currently needed or useful for cached memory accesses. */
84 /* Unfortunately mfence doesn't exist everywhere. */
85 /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is */
86 /* probably a conservative test for it? */
88 #if defined(AO_USE_PENTIUM4_INSTRS)
96 #define AO_HAVE_nop_full
100 /* We could use the cpuid instruction. But that seems to be slower */
101 /* than the default implementation based on test_and_set_full. Thus */
102 /* we omit that bit of misinformation here. */
107 AO_fetch_and_add_full (volatile AO_t *p, AO_t incr)
109 return _InterlockedExchangeAdd64((LONGLONG volatile *)p, (LONGLONG)incr);
112 #define AO_HAVE_fetch_and_add_full
115 AO_fetch_and_add1_full (volatile AO_t *p)
117 return _InterlockedIncrement64((LONGLONG volatile *)p) - 1;
120 #define AO_HAVE_fetch_and_add1_full
123 AO_fetch_and_sub1_full (volatile AO_t *p)
125 return _InterlockedDecrement64((LONGLONG volatile *)p) + 1;
128 #define AO_HAVE_fetch_and_sub1_full
131 AO_compare_and_swap_full(volatile AO_t *addr,
132 AO_t old, AO_t new_val)
134 return _InterlockedCompareExchange64((LONGLONG volatile *)addr,
135 (LONGLONG)new_val, (LONGLONG)old)
139 #define AO_HAVE_compare_and_swap_full
142 FIXME: (__asm not supported)
143 AO_INLINE AO_TS_VAL_t
144 AO_test_and_set_full(volatile AO_TS_t *addr)
150 xchg byte ptr [ebx],al ;
154 #define AO_HAVE_test_and_set_full
156 FIXME: (__asm not supported)
157 NEC LE-IT: Don't have a working Win64 environment here at the moment.
158 AO_compare_double_and_swap_double_full needs implementation for Win64
159 But there is no _InterlockedCompareExchange128 in the WinAPI, so we
160 need basically whats given below.
161 Also see gcc/x86_64.h for partial old opteron workaround:
163 #ifndef AO_CASDOUBLE_MISSING
166 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
167 AO_t old_val1, AO_t old_val2,
168 AO_t new_val1, AO_t new_val2)
173 mov rdx,QWORD PTR [old_val]
174 mov rax,QWORD PTR [old_val + 8]
175 mov rcx,QWORD PTR [new_val]
176 mov rbx,QWORD PTR [new_val + 8]
177 lock cmpxchg16b [addr]
182 #endif // AO_CASDOUBLE_MISSING
183 #define AO_HAVE_compare_double_and_swap_double_full