Home | History | Annotate | Line # | Download | only in include
atomic.h revision 1.2
      1  1.2   scole /*	$NetBSD: atomic.h,v 1.2 2016/08/08 17:44:24 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.1  cherry #ifndef _MACHINE_ATOMIC_H_
     32  1.1  cherry #define	_MACHINE_ATOMIC_H_
     33  1.1  cherry 
     34  1.2   scole /* XXX need these?
     35  1.2   scole #define	mb()	__asm __volatile("mf")
     36  1.2   scole #define	wmb()	mb()
     37  1.2   scole #define	rmb()	mb()
     38  1.2   scole */
     39  1.2   scole 
     40  1.1  cherry /*
     41  1.1  cherry  * Various simple arithmetic on memory which is atomic in the presence
     42  1.1  cherry  * of interrupts and SMP safe.
     43  1.1  cherry  */
     44  1.1  cherry 
     45  1.1  cherry /*
     46  1.1  cherry  * Everything is built out of cmpxchg.
     47  1.1  cherry  */
     48  1.1  cherry #define	IA64_CMPXCHG(sz, sem, p, cmpval, newval, ret)			\
     49  1.1  cherry 	__asm __volatile (						\
     50  1.1  cherry 		"mov ar.ccv=%2;;\n\t"					\
     51  1.1  cherry 		"cmpxchg" #sz "." #sem " %0=%4,%3,ar.ccv\n\t"		\
     52  1.1  cherry 		: "=r" (ret), "=m" (*p)					\
     53  1.2   scole 		: "r" ((uint64_t)cmpval), "r" (newval), "m" (*p)	\
     54  1.1  cherry 		: "memory")
     55  1.1  cherry 
     56  1.1  cherry /*
     57  1.1  cherry  * Some common forms of cmpxch.
     58  1.1  cherry  */
     59  1.1  cherry static __inline uint32_t
     60  1.1  cherry ia64_cmpxchg_acq_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
     61  1.1  cherry {
     62  1.1  cherry 	uint32_t ret;
     63  1.1  cherry 	IA64_CMPXCHG(4, acq, p, cmpval, newval, ret);
     64  1.1  cherry 	return (ret);
     65  1.1  cherry }
     66  1.1  cherry 
     67  1.1  cherry static __inline uint32_t
     68  1.1  cherry ia64_cmpxchg_rel_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
     69  1.1  cherry {
     70  1.1  cherry 	uint32_t ret;
     71  1.1  cherry 	IA64_CMPXCHG(4, rel, p, cmpval, newval, ret);
     72  1.1  cherry 	return (ret);
     73  1.1  cherry }
     74  1.1  cherry 
     75  1.1  cherry static __inline uint64_t
     76  1.1  cherry ia64_cmpxchg_acq_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
     77  1.1  cherry {
     78  1.1  cherry 	uint64_t ret;
     79  1.1  cherry 	IA64_CMPXCHG(8, acq, p, cmpval, newval, ret);
     80  1.1  cherry 	return (ret);
     81  1.1  cherry }
     82  1.1  cherry 
     83  1.1  cherry static __inline uint64_t
     84  1.1  cherry ia64_cmpxchg_rel_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
     85  1.1  cherry {
     86  1.1  cherry 	uint64_t ret;
     87  1.1  cherry 	IA64_CMPXCHG(8, rel, p, cmpval, newval, ret);
     88  1.1  cherry 	return (ret);
     89  1.1  cherry }
     90  1.1  cherry 
     91  1.1  cherry #define	ATOMIC_STORE_LOAD(type, width, size)				\
     92  1.1  cherry 	static __inline uint##width##_t					\
     93  1.1  cherry 	ia64_ld_acq_##width(volatile uint##width##_t* p)		\
     94  1.1  cherry 	{								\
     95  1.1  cherry 		uint##width##_t v;					\
     96  1.1  cherry 		__asm __volatile ("ld" size ".acq %0=%1" : "=r" (v)	\
     97  1.1  cherry 		    : "m" (*p) : "memory");				\
     98  1.1  cherry 		return (v);						\
     99  1.1  cherry 	}								\
    100  1.1  cherry 									\
    101  1.1  cherry 	static __inline uint##width##_t					\
    102  1.1  cherry 	atomic_load_acq_##width(volatile uint##width##_t* p)		\
    103  1.1  cherry 	{								\
    104  1.1  cherry 		uint##width##_t v;					\
    105  1.1  cherry 		__asm __volatile ("ld" size ".acq %0=%1" : "=r" (v)	\
    106  1.1  cherry 		    : "m" (*p) : "memory");				\
    107  1.1  cherry 		return (v);						\
    108  1.1  cherry 	}								\
    109  1.1  cherry 									\
    110  1.1  cherry 	static __inline uint##width##_t					\
    111  1.1  cherry 	atomic_load_acq_##type(volatile uint##width##_t* p)		\
    112  1.1  cherry 	{								\
    113  1.1  cherry 		uint##width##_t v;					\
    114  1.1  cherry 		__asm __volatile ("ld" size ".acq %0=%1" : "=r" (v)	\
    115  1.1  cherry 		    : "m" (*p) : "memory");				\
    116  1.1  cherry 		return (v);						\
    117  1.1  cherry 	}								\
    118  1.1  cherry 								       	\
    119  1.1  cherry 	static __inline void						\
    120  1.1  cherry 	ia64_st_rel_##width(volatile uint##width##_t* p, uint##width##_t v) \
    121  1.1  cherry 	{								\
    122  1.1  cherry 		__asm __volatile ("st" size ".rel %0=%1" : "=m" (*p)	\
    123  1.1  cherry 		    : "r" (v) : "memory");				\
    124  1.1  cherry 	}								\
    125  1.1  cherry 									\
    126  1.1  cherry 	static __inline void						\
    127  1.1  cherry 	atomic_store_rel_##width(volatile uint##width##_t* p,		\
    128  1.1  cherry 	    uint##width##_t v)						\
    129  1.1  cherry 	{								\
    130  1.1  cherry 		__asm __volatile ("st" size ".rel %0=%1" : "=m" (*p)	\
    131  1.1  cherry 		    : "r" (v) : "memory");				\
    132  1.1  cherry 	}								\
    133  1.1  cherry 									\
    134  1.1  cherry 	static __inline void						\
    135  1.1  cherry 	atomic_store_rel_##type(volatile uint##width##_t* p,		\
    136  1.1  cherry 	    uint##width##_t v)						\
    137  1.1  cherry 	{								\
    138  1.1  cherry 		__asm __volatile ("st" size ".rel %0=%1" : "=m" (*p)	\
    139  1.1  cherry 		    : "r" (v) : "memory");				\
    140  1.1  cherry 	}
    141  1.1  cherry 
    142  1.1  cherry ATOMIC_STORE_LOAD(char,	 8,  "1")
    143  1.1  cherry ATOMIC_STORE_LOAD(short, 16, "2")
    144  1.1  cherry ATOMIC_STORE_LOAD(int,	 32, "4")
    145  1.1  cherry ATOMIC_STORE_LOAD(long,	 64, "8")
    146  1.1  cherry 
    147  1.1  cherry #undef ATOMIC_STORE_LOAD
    148  1.1  cherry 
    149  1.2   scole #define	atomic_load_acq_ptr(p)		\
    150  1.2   scole     ((void *)atomic_load_acq_64((volatile uint64_t *)p))
    151  1.2   scole 
    152  1.2   scole #define	atomic_store_rel_ptr(p, v)	\
    153  1.2   scole     atomic_store_rel_64((volatile uint64_t *)p, (uint64_t)v)
    154  1.1  cherry 
    155  1.1  cherry #define	IA64_ATOMIC(sz, type, name, width, op)				\
    156  1.1  cherry 	static __inline type						\
    157  1.1  cherry 	atomic_##name##_acq_##width(volatile type *p, type v)		\
    158  1.1  cherry 	{								\
    159  1.1  cherry 		type old, ret;						\
    160  1.1  cherry 		do {							\
    161  1.1  cherry 			old = *p;					\
    162  1.1  cherry 			IA64_CMPXCHG(sz, acq, p, old, old op v, ret);	\
    163  1.1  cherry 		} while (ret != old);					\
    164  1.1  cherry 		return (old);						\
    165  1.1  cherry 	}								\
    166  1.1  cherry 									\
    167  1.1  cherry 	static __inline type						\
    168  1.1  cherry 	atomic_##name##_rel_##width(volatile type *p, type v)		\
    169  1.1  cherry 	{								\
    170  1.1  cherry 		type old, ret;						\
    171  1.1  cherry 		do {							\
    172  1.1  cherry 			old = *p;					\
    173  1.1  cherry 			IA64_CMPXCHG(sz, rel, p, old, old op v, ret);	\
    174  1.1  cherry 		} while (ret != old);					\
    175  1.1  cherry 		return (old);						\
    176  1.1  cherry 	}
    177  1.1  cherry 
    178  1.1  cherry IA64_ATOMIC(1, uint8_t,	 set, 8,  |)
    179  1.1  cherry IA64_ATOMIC(2, uint16_t, set, 16, |)
    180  1.1  cherry IA64_ATOMIC(4, uint32_t, set, 32, |)
    181  1.1  cherry IA64_ATOMIC(8, uint64_t, set, 64, |)
    182  1.1  cherry 
    183  1.1  cherry IA64_ATOMIC(1, uint8_t,  clear,	8,  &~)
    184  1.1  cherry IA64_ATOMIC(2, uint16_t, clear,	16, &~)
    185  1.1  cherry IA64_ATOMIC(4, uint32_t, clear,	32, &~)
    186  1.1  cherry IA64_ATOMIC(8, uint64_t, clear,	64, &~)
    187  1.1  cherry 
    188  1.1  cherry IA64_ATOMIC(1, uint8_t,  add, 8,  +)
    189  1.1  cherry IA64_ATOMIC(2, uint16_t, add, 16, +)
    190  1.1  cherry IA64_ATOMIC(4, uint32_t, add, 32, +)
    191  1.1  cherry IA64_ATOMIC(8, uint64_t, add, 64, +)
    192  1.1  cherry 
    193  1.1  cherry IA64_ATOMIC(1, uint8_t,  subtract, 8,  -)
    194  1.1  cherry IA64_ATOMIC(2, uint16_t, subtract, 16, -)
    195  1.1  cherry IA64_ATOMIC(4, uint32_t, subtract, 32, -)
    196  1.1  cherry IA64_ATOMIC(8, uint64_t, subtract, 64, -)
    197  1.1  cherry 
    198  1.1  cherry #undef IA64_ATOMIC
    199  1.1  cherry 
    200  1.1  cherry #define	atomic_set_8			atomic_set_acq_8
    201  1.1  cherry #define	atomic_clear_8			atomic_clear_acq_8
    202  1.1  cherry #define	atomic_add_8			atomic_add_acq_8
    203  1.1  cherry #define	atomic_subtract_8		atomic_subtract_acq_8
    204  1.1  cherry 
    205  1.1  cherry #define	atomic_set_16			atomic_set_acq_16
    206  1.1  cherry #define	atomic_clear_16			atomic_clear_acq_16
    207  1.1  cherry #define	atomic_add_16			atomic_add_acq_16
    208  1.1  cherry #define	atomic_subtract_16		atomic_subtract_acq_16
    209  1.1  cherry 
    210  1.1  cherry #define	atomic_set_32			atomic_set_acq_32
    211  1.1  cherry #define	atomic_clear_32			atomic_clear_acq_32
    212  1.1  cherry #define	atomic_add_32			atomic_add_acq_32
    213  1.1  cherry #define	atomic_subtract_32		atomic_subtract_acq_32
    214  1.1  cherry 
    215  1.1  cherry #define	atomic_set_64			atomic_set_acq_64
    216  1.1  cherry #define	atomic_clear_64			atomic_clear_acq_64
    217  1.1  cherry #define	atomic_add_64			atomic_add_acq_64
    218  1.1  cherry #define	atomic_subtract_64		atomic_subtract_acq_64
    219  1.1  cherry 
    220  1.1  cherry #define	atomic_set_char			atomic_set_8
    221  1.1  cherry #define	atomic_clear_char		atomic_clear_8
    222  1.1  cherry #define	atomic_add_char			atomic_add_8
    223  1.1  cherry #define	atomic_subtract_char		atomic_subtract_8
    224  1.1  cherry #define	atomic_set_acq_char		atomic_set_acq_8
    225  1.1  cherry #define	atomic_clear_acq_char		atomic_clear_acq_8
    226  1.1  cherry #define	atomic_add_acq_char		atomic_add_acq_8
    227  1.1  cherry #define	atomic_subtract_acq_char	atomic_subtract_acq_8
    228  1.1  cherry #define	atomic_set_rel_char		atomic_set_rel_8
    229  1.1  cherry #define	atomic_clear_rel_char		atomic_clear_rel_8
    230  1.1  cherry #define	atomic_add_rel_char		atomic_add_rel_8
    231  1.1  cherry #define	atomic_subtract_rel_char	atomic_subtract_rel_8
    232  1.1  cherry 
    233  1.1  cherry #define	atomic_set_short		atomic_set_16
    234  1.1  cherry #define	atomic_clear_short		atomic_clear_16
    235  1.1  cherry #define	atomic_add_short		atomic_add_16
    236  1.1  cherry #define	atomic_subtract_short		atomic_subtract_16
    237  1.1  cherry #define	atomic_set_acq_short		atomic_set_acq_16
    238  1.1  cherry #define	atomic_clear_acq_short		atomic_clear_acq_16
    239  1.1  cherry #define	atomic_add_acq_short		atomic_add_acq_16
    240  1.1  cherry #define	atomic_subtract_acq_short	atomic_subtract_acq_16
    241  1.1  cherry #define	atomic_set_rel_short		atomic_set_rel_16
    242  1.1  cherry #define	atomic_clear_rel_short		atomic_clear_rel_16
    243  1.1  cherry #define	atomic_add_rel_short		atomic_add_rel_16
    244  1.1  cherry #define	atomic_subtract_rel_short	atomic_subtract_rel_16
    245  1.1  cherry 
    246  1.1  cherry #define	atomic_set_int			atomic_set_32
    247  1.1  cherry #define	atomic_clear_int		atomic_clear_32
    248  1.1  cherry #define	atomic_add_int			atomic_add_32
    249  1.1  cherry #define	atomic_subtract_int		atomic_subtract_32
    250  1.1  cherry #define	atomic_set_acq_int		atomic_set_acq_32
    251  1.1  cherry #define	atomic_clear_acq_int		atomic_clear_acq_32
    252  1.1  cherry #define	atomic_add_acq_int		atomic_add_acq_32
    253  1.1  cherry #define	atomic_subtract_acq_int		atomic_subtract_acq_32
    254  1.1  cherry #define	atomic_set_rel_int		atomic_set_rel_32
    255  1.1  cherry #define	atomic_clear_rel_int		atomic_clear_rel_32
    256  1.1  cherry #define	atomic_add_rel_int		atomic_add_rel_32
    257  1.1  cherry #define	atomic_subtract_rel_int		atomic_subtract_rel_32
    258  1.1  cherry 
    259  1.1  cherry #define	atomic_set_long			atomic_set_64
    260  1.1  cherry #define	atomic_clear_long		atomic_clear_64
    261  1.1  cherry #define	atomic_add_long			atomic_add_64
    262  1.1  cherry #define	atomic_subtract_long		atomic_subtract_64
    263  1.1  cherry #define	atomic_set_acq_long		atomic_set_acq_64
    264  1.1  cherry #define	atomic_clear_acq_long		atomic_clear_acq_64
    265  1.1  cherry #define	atomic_add_acq_long		atomic_add_acq_64
    266  1.1  cherry #define	atomic_subtract_acq_long	atomic_subtract_acq_64
    267  1.1  cherry #define	atomic_set_rel_long		atomic_set_rel_64
    268  1.1  cherry #define	atomic_clear_rel_long		atomic_clear_rel_64
    269  1.1  cherry #define	atomic_add_rel_long		atomic_add_rel_64
    270  1.1  cherry #define	atomic_subtract_rel_long	atomic_subtract_rel_64
    271  1.1  cherry 
    272  1.2   scole /* XXX Needs casting. */
    273  1.1  cherry #define	atomic_set_ptr			atomic_set_64
    274  1.1  cherry #define	atomic_clear_ptr		atomic_clear_64
    275  1.1  cherry #define	atomic_add_ptr			atomic_add_64
    276  1.1  cherry #define	atomic_subtract_ptr		atomic_subtract_64
    277  1.1  cherry #define	atomic_set_acq_ptr		atomic_set_acq_64
    278  1.1  cherry #define	atomic_clear_acq_ptr		atomic_clear_acq_64
    279  1.1  cherry #define	atomic_add_acq_ptr		atomic_add_acq_64
    280  1.1  cherry #define	atomic_subtract_acq_ptr		atomic_subtract_acq_64
    281  1.1  cherry #define	atomic_set_rel_ptr		atomic_set_rel_64
    282  1.1  cherry #define	atomic_clear_rel_ptr		atomic_clear_rel_64
    283  1.1  cherry #define	atomic_add_rel_ptr		atomic_add_rel_64
    284  1.1  cherry #define	atomic_subtract_rel_ptr		atomic_subtract_rel_64
    285  1.1  cherry 
    286  1.1  cherry #undef IA64_CMPXCHG
    287  1.1  cherry 
    288  1.1  cherry /*
    289  1.1  cherry  * Atomically compare the value stored at *p with cmpval and if the
    290  1.1  cherry  * two values are equal, update the value of *p with newval. Returns
    291  1.1  cherry  * zero if the compare failed, nonzero otherwise.
    292  1.1  cherry  */
    293  1.1  cherry static __inline int
    294  1.1  cherry atomic_cmpset_acq_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
    295  1.1  cherry {
    296  1.1  cherry 	return (ia64_cmpxchg_acq_32(p, cmpval, newval) == cmpval);
    297  1.1  cherry }
    298  1.1  cherry 
    299  1.1  cherry static __inline int
    300  1.1  cherry atomic_cmpset_rel_32(volatile uint32_t* p, uint32_t cmpval, uint32_t newval)
    301  1.1  cherry {
    302  1.1  cherry 	return (ia64_cmpxchg_rel_32(p, cmpval, newval) == cmpval);
    303  1.1  cherry }
    304  1.1  cherry 
    305  1.1  cherry /*
    306  1.1  cherry  * Atomically compare the value stored at *p with cmpval and if the
    307  1.1  cherry  * two values are equal, update the value of *p with newval. Returns
    308  1.1  cherry  * zero if the compare failed, nonzero otherwise.
    309  1.1  cherry  */
    310  1.1  cherry static __inline int
    311  1.1  cherry atomic_cmpset_acq_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
    312  1.1  cherry {
    313  1.1  cherry 	return (ia64_cmpxchg_acq_64(p, cmpval, newval) == cmpval);
    314  1.1  cherry }
    315  1.1  cherry 
    316  1.1  cherry static __inline int
    317  1.1  cherry atomic_cmpset_rel_64(volatile uint64_t* p, uint64_t cmpval, uint64_t newval)
    318  1.1  cherry {
    319  1.1  cherry 	return (ia64_cmpxchg_rel_64(p, cmpval, newval) == cmpval);
    320  1.1  cherry }
    321  1.1  cherry 
    322  1.1  cherry #define	atomic_cmpset_32		atomic_cmpset_acq_32
    323  1.1  cherry #define	atomic_cmpset_64		atomic_cmpset_acq_64
    324  1.1  cherry #define	atomic_cmpset_int		atomic_cmpset_32
    325  1.1  cherry #define	atomic_cmpset_long		atomic_cmpset_64
    326  1.1  cherry #define	atomic_cmpset_acq_int		atomic_cmpset_acq_32
    327  1.1  cherry #define	atomic_cmpset_rel_int		atomic_cmpset_rel_32
    328  1.1  cherry #define	atomic_cmpset_acq_long		atomic_cmpset_acq_64
    329  1.1  cherry #define	atomic_cmpset_rel_long		atomic_cmpset_rel_64
    330  1.2   scole 
    331  1.2   scole #define	atomic_cmpset_acq_ptr(p, o, n)	\
    332  1.2   scole     (atomic_cmpset_acq_64((volatile uint64_t *)p, (uint64_t)o, (uint64_t)n))
    333  1.2   scole 
    334  1.2   scole #define	atomic_cmpset_ptr		atomic_cmpset_acq_ptr
    335  1.2   scole 
    336  1.2   scole #define	atomic_cmpset_rel_ptr(p, o, n)	\
    337  1.2   scole     (atomic_cmpset_rel_64((volatile uint64_t *)p, (uint64_t)o, (uint64_t)n))
    338  1.1  cherry 
    339  1.1  cherry static __inline uint32_t
    340  1.1  cherry atomic_readandclear_32(volatile uint32_t* p)
    341  1.1  cherry {
    342  1.1  cherry 	uint32_t val;
    343  1.1  cherry 	do {
    344  1.1  cherry 		val = *p;
    345  1.1  cherry 	} while (!atomic_cmpset_32(p, val, 0));
    346  1.1  cherry 	return (val);
    347  1.1  cherry }
    348  1.1  cherry 
    349  1.1  cherry static __inline uint64_t
    350  1.1  cherry atomic_readandclear_64(volatile uint64_t* p)
    351  1.1  cherry {
    352  1.1  cherry 	uint64_t val;
    353  1.1  cherry 	do {
    354  1.1  cherry 		val = *p;
    355  1.1  cherry 	} while (!atomic_cmpset_64(p, val, 0));
    356  1.1  cherry 	return (val);
    357  1.1  cherry }
    358  1.1  cherry 
    359  1.1  cherry #define	atomic_readandclear_int		atomic_readandclear_32
    360  1.1  cherry #define	atomic_readandclear_long	atomic_readandclear_64
    361  1.2   scole #define	atomic_readandclear_ptr		atomic_readandclear_64
    362  1.1  cherry 
    363  1.1  cherry /*
    364  1.1  cherry  * Atomically add the value of v to the integer pointed to by p and return
    365  1.1  cherry  * the previous value of *p.
    366  1.1  cherry  *
    367  1.1  cherry  * XXX: Should we use the fetchadd instruction here?
    368  1.1  cherry  */
    369  1.1  cherry static __inline uint32_t
    370  1.1  cherry atomic_fetchadd_32(volatile uint32_t *p, uint32_t v)
    371  1.1  cherry {
    372  1.1  cherry 	uint32_t value;
    373  1.1  cherry 
    374  1.1  cherry 	do {
    375  1.1  cherry 		value = *p;
    376  1.1  cherry 	} while (!atomic_cmpset_32(p, value, value + v));
    377  1.1  cherry 	return (value);
    378  1.1  cherry }
    379  1.1  cherry 
    380  1.1  cherry #define	atomic_fetchadd_int		atomic_fetchadd_32
    381  1.1  cherry 
    382  1.2   scole static __inline u_long
    383  1.2   scole atomic_fetchadd_long(volatile u_long *p, u_long v)
    384  1.2   scole {
    385  1.2   scole 	u_long value;
    386  1.2   scole 
    387  1.2   scole 	do {
    388  1.2   scole 		value = *p;
    389  1.2   scole 	} while (!atomic_cmpset_64(p, value, value + v));
    390  1.2   scole 	return (value);
    391  1.2   scole }
    392  1.2   scole 
    393  1.2   scole /*
    394  1.2   scole  * XXX already defined in
    395  1.2   scole  * src/common/lib/libc/arch/ia64/atomic/atomic.S.  Need to sort out
    396  1.2   scole  * what to do with this file and atomic.S.  atomic.S version comments
    397  1.2   scole  * (from 2008) say it is not tested at all, but if it works we may not
    398  1.2   scole  * need anything in this file?
    399  1.2   scole  */
    400  1.2   scole #if 0
    401  1.2   scole 
    402  1.2   scole /*
    403  1.2   scole  * <type> atomic_swap_<type>(volatile <type> *p, <type> v);
    404  1.2   scole  */
    405  1.2   scole 
    406  1.2   scole static __inline uint32_t
    407  1.2   scole atomic_swap_32(volatile uint32_t *p, uint32_t v)
    408  1.2   scole {
    409  1.2   scole 	uint32_t r;
    410  1.2   scole 
    411  1.2   scole 	__asm __volatile ("xchg4 %0 = %3, %2;;" : "=r"(r), "=m"(*p) :
    412  1.2   scole 	    "r"(v), "m"(*p) : "memory");
    413  1.2   scole 	return (r);
    414  1.2   scole }
    415  1.2   scole 
    416  1.2   scole static __inline uint64_t
    417  1.2   scole atomic_swap_64(volatile uint64_t *p, uint64_t v)
    418  1.2   scole {
    419  1.2   scole 	uint64_t r;
    420  1.2   scole 
    421  1.2   scole 	__asm __volatile ("xchg8 %0 = %3, %2;;" : "=r"(r), "=m"(*p) :
    422  1.2   scole 	    "r"(v), "m"(*p) : "memory");
    423  1.2   scole 	return (r);
    424  1.2   scole }
    425  1.2   scole #endif
    426  1.2   scole 
    427  1.2   scole #define	atomic_swap_int		atomic_swap_32
    428  1.2   scole #define	atomic_swap_long	atomic_swap_64
    429  1.2   scole #define	atomic_swap_ptr		atomic_swap_64
    430  1.2   scole 
    431  1.1  cherry #endif /* ! _MACHINE_ATOMIC_H_ */
    432