Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: atomic.h,v 1.3 2001/07/25 16:15:19 bjornw Exp $ */ |
| 2 | |
| 3 | #ifndef __ASM_CRIS_ATOMIC__ |
| 4 | #define __ASM_CRIS_ATOMIC__ |
| 5 | |
Andrew Morton | 9587997 | 2007-11-14 17:00:51 -0800 | [diff] [blame] | 6 | #include <linux/compiler.h> |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <asm/system.h> |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 9 | #include <asm/arch/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * Atomic operations that C can't guarantee us. Useful for |
| 13 | * resource counting etc.. |
| 14 | */ |
| 15 | |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 16 | typedef struct { volatile int counter; } atomic_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #define ATOMIC_INIT(i) { (i) } |
| 19 | |
| 20 | #define atomic_read(v) ((v)->counter) |
| 21 | #define atomic_set(v,i) (((v)->counter) = (i)) |
| 22 | |
| 23 | /* These should be written in asm but we do it in C for now. */ |
| 24 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 25 | static inline void atomic_add(int i, volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
| 27 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 28 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | v->counter += i; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 30 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 33 | static inline void atomic_sub(int i, volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 36 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | v->counter -= i; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 38 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 41 | static inline int atomic_add_return(int i, volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | unsigned long flags; |
| 44 | int retval; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 45 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | retval = (v->counter += i); |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 47 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return retval; |
| 49 | } |
| 50 | |
| 51 | #define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0) |
| 52 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 53 | static inline int atomic_sub_return(int i, volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | unsigned long flags; |
| 56 | int retval; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 57 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | retval = (v->counter -= i); |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 59 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return retval; |
| 61 | } |
| 62 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 63 | static inline int atomic_sub_and_test(int i, volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | int retval; |
| 66 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 67 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | retval = (v->counter -= i) == 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 69 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return retval; |
| 71 | } |
| 72 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 73 | static inline void atomic_inc(volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 76 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | (v->counter)++; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 78 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 81 | static inline void atomic_dec(volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 84 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | (v->counter)--; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 86 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 89 | static inline int atomic_inc_return(volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
| 91 | unsigned long flags; |
| 92 | int retval; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 93 | cris_atomic_save(v, flags); |
Jesper Nilsson | 3c1d930 | 2008-01-21 17:01:31 +0100 | [diff] [blame] | 94 | retval = ++(v->counter); |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 95 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return retval; |
| 97 | } |
| 98 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 99 | static inline int atomic_dec_return(volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | unsigned long flags; |
| 102 | int retval; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 103 | cris_atomic_save(v, flags); |
Jesper Nilsson | 3c1d930 | 2008-01-21 17:01:31 +0100 | [diff] [blame] | 104 | retval = --(v->counter); |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 105 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return retval; |
| 107 | } |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 108 | static inline int atomic_dec_and_test(volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
| 110 | int retval; |
| 111 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 112 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | retval = --(v->counter) == 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 114 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return retval; |
| 116 | } |
| 117 | |
Adrian Bunk | d9b5444 | 2005-11-07 00:58:44 -0800 | [diff] [blame] | 118 | static inline int atomic_inc_and_test(volatile atomic_t *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | int retval; |
| 121 | unsigned long flags; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 122 | cris_atomic_save(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | retval = ++(v->counter) == 0; |
Mikael Starvik | 8d20a54 | 2005-07-27 11:44:42 -0700 | [diff] [blame] | 124 | cris_atomic_restore(v, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return retval; |
| 126 | } |
| 127 | |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 128 | static inline int atomic_cmpxchg(atomic_t *v, int old, int new) |
| 129 | { |
| 130 | int ret; |
| 131 | unsigned long flags; |
| 132 | |
| 133 | cris_atomic_save(v, flags); |
| 134 | ret = v->counter; |
| 135 | if (likely(ret == old)) |
| 136 | v->counter = new; |
| 137 | cris_atomic_restore(v, flags); |
| 138 | return ret; |
| 139 | } |
| 140 | |
Ingo Molnar | ffbf670 | 2006-01-09 15:59:17 -0800 | [diff] [blame] | 141 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
| 142 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 143 | static inline int atomic_add_unless(atomic_t *v, int a, int u) |
| 144 | { |
| 145 | int ret; |
| 146 | unsigned long flags; |
| 147 | |
| 148 | cris_atomic_save(v, flags); |
| 149 | ret = v->counter; |
| 150 | if (ret != u) |
| 151 | v->counter += a; |
| 152 | cris_atomic_restore(v, flags); |
| 153 | return ret != u; |
| 154 | } |
| 155 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* Atomic operations are already serializing */ |
| 158 | #define smp_mb__before_atomic_dec() barrier() |
| 159 | #define smp_mb__after_atomic_dec() barrier() |
| 160 | #define smp_mb__before_atomic_inc() barrier() |
| 161 | #define smp_mb__after_atomic_inc() barrier() |
| 162 | |
Christoph Lameter | d3cb487 | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 163 | #include <asm-generic/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | #endif |