MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
x86-gcc.h
1 #ifndef ATOMIC_X86_GCC_INCLUDED
2 #define ATOMIC_X86_GCC_INCLUDED
3 
4 /* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 /*
20  XXX 64-bit atomic operations can be implemented using
21  cmpxchg8b, if necessary. Though I've heard that not all 64-bit
22  architectures support double-word (128-bit) cas.
23 */
24 
25 /*
26  No special support of 8 and 16 bit operations are implemented here
27  currently.
28 */
29 #undef MY_ATOMIC_HAS_8_AND_16
30 
31 #ifdef __x86_64__
32 # ifdef MY_ATOMIC_NO_XADD
33 # define MY_ATOMIC_MODE "gcc-amd64" LOCK_prefix "-no-xadd"
34 # else
35 # define MY_ATOMIC_MODE "gcc-amd64" LOCK_prefix
36 # endif
37 #else
38 # ifdef MY_ATOMIC_NO_XADD
39 # define MY_ATOMIC_MODE "gcc-x86" LOCK_prefix "-no-xadd"
40 # else
41 # define MY_ATOMIC_MODE "gcc-x86" LOCK_prefix
42 # endif
43 #endif
44 
45 /* fix -ansi errors while maintaining readability */
46 #ifndef asm
47 #define asm __asm__
48 #endif
49 
50 #ifndef MY_ATOMIC_NO_XADD
51 #define make_atomic_add_body(S) make_atomic_add_body ## S
52 #define make_atomic_cas_body(S) make_atomic_cas_body ## S
53 #endif
54 
55 #define make_atomic_add_body32 \
56  asm volatile (LOCK_prefix "; xadd %0, %1;" \
57  : "+r" (v), "=m" (*a) \
58  : "m" (*a) \
59  : "memory")
60 
61 #define make_atomic_cas_body32 \
62  __typeof__(*cmp) sav; \
63  asm volatile (LOCK_prefix "; cmpxchg %3, %0; setz %2;" \
64  : "=m" (*a), "=a" (sav), "=q" (ret) \
65  : "r" (set), "m" (*a), "a" (*cmp) \
66  : "memory"); \
67  if (!ret) \
68  *cmp= sav
69 
70 #ifdef __x86_64__
71 #define make_atomic_add_body64 make_atomic_add_body32
72 #define make_atomic_cas_body64 make_atomic_cas_body32
73 
74 #define make_atomic_fas_body(S) \
75  asm volatile ("xchg %0, %1;" \
76  : "+r" (v), "=m" (*a) \
77  : "m" (*a) \
78  : "memory")
79 
80 /*
81  Actually 32/64-bit reads/writes are always atomic on x86_64,
82  nonetheless issue memory barriers as appropriate.
83 */
84 #define make_atomic_load_body(S) \
85  /* Serialize prior load and store operations. */ \
86  asm volatile ("mfence" ::: "memory"); \
87  ret= *a; \
88  /* Prevent compiler from reordering instructions. */ \
89  asm volatile ("" ::: "memory")
90 #define make_atomic_store_body(S) \
91  asm volatile ("; xchg %0, %1;" \
92  : "=m" (*a), "+r" (v) \
93  : "m" (*a) \
94  : "memory")
95 
96 #else
97 /*
98  Use default implementations of 64-bit operations since we solved
99  the 64-bit problem on 32-bit platforms for CAS, no need to solve it
100  once more for ADD, LOAD, STORE and FAS as well.
101  Since we already added add32 support, we need to define add64
102  here, but we haven't defined fas, load and store at all, so
103  we can fallback on default implementations.
104 */
105 #define make_atomic_add_body64 \
106  int64 tmp=*a; \
107  while (!my_atomic_cas64(a, &tmp, tmp+v)) ; \
108  v=tmp;
109 
110 /*
111  On some platforms (e.g. Mac OS X and Solaris) the ebx register
112  is held as a pointer to the global offset table. Thus we're not
113  allowed to use the b-register on those platforms when compiling
114  PIC code, to avoid this we push ebx and pop ebx. The new value
115  is copied directly from memory to avoid problems with a implicit
116  manipulation of the stack pointer by the push.
117 
118  cmpxchg8b works on both 32-bit platforms and 64-bit platforms but
119  the code here is only used on 32-bit platforms, on 64-bit
120  platforms the much simpler make_atomic_cas_body32 will work
121  fine.
122 */
123 #define make_atomic_cas_body64 \
124  asm volatile ("push %%ebx;" \
125  "movl (%%ecx), %%ebx;" \
126  "movl 4(%%ecx), %%ecx;" \
127  LOCK_prefix "; cmpxchg8b %0;" \
128  "setz %2; pop %%ebx" \
129  : "=m" (*a), "+A" (*cmp), "=c" (ret) \
130  : "c" (&set), "m" (*a) \
131  : "memory", "esp")
132 #endif
133 
134 /*
135  The implementation of make_atomic_cas_body32 is adaptable to
136  the OS word size, so on 64-bit platforms it will automatically
137  adapt to 64-bits and so it will work also on 64-bit platforms
138 */
139 #define make_atomic_cas_bodyptr make_atomic_cas_body32
140 
141 #ifdef MY_ATOMIC_MODE_DUMMY
142 #define make_atomic_load_body(S) ret=*a
143 #define make_atomic_store_body(S) *a=v
144 #endif
145 #endif /* ATOMIC_X86_GCC_INCLUDED */