blob: c7983124d99dfb6a31bfbf5233f48ba77a168119 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 Wilcoxea4354672009-01-06 14:40:39 -080010#include <linux/compiler.h>
11#include <linux/types.h>
12#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
15
Anton Blanchardf3d46f92010-05-17 14:33:53 +100016#define atomic_read(v) (*(volatile int *)&(v)->counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define atomic_set(v,i) ((v)->counter = (i))
18
Stuart Menefy1efe4ce2007-11-30 16:12:36 +090019#if defined(CONFIG_GUSA_RB)
20#include <asm/atomic-grb.h>
21#elif defined(CONFIG_CPU_SH4A)
Paul Mundtec723fbe2006-12-07 20:33:38 +090022#include <asm/atomic-llsc.h>
Paul Mundt781125c2006-09-27 17:52:19 +090023#else
Paul Mundtec723fbe2006-12-07 20:33:38 +090024#include <asm/atomic-irq.h>
Paul Mundt781125c2006-09-27 17:52:19 +090025#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
Paul Mundt8c0b8132010-01-08 17:02:17 +090028#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 Torvalds1da177e2005-04-16 15:20:36 -070034
Paul Mundt8c0b8132010-01-08 17:02:17 +090035#define atomic_inc(v) atomic_add(1, (v))
36#define atomic_dec(v) atomic_sub(1, (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Paul Mundt8c0b8132010-01-08 17:02:17 +090038#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 Torvalds1da177e2005-04-16 15:20:36 -070043 * @v: pointer of type atomic_t
Paul Mundt8c0b8132010-01-08 17:02:17 +090044 * @a: the amount to add to v...
45 * @u: ...unless v is equal to u.
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
Paul Mundt8c0b8132010-01-08 17:02:17 +090047 * 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 Torvalds1da177e2005-04-16 15:20:36 -070049 */
Nick Piggin8426e1f2005-11-13 16:07:25 -080050static inline int atomic_add_unless(atomic_t *v, int a, int u)
51{
Paul Mundt8c0b8132010-01-08 17:02:17 +090052 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 Piggin8426e1f2005-11-13 16:07:25 -080062
Paul Mundt8c0b8132010-01-08 17:02:17 +090063 return c != (u);
Nick Piggin8426e1f2005-11-13 16:07:25 -080064}
Nick Piggin8426e1f2005-11-13 16:07:25 -080065
Paul Mundt1c8db712009-10-18 15:36:02 +090066#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 Torvalds1da177e2005-04-16 15:20:36 -070070
Arnd Bergmann72099ed2009-05-13 22:56:29 +000071#include <asm-generic/atomic-long.h>
Paul Mundtf01789c2009-06-17 10:43:13 +090072#include <asm-generic/atomic64.h>
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif /* __ASM_SH_ATOMIC_H */