Upgrade Boehm GC to 7.2alpha4.
[cacao.git] / src / mm / boehm-gc / libatomic_ops / 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 /* If AO_ASSUME_WINDOWS98 is defined, we assume Windows 98 or newer.    */
24 /* If AO_ASSUME_VISTA is defined, we assume Windows Server 2003, Vista  */
25 /* or later.                                                            */
26
27 #include "../all_aligned_atomic_load_store.h"
28
29 /* Real X86 implementations, except for some old WinChips, appear       */
30 /* to enforce ordering between memory operations, EXCEPT that a later   */
31 /* read can pass earlier writes, presumably due to the visible          */
32 /* presence of store buffers.                                           */
33 /* We ignore both the WinChips, and the fact that the official specs    */
34 /* seem to be much weaker (and arguably too weak to be usable).         */
35
36 #include "../ordered_except_wr.h"
37
38 #include "../test_and_set_t_is_char.h"
39
40 #ifndef AO_USE_INTERLOCKED_INTRINSICS
41   /* _Interlocked primitives (Inc, Dec, Xchg, Add) are always available */
42 # define AO_USE_INTERLOCKED_INTRINSICS
43 #endif
44 #include "common32_defs.h"
45
46 /* As far as we can tell, the lfence and sfence instructions are not    */
47 /* currently needed or useful for cached memory accesses.               */
48
49 /* Unfortunately mfence doesn't exist everywhere.               */
50 /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is         */
51 /* probably a conservative test for it?                         */
52
53 #if defined(AO_USE_PENTIUM4_INSTRS)
54
55 AO_INLINE void
56 AO_nop_full(void)
57 {
58   __asm { mfence }
59 }
60
61 #define AO_HAVE_nop_full
62
63 #else
64
65 /* We could use the cpuid instruction.  But that seems to be slower     */
66 /* than the default implementation based on test_and_set_full.  Thus    */
67 /* we omit that bit of misinformation here.                             */
68
69 #endif
70
71 AO_INLINE AO_TS_VAL_t
72 AO_test_and_set_full(volatile AO_TS_t *addr)
73 {
74     __asm
75     {
76         mov     eax,0xff                ; /* AO_TS_SET */
77         mov     ebx,addr                ;
78         xchg    byte ptr [ebx],al       ;
79     }
80     /* Ignore possible "missing return value" warning here. */
81 }
82
83 #define AO_HAVE_test_and_set_full
84
85 #ifdef _WIN64
86 #  error wrong architecture
87 #endif
88
89 #ifdef AO_ASSUME_VISTA
90 /* NEC LE-IT: whenever we run on a pentium class machine we have that
91  * certain function */
92
93 #include "../standard_ao_double_t.h"
94 #pragma intrinsic (_InterlockedCompareExchange64)
95 /* Returns nonzero if the comparison succeeded. */
96 AO_INLINE int
97 AO_compare_double_and_swap_double_full(volatile AO_double_t *addr,
98                                        AO_t old_val1, AO_t old_val2,
99                                        AO_t new_val1, AO_t new_val2)
100 {
101     __int64 oldv = (__int64)old_val1 | ((__int64)old_val2 << 32);
102     __int64 newv = (__int64)new_val1 | ((__int64)new_val2 << 32);
103     return _InterlockedCompareExchange64((__int64 volatile *)addr,
104                                        newv, oldv) == oldv;
105 }
106 #define AO_HAVE_compare_double_and_swap_double_full
107
108 #ifdef __cplusplus
109 AO_INLINE int
110 AO_double_compare_and_swap_full(volatile AO_double_t *addr,
111                                 AO_double_t old_val, AO_double_t new_val)
112 {
113     return _InterlockedCompareExchange64((__int64 volatile *)addr,
114                 new_val.AO_whole, old_val.AO_whole) == old_val.AO_whole;
115 }
116 #define AO_HAVE_double_compare_and_swap_full
117 #endif /* __cplusplus */
118 #endif /* AO_ASSUME_VISTA */
119
120 #include "../ao_t_is_int.h"