1 #ifndef __ASM_SH64_ATOMIC_H
2 #define __ASM_SH64_ATOMIC_H
5 * This file is subject to the terms and conditions of the GNU General Public
6 * License. See the file "COPYING" in the main directory of this archive
9 * include/asm-sh64/atomic.h
11 * Copyright (C) 2000, 2001 Paolo Alberelli
12 * Copyright (C) 2003 Paul Mundt
17 * Atomic operations that C can't guarantee us. Useful for
18 * resource counting etc..
22 typedef struct { volatile int counter; } atomic_t;
24 #define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
26 #define atomic_read(v) ((v)->counter)
27 #define atomic_set(v,i) ((v)->counter = (i))
29 #include <asm/system.h>
32 * To get proper branch prediction for the main line, we must branch
33 * forward to code at the end of this object's .text section, then
34 * branch back to restart the operation.
37 static __inline__ void atomic_add(int i, atomic_t * v)
41 local_irq_save(flags);
43 local_irq_restore(flags);
46 static __inline__ void atomic_sub(int i, atomic_t *v)
50 local_irq_save(flags);
52 local_irq_restore(flags);
55 static __inline__ int atomic_add_return(int i, atomic_t * v)
57 unsigned long temp, flags;
59 local_irq_save(flags);
63 local_irq_restore(flags);
68 #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
70 static __inline__ int atomic_sub_return(int i, atomic_t * v)
72 unsigned long temp, flags;
74 local_irq_save(flags);
78 local_irq_restore(flags);
83 #define atomic_dec_return(v) atomic_sub_return(1,(v))
84 #define atomic_inc_return(v) atomic_add_return(1,(v))
87 * atomic_inc_and_test - increment and test
88 * @v: pointer of type atomic_t
90 * Atomically increments @v by 1
91 * and returns true if the result is zero, or false for all
94 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
96 #define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
97 #define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
99 #define atomic_inc(v) atomic_add(1,(v))
100 #define atomic_dec(v) atomic_sub(1,(v))
102 static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
107 local_irq_save(flags);
109 if (likely(ret == old))
111 local_irq_restore(flags);
116 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
118 static inline int atomic_add_unless(atomic_t *v, int a, int u)
123 local_irq_save(flags);
127 local_irq_restore(flags);
131 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
133 static __inline__ void atomic_clear_mask(unsigned int mask, atomic_t *v)
137 local_irq_save(flags);
139 local_irq_restore(flags);
142 static __inline__ void atomic_set_mask(unsigned int mask, atomic_t *v)
146 local_irq_save(flags);
148 local_irq_restore(flags);
151 /* Atomic operations are already serializing on SH */
152 #define smp_mb__before_atomic_dec() barrier()
153 #define smp_mb__after_atomic_dec() barrier()
154 #define smp_mb__before_atomic_inc() barrier()
155 #define smp_mb__after_atomic_inc() barrier()
157 #include <asm-generic/atomic.h>
158 #endif /* __ASM_SH64_ATOMIC_H */