remove extra parentheses in return statement
[dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_atomic.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_ATOMIC_X86_H_
35 #define _RTE_ATOMIC_X86_H_
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #include <emmintrin.h>
42 #include "generic/rte_atomic.h"
43
44 #if RTE_MAX_LCORE == 1
45 #define MPLOCKED                        /**< No need to insert MP lock prefix. */
46 #else
47 #define MPLOCKED        "lock ; "       /**< Insert MP lock prefix. */
48 #endif
49
50 #define rte_mb() _mm_mfence()
51
52 #define rte_wmb() _mm_sfence()
53
54 #define rte_rmb() _mm_lfence()
55
56 #define rte_smp_mb() rte_mb()
57
58 #define rte_smp_wmb() rte_compiler_barrier()
59
60 #define rte_smp_rmb() rte_compiler_barrier()
61
62 /*------------------------- 16 bit atomic operations -------------------------*/
63
64 #ifndef RTE_FORCE_INTRINSICS
65 static inline int
66 rte_atomic16_cmpset(volatile uint16_t *dst, uint16_t exp, uint16_t src)
67 {
68         uint8_t res;
69
70         asm volatile(
71                         MPLOCKED
72                         "cmpxchgw %[src], %[dst];"
73                         "sete %[res];"
74                         : [res] "=a" (res),     /* output */
75                           [dst] "=m" (*dst)
76                         : [src] "r" (src),      /* input */
77                           "a" (exp),
78                           "m" (*dst)
79                         : "memory");            /* no-clobber list */
80         return res;
81 }
82
83 static inline int rte_atomic16_test_and_set(rte_atomic16_t *v)
84 {
85         return rte_atomic16_cmpset((volatile uint16_t *)&v->cnt, 0, 1);
86 }
87
88 static inline void
89 rte_atomic16_inc(rte_atomic16_t *v)
90 {
91         asm volatile(
92                         MPLOCKED
93                         "incw %[cnt]"
94                         : [cnt] "=m" (v->cnt)   /* output */
95                         : "m" (v->cnt)          /* input */
96                         );
97 }
98
99 static inline void
100 rte_atomic16_dec(rte_atomic16_t *v)
101 {
102         asm volatile(
103                         MPLOCKED
104                         "decw %[cnt]"
105                         : [cnt] "=m" (v->cnt)   /* output */
106                         : "m" (v->cnt)          /* input */
107                         );
108 }
109
110 static inline int rte_atomic16_inc_and_test(rte_atomic16_t *v)
111 {
112         uint8_t ret;
113
114         asm volatile(
115                         MPLOCKED
116                         "incw %[cnt] ; "
117                         "sete %[ret]"
118                         : [cnt] "+m" (v->cnt),  /* output */
119                           [ret] "=qm" (ret)
120                         );
121         return ret != 0;
122 }
123
124 static inline int rte_atomic16_dec_and_test(rte_atomic16_t *v)
125 {
126         uint8_t ret;
127
128         asm volatile(MPLOCKED
129                         "decw %[cnt] ; "
130                         "sete %[ret]"
131                         : [cnt] "+m" (v->cnt),  /* output */
132                           [ret] "=qm" (ret)
133                         );
134         return ret != 0;
135 }
136
137 /*------------------------- 32 bit atomic operations -------------------------*/
138
139 static inline int
140 rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
141 {
142         uint8_t res;
143
144         asm volatile(
145                         MPLOCKED
146                         "cmpxchgl %[src], %[dst];"
147                         "sete %[res];"
148                         : [res] "=a" (res),     /* output */
149                           [dst] "=m" (*dst)
150                         : [src] "r" (src),      /* input */
151                           "a" (exp),
152                           "m" (*dst)
153                         : "memory");            /* no-clobber list */
154         return res;
155 }
156
157 static inline int rte_atomic32_test_and_set(rte_atomic32_t *v)
158 {
159         return rte_atomic32_cmpset((volatile uint32_t *)&v->cnt, 0, 1);
160 }
161
162 static inline void
163 rte_atomic32_inc(rte_atomic32_t *v)
164 {
165         asm volatile(
166                         MPLOCKED
167                         "incl %[cnt]"
168                         : [cnt] "=m" (v->cnt)   /* output */
169                         : "m" (v->cnt)          /* input */
170                         );
171 }
172
173 static inline void
174 rte_atomic32_dec(rte_atomic32_t *v)
175 {
176         asm volatile(
177                         MPLOCKED
178                         "decl %[cnt]"
179                         : [cnt] "=m" (v->cnt)   /* output */
180                         : "m" (v->cnt)          /* input */
181                         );
182 }
183
184 static inline int rte_atomic32_inc_and_test(rte_atomic32_t *v)
185 {
186         uint8_t ret;
187
188         asm volatile(
189                         MPLOCKED
190                         "incl %[cnt] ; "
191                         "sete %[ret]"
192                         : [cnt] "+m" (v->cnt),  /* output */
193                           [ret] "=qm" (ret)
194                         );
195         return ret != 0;
196 }
197
198 static inline int rte_atomic32_dec_and_test(rte_atomic32_t *v)
199 {
200         uint8_t ret;
201
202         asm volatile(MPLOCKED
203                         "decl %[cnt] ; "
204                         "sete %[ret]"
205                         : [cnt] "+m" (v->cnt),  /* output */
206                           [ret] "=qm" (ret)
207                         );
208         return ret != 0;
209 }
210 #endif
211
212 #ifdef RTE_ARCH_I686
213 #include "rte_atomic_32.h"
214 #else
215 #include "rte_atomic_64.h"
216 #endif
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif /* _RTE_ATOMIC_X86_H_ */