| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __ASM_SH_ATOMIC_H | 
 | 2 | #define __ASM_SH_ATOMIC_H | 
 | 3 |  | 
 | 4 | /* | 
 | 5 |  * Atomic operations that C can't guarantee us.  Useful for | 
 | 6 |  * resource counting etc.. | 
 | 7 |  * | 
 | 8 |  */ | 
 | 9 |  | 
| Matthew Wilcox | ea435467 | 2009-01-06 14:40:39 -0800 | [diff] [blame] | 10 | #include <linux/compiler.h> | 
 | 11 | #include <linux/types.h> | 
 | 12 | #include <asm/system.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
 | 14 | #define ATOMIC_INIT(i)	( (atomic_t) { (i) } ) | 
 | 15 |  | 
| Anton Blanchard | f3d46f9 | 2010-05-17 14:33:53 +1000 | [diff] [blame] | 16 | #define atomic_read(v)		(*(volatile int *)&(v)->counter) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #define atomic_set(v,i)		((v)->counter = (i)) | 
 | 18 |  | 
| Stuart Menefy | 1efe4ce | 2007-11-30 16:12:36 +0900 | [diff] [blame] | 19 | #if defined(CONFIG_GUSA_RB) | 
 | 20 | #include <asm/atomic-grb.h> | 
 | 21 | #elif defined(CONFIG_CPU_SH4A) | 
| Paul Mundt | ec723fb | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 22 | #include <asm/atomic-llsc.h> | 
| Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 23 | #else | 
| Paul Mundt | ec723fb | 2006-12-07 20:33:38 +0900 | [diff] [blame] | 24 | #include <asm/atomic-irq.h> | 
| Paul Mundt | 781125c | 2006-09-27 17:52:19 +0900 | [diff] [blame] | 25 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
 | 27 | #define atomic_add_negative(a, v)	(atomic_add_return((a), (v)) < 0) | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 28 | #define atomic_dec_return(v)		atomic_sub_return(1, (v)) | 
 | 29 | #define atomic_inc_return(v)		atomic_add_return(1, (v)) | 
 | 30 | #define atomic_inc_and_test(v)		(atomic_inc_return(v) == 0) | 
 | 31 | #define atomic_sub_and_test(i,v)	(atomic_sub_return((i), (v)) == 0) | 
 | 32 | #define atomic_dec_and_test(v)		(atomic_sub_return(1, (v)) == 0) | 
 | 33 | #define atomic_inc_not_zero(v)		atomic_add_unless((v), 1, 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 35 | #define atomic_inc(v)			atomic_add(1, (v)) | 
 | 36 | #define atomic_dec(v)			atomic_sub(1, (v)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 38 | #define atomic_xchg(v, new)		(xchg(&((v)->counter), new)) | 
 | 39 | #define atomic_cmpxchg(v, o, n)		(cmpxchg(&((v)->counter), (o), (n))) | 
 | 40 |  | 
 | 41 | /** | 
 | 42 |  * atomic_add_unless - add unless the number is a given value | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  * @v: pointer of type atomic_t | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 44 |  * @a: the amount to add to v... | 
 | 45 |  * @u: ...unless v is equal to u. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  * | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 47 |  * Atomically adds @a to @v, so long as it was not @u. | 
 | 48 |  * Returns non-zero if @v was not @u, and zero otherwise. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 |  */ | 
| Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 50 | static inline int atomic_add_unless(atomic_t *v, int a, int u) | 
 | 51 | { | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 52 | 	int c, old; | 
 | 53 | 	c = atomic_read(v); | 
 | 54 | 	for (;;) { | 
 | 55 | 		if (unlikely(c == (u))) | 
 | 56 | 			break; | 
 | 57 | 		old = atomic_cmpxchg((v), c, c + (a)); | 
 | 58 | 		if (likely(old == c)) | 
 | 59 | 			break; | 
 | 60 | 		c = old; | 
 | 61 | 	} | 
| Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 62 |  | 
| Paul Mundt | 8c0b813 | 2010-01-08 17:02:17 +0900 | [diff] [blame] | 63 | 	return c != (u); | 
| Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 64 | } | 
| Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 65 |  | 
| Paul Mundt | 1c8db71 | 2009-10-18 15:36:02 +0900 | [diff] [blame] | 66 | #define smp_mb__before_atomic_dec()	smp_mb() | 
 | 67 | #define smp_mb__after_atomic_dec()	smp_mb() | 
 | 68 | #define smp_mb__before_atomic_inc()	smp_mb() | 
 | 69 | #define smp_mb__after_atomic_inc()	smp_mb() | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 |  | 
| Arnd Bergmann | 72099ed | 2009-05-13 22:56:29 +0000 | [diff] [blame] | 71 | #include <asm-generic/atomic-long.h> | 
| Paul Mundt | f01789c | 2009-06-17 10:43:13 +0900 | [diff] [blame] | 72 | #include <asm-generic/atomic64.h> | 
 | 73 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #endif /* __ASM_SH_ATOMIC_H */ |