4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 * Inspired from FreeBSD src/sys/amd64/include/atomic.h
36 * Copyright (c) 1998 Doug Rabson
37 * All rights reserved.
40 #ifndef _RTE_ATOMIC_X86_64_H_
41 #define _RTE_ATOMIC_X86_64_H_
43 /*------------------------- 64 bit atomic operations -------------------------*/
45 #ifndef RTE_FORCE_INTRINSICS
47 rte_atomic64_cmpset(volatile uint64_t *dst, uint64_t exp, uint64_t src)
54 "cmpxchgq %[src], %[dst];"
56 : [res] "=a" (res), /* output */
58 : [src] "r" (src), /* input */
61 : "memory"); /* no-clobber list */
67 rte_atomic64_init(rte_atomic64_t *v)
73 rte_atomic64_read(rte_atomic64_t *v)
79 rte_atomic64_set(rte_atomic64_t *v, int64_t new_value)
85 rte_atomic64_add(rte_atomic64_t *v, int64_t inc)
90 : [cnt] "=m" (v->cnt) /* output */
91 : [inc] "ir" (inc), /* input */
97 rte_atomic64_sub(rte_atomic64_t *v, int64_t dec)
101 "subq %[dec], %[cnt]"
102 : [cnt] "=m" (v->cnt) /* output */
103 : [dec] "ir" (dec), /* input */
109 rte_atomic64_inc(rte_atomic64_t *v)
114 : [cnt] "=m" (v->cnt) /* output */
115 : "m" (v->cnt) /* input */
120 rte_atomic64_dec(rte_atomic64_t *v)
125 : [cnt] "=m" (v->cnt) /* output */
126 : "m" (v->cnt) /* input */
130 static inline int64_t
131 rte_atomic64_add_return(rte_atomic64_t *v, int64_t inc)
137 "xaddq %[prev], %[cnt]"
138 : [prev] "+r" (prev), /* output */
140 : "m" (v->cnt) /* input */
145 static inline int64_t
146 rte_atomic64_sub_return(rte_atomic64_t *v, int64_t dec)
148 return rte_atomic64_add_return(v, -dec);
151 static inline int rte_atomic64_inc_and_test(rte_atomic64_t *v)
159 : [cnt] "+m" (v->cnt), /* output */
166 static inline int rte_atomic64_dec_and_test(rte_atomic64_t *v)
174 : [cnt] "+m" (v->cnt), /* output */
180 static inline int rte_atomic64_test_and_set(rte_atomic64_t *v)
182 return rte_atomic64_cmpset((volatile uint64_t *)&v->cnt, 0, 1);
185 static inline void rte_atomic64_clear(rte_atomic64_t *v)
191 #endif /* _RTE_ATOMIC_X86_64_H_ */