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