1 /* MN10300 Atomic counter operations
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
19 * Atomic operations that C can't guarantee us. Useful for
20 * resource counting etc..
23 #define ATOMIC_INIT(i) { (i) }
28 * atomic_read - read atomic variable
29 * @v: pointer of type atomic_t
31 * Atomically reads the value of @v. Note that the guaranteed
32 * useful range of an atomic_t is only 24 bits.
34 #define atomic_read(v) ((v)->counter)
37 * atomic_set - set atomic variable
38 * @v: pointer of type atomic_t
41 * Atomically sets the value of @v to @i. Note that the guaranteed
42 * useful range of an atomic_t is only 24 bits.
44 #define atomic_set(v, i) (((v)->counter) = (i))
46 #include <asm/system.h>
49 * atomic_add_return - add integer to atomic variable
50 * @i: integer value to add
51 * @v: pointer of type atomic_t
53 * Atomically adds @i to @v and returns the result
54 * Note that the guaranteed useful range of an atomic_t is only 24 bits.
56 static inline int atomic_add_return(int i, atomic_t *v)
61 local_irq_save(flags);
65 local_irq_restore(flags);
71 * atomic_sub_return - subtract integer from atomic variable
72 * @i: integer value to subtract
73 * @v: pointer of type atomic_t
75 * Atomically subtracts @i from @v and returns the result
76 * Note that the guaranteed useful range of an atomic_t is only 24 bits.
78 static inline int atomic_sub_return(int i, atomic_t *v)
83 local_irq_save(flags);
87 local_irq_restore(flags);
92 static inline int atomic_add_negative(int i, atomic_t *v)
94 return atomic_add_return(i, v) < 0;
97 static inline void atomic_add(int i, atomic_t *v)
99 atomic_add_return(i, v);
102 static inline void atomic_sub(int i, atomic_t *v)
104 atomic_sub_return(i, v);
107 static inline void atomic_inc(atomic_t *v)
109 atomic_add_return(1, v);
112 static inline void atomic_dec(atomic_t *v)
114 atomic_sub_return(1, v);
117 #define atomic_dec_return(v) atomic_sub_return(1, (v))
118 #define atomic_inc_return(v) atomic_add_return(1, (v))
120 #define atomic_sub_and_test(i, v) (atomic_sub_return((i), (v)) == 0)
121 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
122 #define atomic_inc_and_test(v) (atomic_add_return(1, (v)) == 0)
124 #define atomic_add_unless(v, a, u) \
127 c = atomic_read(v); \
128 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
133 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
135 static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
140 local_irq_save(flags);
142 local_irq_restore(flags);
145 #define atomic_xchg(ptr, v) (xchg(&(ptr)->counter, (v)))
146 #define atomic_cmpxchg(v, old, new) (cmpxchg(&((v)->counter), (old), (new)))
148 /* Atomic operations are already serializing on MN10300??? */
149 #define smp_mb__before_atomic_dec() barrier()
150 #define smp_mb__after_atomic_dec() barrier()
151 #define smp_mb__before_atomic_inc() barrier()
152 #define smp_mb__after_atomic_inc() barrier()
154 #include <asm-generic/atomic.h>
156 #endif /* __KERNEL__ */
157 #endif /* _ASM_ATOMIC_H */