Home | History | Annotate | Line # | Download | only in linux
atomic.h revision 1.1.2.10
      1 /*	$NetBSD: atomic.h,v 1.1.2.10 2013/09/08 15:37:04 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _LINUX_ATOMIC_H_
     33 #define _LINUX_ATOMIC_H_
     34 
     35 #include <sys/atomic.h>
     36 
     37 #include <machine/limits.h>
     38 
     39 struct atomic {
     40 	union {
     41 		int au_int;
     42 		unsigned int au_uint;
     43 	} a_u;
     44 };
     45 
     46 typedef struct atomic atomic_t;
     47 
     48 static inline int
     49 atomic_read(atomic_t *atomic)
     50 {
     51 	return *(volatile int *)&atomic->a_u.au_int;
     52 }
     53 
     54 static inline void
     55 atomic_set(atomic_t *atomic, int value)
     56 {
     57 	atomic->a_u.au_int = value;
     58 }
     59 
     60 static inline void
     61 atomic_add(int addend, atomic_t *atomic)
     62 {
     63 	atomic_add_int(&atomic->a_u.au_uint, addend);
     64 }
     65 
     66 static inline void
     67 atomic_sub(int subtrahend, atomic_t *atomic)
     68 {
     69 	atomic_add_int(&atomic->a_u.au_uint, -subtrahend);
     70 }
     71 
     72 static inline int
     73 atomic_add_return(int addend, atomic_t *atomic)
     74 {
     75 	return (int)atomic_add_int_nv(&atomic->a_u.au_uint, addend);
     76 }
     77 
     78 static inline void
     79 atomic_inc(atomic_t *atomic)
     80 {
     81 	atomic_inc_uint(&atomic->a_u.au_uint);
     82 }
     83 
     84 static inline void
     85 atomic_dec(atomic_t *atomic)
     86 {
     87 	atomic_dec_uint(&atomic->a_u.au_uint);
     88 }
     89 
     90 static inline int
     91 atomic_dec_and_test(atomic_t *atomic)
     92 {
     93 	return (-1 == (int)atomic_dec_uint_nv(&atomic->a_u.au_uint));
     94 }
     95 
     96 static inline void
     97 atomic_set_mask(unsigned long mask, atomic_t *atomic)
     98 {
     99 	atomic_or_uint(&atomic->a_u.au_uint, mask);
    100 }
    101 
    102 static inline void
    103 atomic_clear_mask(unsigned long mask, atomic_t *atomic)
    104 {
    105 	atomic_and_uint(&atomic->a_u.au_uint, ~mask);
    106 }
    107 
    108 static inline int
    109 atomic_add_unless(atomic_t *atomic, int addend, int zero)
    110 {
    111 	int value;
    112 
    113 	do {
    114 		value = atomic->a_u.au_int;
    115 		if (value == zero)
    116 			return 0;
    117 	} while (atomic_cas_uint(&atomic->a_u.au_uint, value, (value + addend))
    118 	    != value);
    119 
    120 	return 1;
    121 }
    122 
    123 static inline int
    124 atomic_inc_not_zero(atomic_t *atomic)
    125 {
    126 	return atomic_add_unless(atomic, 1, 0);
    127 }
    128 
    129 static inline void
    130 set_bit(unsigned int bit, volatile unsigned long *ptr)
    131 {
    132 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    133 
    134 	atomic_or_ulong(&ptr[bit / units], (1UL << (bit % units)));
    135 }
    136 
    137 static inline void
    138 clear_bit(unsigned int bit, volatile unsigned long *ptr)
    139 {
    140 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    141 
    142 	atomic_and_ulong(&ptr[bit / units], ~(1UL << (bit % units)));
    143 }
    144 
    145 static inline void
    146 change_bit(unsigned int bit, volatile unsigned long *ptr)
    147 {
    148 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    149 	volatile unsigned long *const p = &ptr[bit / units];
    150 	const unsigned long mask = (1UL << (bit % units));
    151 	unsigned long v;
    152 
    153 	do v = *p; while (atomic_cas_ulong(p, v, (v ^ mask)) != v);
    154 }
    155 
    156 static inline unsigned long
    157 test_and_set_bit(unsigned int bit, volatile unsigned long *ptr)
    158 {
    159 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    160 	volatile unsigned long *const p = &ptr[bit / units];
    161 	const unsigned long mask = (1UL << (bit % units));
    162 	unsigned long v;
    163 
    164 	do v = *p; while (atomic_cas_ulong(p, v, (v | mask)) != v);
    165 
    166 	return (v & mask);
    167 }
    168 
    169 static inline unsigned long
    170 test_and_clear_bit(unsigned int bit, volatile unsigned long *ptr)
    171 {
    172 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    173 	volatile unsigned long *const p = &ptr[bit / units];
    174 	const unsigned long mask = (1UL << (bit % units));
    175 	unsigned long v;
    176 
    177 	do v = *p; while (atomic_cas_ulong(p, v, (v & ~mask)) != v);
    178 
    179 	return (v & mask);
    180 }
    181 
    182 static inline unsigned long
    183 test_and_change_bit(unsigned int bit, volatile unsigned long *ptr)
    184 {
    185 	const unsigned int units = (sizeof(*ptr) * CHAR_BIT);
    186 	volatile unsigned long *const p = &ptr[bit / units];
    187 	const unsigned long mask = (1UL << (bit % units));
    188 	unsigned long v;
    189 
    190 	do v = *p; while (atomic_cas_ulong(p, v, (v ^ mask)) != v);
    191 
    192 	return (v & mask);
    193 }
    194 
    195 #if defined(MULTIPROCESSOR) && !defined(__HAVE_ATOMIC_AS_MEMBAR)
    196 /*
    197  * XXX These memory barriers are doubtless overkill, but I am having
    198  * trouble understanding the intent and use of the Linux atomic membar
    199  * API.  I think that for reference counting purposes, the sequences
    200  * should be insn/inc/enter and exit/dec/insn, but the use of the
    201  * before/after memory barriers is not consistent throughout Linux.
    202  */
    203 #  define	smp_mb__before_atomic_inc()	membar_sync()
    204 #  define	smp_mb__after_atomic_inc()	membar_sync()
    205 #  define	smp_mb__before_atomic_dec()	membar_sync()
    206 #  define	smp_mb__after_atomic_dec()	membar_sync()
    207 #else
    208 #  define	smp_mb__before_atomic_inc()	__insn_barrier()
    209 #  define	smp_mb__after_atomic_inc()	__insn_barrier()
    210 #  define	smp_mb__before_atomic_dec()	__insn_barrier()
    211 #  define	smp_mb__after_atomic_dec()	__insn_barrier()
    212 #endif
    213 
    214 #endif  /* _LINUX_ATOMIC_H_ */
    215