Home | History | Annotate | Line # | Download | only in include
atomic.h revision 1.3
      1  1.3   scole /*	$NetBSD: atomic.h,v 1.3 2016/08/09 13:45:45 scole Exp $	*/
      2  1.1  cherry 
      3  1.1  cherry /*-
      4  1.1  cherry  * Copyright (c) 1998 Doug Rabson
      5  1.1  cherry  * All rights reserved.
      6  1.1  cherry  *
      7  1.1  cherry  * Redistribution and use in source and binary forms, with or without
      8  1.1  cherry  * modification, are permitted provided that the following conditions
      9  1.1  cherry  * are met:
     10  1.1  cherry  * 1. Redistributions of source code must retain the above copyright
     11  1.1  cherry  *    notice, this list of conditions and the following disclaimer.
     12  1.1  cherry  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  cherry  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  cherry  *    documentation and/or other materials provided with the distribution.
     15  1.1  cherry  *
     16  1.1  cherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  cherry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  cherry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  cherry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  cherry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  cherry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  cherry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  cherry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  cherry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  cherry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  cherry  * SUCH DAMAGE.
     27  1.1  cherry  *
     28  1.2   scole  * $FreeBSD: releng/10.1/sys/ia64/include/atomic.h 262004 2014-02-16 23:08:21Z marcel $
     29  1.1  cherry  */
     30  1.1  cherry 
     31  1.3   scole #ifndef _IA64_ATOMIC_H_
     32  1.3   scole #define	_IA64_ATOMIC_H_
     33  1.2   scole 
     34  1.1  cherry /*
     35  1.1  cherry  * Various simple arithmetic on memory which is atomic in the presence
     36  1.1  cherry  * of interrupts and SMP safe.
     37  1.1  cherry  */
     38  1.1  cherry 
     39  1.1  cherry /*
     40  1.1  cherry  * Everything is built out of cmpxchg.
     41  1.1  cherry  */
     42  1.1  cherry #define	IA64_CMPXCHG(sz, sem, p, cmpval, newval, ret)			\
     43  1.1  cherry 	__asm __volatile (						\
     44  1.1  cherry 		"mov ar.ccv=%2;;\n\t"					\
     45  1.1  cherry 		"cmpxchg" #sz "." #sem " %0=%4,%3,ar.ccv\n\t"		\
     46  1.1  cherry 		: "=r" (ret), "=m" (*p)					\
     47  1.2   scole 		: "r" ((uint64_t)cmpval), "r" (newval), "m" (*p)	\
     48  1.1  cherry 		: "memory")
     49  1.1  cherry 
     50  1.1  cherry /*
     51  1.1  cherry  * Some common forms of cmpxch.
     52  1.1  cherry  */
     53  1.1  cherry static __inline uint32_t
     54  1.1  cherry ia64_cmpxchg_acq_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
     55  1.1  cherry {
     56  1.1  cherry 	uint32_t ret;
     57  1.1  cherry 	IA64_CMPXCHG(4, acq, p, cmpval, newval, ret);
     58  1.1  cherry 	return (ret);
     59  1.1  cherry }
     60  1.1  cherry 
     61  1.1  cherry static __inline uint32_t
     62  1.1  cherry ia64_cmpxchg_rel_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
     63  1.1  cherry {
     64  1.1  cherry 	uint32_t ret;
     65  1.1  cherry 	IA64_CMPXCHG(4, rel, p, cmpval, newval, ret);
     66  1.1  cherry 	return (ret);
     67  1.1  cherry }
     68  1.1  cherry 
     69  1.1  cherry static __inline uint64_t
     70  1.1  cherry ia64_cmpxchg_acq_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
     71  1.1  cherry {
     72  1.1  cherry 	uint64_t ret;
     73  1.1  cherry 	IA64_CMPXCHG(8, acq, p, cmpval, newval, ret);
     74  1.1  cherry 	return (ret);
     75  1.1  cherry }
     76  1.1  cherry 
     77  1.1  cherry static __inline uint64_t
     78  1.1  cherry ia64_cmpxchg_rel_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
     79  1.1  cherry {
     80  1.1  cherry 	uint64_t ret;
     81  1.1  cherry 	IA64_CMPXCHG(8, rel, p, cmpval, newval, ret);
     82  1.1  cherry 	return (ret);
     83  1.1  cherry }
     84  1.1  cherry 
     85  1.1  cherry #define	ATOMIC_STORE_LOAD(type, width, size)				\
     86  1.1  cherry 	static __inline uint##width##_t					\
     87  1.1  cherry 	ia64_ld_acq_##width(volatile uint##width##_t* p)		\
     88  1.1  cherry 	{								\
     89  1.1  cherry 		uint##width##_t v;					\
     90  1.1  cherry 		__asm __volatile ("ld" size ".acq %0=%1" : "=r" (v)	\
     91  1.1  cherry 		    : "m" (*p) : "memory");				\
     92  1.1  cherry 		return (v);						\
     93  1.1  cherry 	}								\
     94  1.1  cherry 									\
     95  1.1  cherry 	static __inline uint##width##_t					\
     96  1.1  cherry 	atomic_load_acq_##width(volatile uint##width##_t* p)		\
     97  1.1  cherry 	{								\
     98  1.1  cherry 		uint##width##_t v;					\
     99  1.1  cherry 		__asm __volatile ("ld" size ".acq %0=%1" : "=r" (v)	\
    100  1.1  cherry 		    : "m" (*p) : "memory");				\
    101  1.1  cherry 		return (v);						\
    102  1.1  cherry 	}								\
    103  1.1  cherry 									\
    104  1.1  cherry 	static __inline uint##width##_t					\
    105  1.1  cherry 	atomic_load_acq_##type(volatile uint##width##_t* p)		\
    106  1.1  cherry 	{								\
    107  1.1  cherry 		uint##width##_t v;					\
    108  1.1  cherry 		__asm __volatile ("ld" size ".acq %0=%1" : "=r" (v)	\
    109  1.1  cherry 		    : "m" (*p) : "memory");				\
    110  1.1  cherry 		return (v);						\
    111  1.1  cherry 	}								\
    112  1.1  cherry 								       	\
    113  1.1  cherry 	static __inline void						\
    114  1.1  cherry 	ia64_st_rel_##width(volatile uint##width##_t* p, uint##width##_t v) \
    115  1.1  cherry 	{								\
    116  1.1  cherry 		__asm __volatile ("st" size ".rel %0=%1" : "=m" (*p)	\
    117  1.1  cherry 		    : "r" (v) : "memory");				\
    118  1.1  cherry 	}								\
    119  1.1  cherry 									\
    120  1.1  cherry 	static __inline void						\
    121  1.1  cherry 	atomic_store_rel_##width(volatile uint##width##_t* p,		\
    122  1.1  cherry 	    uint##width##_t v)						\
    123  1.1  cherry 	{								\
    124  1.1  cherry 		__asm __volatile ("st" size ".rel %0=%1" : "=m" (*p)	\
    125  1.1  cherry 		    : "r" (v) : "memory");				\
    126  1.1  cherry 	}								\
    127  1.1  cherry 									\
    128  1.1  cherry 	static __inline void						\
    129  1.1  cherry 	atomic_store_rel_##type(volatile uint##width##_t* p,		\
    130  1.1  cherry 	    uint##width##_t v)						\
    131  1.1  cherry 	{								\
    132  1.1  cherry 		__asm __volatile ("st" size ".rel %0=%1" : "=m" (*p)	\
    133  1.1  cherry 		    : "r" (v) : "memory");				\
    134  1.1  cherry 	}
    135  1.1  cherry 
    136  1.1  cherry ATOMIC_STORE_LOAD(char,	 8,  "1")
    137  1.1  cherry ATOMIC_STORE_LOAD(short, 16, "2")
    138  1.1  cherry ATOMIC_STORE_LOAD(int,	 32, "4")
    139  1.1  cherry ATOMIC_STORE_LOAD(long,	 64, "8")
    140  1.1  cherry 
    141  1.1  cherry #undef ATOMIC_STORE_LOAD
    142  1.1  cherry 
    143  1.2   scole #define	atomic_load_acq_ptr(p)		\
    144  1.2   scole     ((void *)atomic_load_acq_64((volatile uint64_t *)p))
    145  1.2   scole 
    146  1.2   scole #define	atomic_store_rel_ptr(p, v)	\
    147  1.2   scole     atomic_store_rel_64((volatile uint64_t *)p, (uint64_t)v)
    148  1.1  cherry 
    149  1.1  cherry #define	IA64_ATOMIC(sz, type, name, width, op)				\
    150  1.1  cherry 	static __inline type						\
    151  1.1  cherry 	atomic_##name##_acq_##width(volatile type *p, type v)		\
    152  1.1  cherry 	{								\
    153  1.1  cherry 		type old, ret;						\
    154  1.1  cherry 		do {							\
    155  1.1  cherry 			old = *p;					\
    156  1.1  cherry 			IA64_CMPXCHG(sz, acq, p, old, old op v, ret);	\
    157  1.1  cherry 		} while (ret != old);					\
    158  1.1  cherry 		return (old);						\
    159  1.1  cherry 	}								\
    160  1.1  cherry 									\
    161  1.1  cherry 	static __inline type						\
    162  1.1  cherry 	atomic_##name##_rel_##width(volatile type *p, type v)		\
    163  1.1  cherry 	{								\
    164  1.1  cherry 		type old, ret;						\
    165  1.1  cherry 		do {							\
    166  1.1  cherry 			old = *p;					\
    167  1.1  cherry 			IA64_CMPXCHG(sz, rel, p, old, old op v, ret);	\
    168  1.1  cherry 		} while (ret != old);					\
    169  1.1  cherry 		return (old);						\
    170  1.1  cherry 	}
    171  1.1  cherry 
    172  1.1  cherry IA64_ATOMIC(1, uint8_t,	 set, 8,  |)
    173  1.1  cherry IA64_ATOMIC(2, uint16_t, set, 16, |)
    174  1.1  cherry IA64_ATOMIC(4, uint32_t, set, 32, |)
    175  1.1  cherry IA64_ATOMIC(8, uint64_t, set, 64, |)
    176  1.1  cherry 
    177  1.1  cherry IA64_ATOMIC(1, uint8_t,  clear,	8,  &~)
    178  1.1  cherry IA64_ATOMIC(2, uint16_t, clear,	16, &~)
    179  1.1  cherry IA64_ATOMIC(4, uint32_t, clear,	32, &~)
    180  1.1  cherry IA64_ATOMIC(8, uint64_t, clear,	64, &~)
    181  1.1  cherry 
    182  1.1  cherry IA64_ATOMIC(1, uint8_t,  add, 8,  +)
    183  1.1  cherry IA64_ATOMIC(2, uint16_t, add, 16, +)
    184  1.1  cherry IA64_ATOMIC(4, uint32_t, add, 32, +)
    185  1.1  cherry IA64_ATOMIC(8, uint64_t, add, 64, +)
    186  1.1  cherry 
    187  1.1  cherry IA64_ATOMIC(1, uint8_t,  subtract, 8,  -)
    188  1.1  cherry IA64_ATOMIC(2, uint16_t, subtract, 16, -)
    189  1.1  cherry IA64_ATOMIC(4, uint32_t, subtract, 32, -)
    190  1.1  cherry IA64_ATOMIC(8, uint64_t, subtract, 64, -)
    191  1.1  cherry 
    192  1.1  cherry #undef IA64_ATOMIC
    193  1.1  cherry 
    194  1.1  cherry #define	atomic_set_8			atomic_set_acq_8
    195  1.1  cherry #define	atomic_clear_8			atomic_clear_acq_8
    196  1.1  cherry #define	atomic_add_8			atomic_add_acq_8
    197  1.1  cherry #define	atomic_subtract_8		atomic_subtract_acq_8
    198  1.1  cherry 
    199  1.1  cherry #define	atomic_set_16			atomic_set_acq_16
    200  1.1  cherry #define	atomic_clear_16			atomic_clear_acq_16
    201  1.1  cherry #define	atomic_add_16			atomic_add_acq_16
    202  1.1  cherry #define	atomic_subtract_16		atomic_subtract_acq_16
    203  1.1  cherry 
    204  1.1  cherry #define	atomic_set_32			atomic_set_acq_32
    205  1.1  cherry #define	atomic_clear_32			atomic_clear_acq_32
    206  1.1  cherry #define	atomic_add_32			atomic_add_acq_32
    207  1.1  cherry #define	atomic_subtract_32		atomic_subtract_acq_32
    208  1.1  cherry 
    209  1.1  cherry #define	atomic_set_64			atomic_set_acq_64
    210  1.1  cherry #define	atomic_clear_64			atomic_clear_acq_64
    211  1.1  cherry #define	atomic_add_64			atomic_add_acq_64
    212  1.1  cherry #define	atomic_subtract_64		atomic_subtract_acq_64
    213  1.1  cherry 
    214  1.1  cherry #define	atomic_set_char			atomic_set_8
    215  1.1  cherry #define	atomic_clear_char		atomic_clear_8
    216  1.1  cherry #define	atomic_add_char			atomic_add_8
    217  1.1  cherry #define	atomic_subtract_char		atomic_subtract_8
    218  1.1  cherry #define	atomic_set_acq_char		atomic_set_acq_8
    219  1.1  cherry #define	atomic_clear_acq_char		atomic_clear_acq_8
    220  1.1  cherry #define	atomic_add_acq_char		atomic_add_acq_8
    221  1.1  cherry #define	atomic_subtract_acq_char	atomic_subtract_acq_8
    222  1.1  cherry #define	atomic_set_rel_char		atomic_set_rel_8
    223  1.1  cherry #define	atomic_clear_rel_char		atomic_clear_rel_8
    224  1.1  cherry #define	atomic_add_rel_char		atomic_add_rel_8
    225  1.1  cherry #define	atomic_subtract_rel_char	atomic_subtract_rel_8
    226  1.1  cherry 
    227  1.1  cherry #define	atomic_set_short		atomic_set_16
    228  1.1  cherry #define	atomic_clear_short		atomic_clear_16
    229  1.1  cherry #define	atomic_add_short		atomic_add_16
    230  1.1  cherry #define	atomic_subtract_short		atomic_subtract_16
    231  1.1  cherry #define	atomic_set_acq_short		atomic_set_acq_16
    232  1.1  cherry #define	atomic_clear_acq_short		atomic_clear_acq_16
    233  1.1  cherry #define	atomic_add_acq_short		atomic_add_acq_16
    234  1.1  cherry #define	atomic_subtract_acq_short	atomic_subtract_acq_16
    235  1.1  cherry #define	atomic_set_rel_short		atomic_set_rel_16
    236  1.1  cherry #define	atomic_clear_rel_short		atomic_clear_rel_16
    237  1.1  cherry #define	atomic_add_rel_short		atomic_add_rel_16
    238  1.1  cherry #define	atomic_subtract_rel_short	atomic_subtract_rel_16
    239  1.1  cherry 
    240  1.1  cherry #define	atomic_set_int			atomic_set_32
    241  1.1  cherry #define	atomic_clear_int		atomic_clear_32
    242  1.1  cherry #define	atomic_add_int			atomic_add_32
    243  1.1  cherry #define	atomic_subtract_int		atomic_subtract_32
    244  1.1  cherry #define	atomic_set_acq_int		atomic_set_acq_32
    245  1.1  cherry #define	atomic_clear_acq_int		atomic_clear_acq_32
    246  1.1  cherry #define	atomic_add_acq_int		atomic_add_acq_32
    247  1.1  cherry #define	atomic_subtract_acq_int		atomic_subtract_acq_32
    248  1.1  cherry #define	atomic_set_rel_int		atomic_set_rel_32
    249  1.1  cherry #define	atomic_clear_rel_int		atomic_clear_rel_32
    250  1.1  cherry #define	atomic_add_rel_int		atomic_add_rel_32
    251  1.1  cherry #define	atomic_subtract_rel_int		atomic_subtract_rel_32
    252  1.1  cherry 
    253  1.1  cherry #define	atomic_set_long			atomic_set_64
    254  1.1  cherry #define	atomic_clear_long		atomic_clear_64
    255  1.1  cherry #define	atomic_add_long			atomic_add_64
    256  1.1  cherry #define	atomic_subtract_long		atomic_subtract_64
    257  1.1  cherry #define	atomic_set_acq_long		atomic_set_acq_64
    258  1.1  cherry #define	atomic_clear_acq_long		atomic_clear_acq_64
    259  1.1  cherry #define	atomic_add_acq_long		atomic_add_acq_64
    260  1.1  cherry #define	atomic_subtract_acq_long	atomic_subtract_acq_64
    261  1.1  cherry #define	atomic_set_rel_long		atomic_set_rel_64
    262  1.1  cherry #define	atomic_clear_rel_long		atomic_clear_rel_64
    263  1.1  cherry #define	atomic_add_rel_long		atomic_add_rel_64
    264  1.1  cherry #define	atomic_subtract_rel_long	atomic_subtract_rel_64
    265  1.1  cherry 
    266  1.2   scole /* XXX Needs casting. */
    267  1.1  cherry #define	atomic_set_ptr			atomic_set_64
    268  1.1  cherry #define	atomic_clear_ptr		atomic_clear_64
    269  1.1  cherry #define	atomic_add_ptr			atomic_add_64
    270  1.1  cherry #define	atomic_subtract_ptr		atomic_subtract_64
    271  1.1  cherry #define	atomic_set_acq_ptr		atomic_set_acq_64
    272  1.1  cherry #define	atomic_clear_acq_ptr		atomic_clear_acq_64
    273  1.1  cherry #define	atomic_add_acq_ptr		atomic_add_acq_64
    274  1.1  cherry #define	atomic_subtract_acq_ptr		atomic_subtract_acq_64
    275  1.1  cherry #define	atomic_set_rel_ptr		atomic_set_rel_64
    276  1.1  cherry #define	atomic_clear_rel_ptr		atomic_clear_rel_64
    277  1.1  cherry #define	atomic_add_rel_ptr		atomic_add_rel_64
    278  1.1  cherry #define	atomic_subtract_rel_ptr		atomic_subtract_rel_64
    279  1.1  cherry 
    280  1.1  cherry #undef IA64_CMPXCHG
    281  1.1  cherry 
    282  1.1  cherry /*
    283  1.1  cherry  * Atomically compare the value stored at *p with cmpval and if the
    284  1.1  cherry  * two values are equal, update the value of *p with newval. Returns
    285  1.1  cherry  * zero if the compare failed, nonzero otherwise.
    286  1.1  cherry  */
    287  1.1  cherry static __inline int
    288  1.1  cherry atomic_cmpset_acq_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
    289  1.1  cherry {
    290  1.1  cherry 	return (ia64_cmpxchg_acq_32(p, cmpval, newval) == cmpval);
    291  1.1  cherry }
    292  1.1  cherry 
    293  1.1  cherry static __inline int
    294  1.1  cherry atomic_cmpset_rel_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
    295  1.1  cherry {
    296  1.1  cherry 	return (ia64_cmpxchg_rel_32(p, cmpval, newval) == cmpval);
    297  1.1  cherry }
    298  1.1  cherry 
    299  1.1  cherry /*
    300  1.1  cherry  * Atomically compare the value stored at *p with cmpval and if the
    301  1.1  cherry  * two values are equal, update the value of *p with newval. Returns
    302  1.1  cherry  * zero if the compare failed, nonzero otherwise.
    303  1.1  cherry  */
    304  1.1  cherry static __inline int
    305  1.1  cherry atomic_cmpset_acq_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
    306  1.1  cherry {
    307  1.1  cherry 	return (ia64_cmpxchg_acq_64(p, cmpval, newval) == cmpval);
    308  1.1  cherry }
    309  1.1  cherry 
    310  1.1  cherry static __inline int
    311  1.1  cherry atomic_cmpset_rel_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
    312  1.1  cherry {
    313  1.1  cherry 	return (ia64_cmpxchg_rel_64(p, cmpval, newval) == cmpval);
    314  1.1  cherry }
    315  1.1  cherry 
    316  1.1  cherry #define	atomic_cmpset_32		atomic_cmpset_acq_32
    317  1.1  cherry #define	atomic_cmpset_64		atomic_cmpset_acq_64
    318  1.1  cherry #define	atomic_cmpset_int		atomic_cmpset_32
    319  1.1  cherry #define	atomic_cmpset_long		atomic_cmpset_64
    320  1.1  cherry #define	atomic_cmpset_acq_int		atomic_cmpset_acq_32
    321  1.1  cherry #define	atomic_cmpset_rel_int		atomic_cmpset_rel_32
    322  1.1  cherry #define	atomic_cmpset_acq_long		atomic_cmpset_acq_64
    323  1.1  cherry #define	atomic_cmpset_rel_long		atomic_cmpset_rel_64
    324  1.2   scole 
    325  1.2   scole #define	atomic_cmpset_acq_ptr(p, o, n)	\
    326  1.2   scole     (atomic_cmpset_acq_64((volatile uint64_t *)p, (uint64_t)o, (uint64_t)n))
    327  1.2   scole 
    328  1.2   scole #define	atomic_cmpset_ptr		atomic_cmpset_acq_ptr
    329  1.2   scole 
    330  1.2   scole #define	atomic_cmpset_rel_ptr(p, o, n)	\
    331  1.2   scole     (atomic_cmpset_rel_64((volatile uint64_t *)p, (uint64_t)o, (uint64_t)n))
    332  1.1  cherry 
    333  1.1  cherry static __inline uint32_t
    334  1.1  cherry atomic_readandclear_32(volatile uint32_t* p)
    335  1.1  cherry {
    336  1.1  cherry 	uint32_t val;
    337  1.1  cherry 	do {
    338  1.1  cherry 		val = *p;
    339  1.1  cherry 	} while (!atomic_cmpset_32(p, val, 0));
    340  1.1  cherry 	return (val);
    341  1.1  cherry }
    342  1.1  cherry 
    343  1.1  cherry static __inline uint64_t
    344  1.1  cherry atomic_readandclear_64(volatile uint64_t* p)
    345  1.1  cherry {
    346  1.1  cherry 	uint64_t val;
    347  1.1  cherry 	do {
    348  1.1  cherry 		val = *p;
    349  1.1  cherry 	} while (!atomic_cmpset_64(p, val, 0));
    350  1.1  cherry 	return (val);
    351  1.1  cherry }
    352  1.1  cherry 
    353  1.1  cherry #define	atomic_readandclear_int		atomic_readandclear_32
    354  1.1  cherry #define	atomic_readandclear_long	atomic_readandclear_64
    355  1.2   scole #define	atomic_readandclear_ptr		atomic_readandclear_64
    356  1.1  cherry 
    357  1.1  cherry /*
    358  1.1  cherry  * Atomically add the value of v to the integer pointed to by p and return
    359  1.1  cherry  * the previous value of *p.
    360  1.1  cherry  *
    361  1.1  cherry  * XXX: Should we use the fetchadd instruction here?
    362  1.1  cherry  */
    363  1.1  cherry static __inline uint32_t
    364  1.1  cherry atomic_fetchadd_32(volatile uint32_t *p, uint32_t v)
    365  1.1  cherry {
    366  1.1  cherry 	uint32_t value;
    367  1.1  cherry 
    368  1.1  cherry 	do {
    369  1.1  cherry 		value = *p;
    370  1.1  cherry 	} while (!atomic_cmpset_32(p, value, value + v));
    371  1.1  cherry 	return (value);
    372  1.1  cherry }
    373  1.1  cherry 
    374  1.1  cherry #define	atomic_fetchadd_int		atomic_fetchadd_32
    375  1.1  cherry 
    376  1.2   scole static __inline u_long
    377  1.2   scole atomic_fetchadd_long(volatile u_long *p, u_long v)
    378  1.2   scole {
    379  1.2   scole 	u_long value;
    380  1.2   scole 
    381  1.2   scole 	do {
    382  1.2   scole 		value = *p;
    383  1.2   scole 	} while (!atomic_cmpset_64(p, value, value + v));
    384  1.2   scole 	return (value);
    385  1.2   scole }
    386  1.2   scole 
    387  1.2   scole /*
    388  1.2   scole  * <type> atomic_swap_<type>(volatile <type> *p, <type> v);
    389  1.2   scole  */
    390  1.2   scole 
    391  1.2   scole static __inline uint32_t
    392  1.3   scole ia64_atomic_swap_32(volatile uint32_t *p, uint32_t v)
    393  1.2   scole {
    394  1.2   scole 	uint32_t r;
    395  1.2   scole 
    396  1.2   scole 	__asm __volatile ("xchg4 %0 = %3, %2;;" : "=r"(r), "=m"(*p) :
    397  1.2   scole 	    "r"(v), "m"(*p) : "memory");
    398  1.2   scole 	return (r);
    399  1.2   scole }
    400  1.2   scole 
    401  1.2   scole static __inline uint64_t
    402  1.3   scole ia64_atomic_swap_64(volatile uint64_t *p, uint64_t v)
    403  1.2   scole {
    404  1.2   scole 	uint64_t r;
    405  1.2   scole 
    406  1.2   scole 	__asm __volatile ("xchg8 %0 = %3, %2;;" : "=r"(r), "=m"(*p) :
    407  1.2   scole 	    "r"(v), "m"(*p) : "memory");
    408  1.2   scole 	return (r);
    409  1.2   scole }
    410  1.2   scole 
    411  1.3   scole #define	ia64_atomic_swap_int	ia64_atomic_swap_32
    412  1.3   scole #define	ia64_atomic_swap_long	ia64_atomic_swap_64
    413  1.3   scole #define	ia64_atomic_swap_ptr	ia64_atomic_swap_64
    414  1.2   scole 
    415  1.3   scole #endif /* ! _IA64_ATOMIC_H_ */
    416