blob: e12570b9339dcb1a127222cdc04383752a88fc59 [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
10typedef struct { volatile int counter; } atomic_t;
11
12#define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
13
14#define atomic_read(v) ((v)->counter)
15#define atomic_set(v,i) ((v)->counter = (i))
16
Paul Mundte4c2cfe2006-09-27 12:31:01 +090017#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/system.h>
19
Paul Mundt781125c2006-09-27 17:52:19 +090020#ifdef CONFIG_CPU_SH4A
Paul Mundtec723fbe2006-12-07 20:33:38 +090021#include <asm/atomic-llsc.h>
Paul Mundt781125c2006-09-27 17:52:19 +090022#else
Paul Mundtec723fbe2006-12-07 20:33:38 +090023#include <asm/atomic-irq.h>
Paul Mundt781125c2006-09-27 17:52:19 +090024#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define atomic_dec_return(v) atomic_sub_return(1,(v))
29#define atomic_inc_return(v) atomic_add_return(1,(v))
30
31/*
32 * atomic_inc_and_test - increment and test
33 * @v: pointer of type atomic_t
34 *
35 * Atomically increments @v by 1
36 * and returns true if the result is zero, or false for all
37 * other cases.
38 */
39#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
40
41#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
42#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
43
44#define atomic_inc(v) atomic_add(1,(v))
45#define atomic_dec(v) atomic_sub(1,(v))
46
Nick Piggin4a6dae62005-11-13 16:07:24 -080047static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
48{
49 int ret;
50 unsigned long flags;
51
52 local_irq_save(flags);
53 ret = v->counter;
54 if (likely(ret == old))
55 v->counter = new;
56 local_irq_restore(flags);
57
58 return ret;
59}
60
Ingo Molnarffbf6702006-01-09 15:59:17 -080061#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
62
Nick Piggin8426e1f2005-11-13 16:07:25 -080063static inline int atomic_add_unless(atomic_t *v, int a, int u)
64{
65 int ret;
66 unsigned long flags;
67
68 local_irq_save(flags);
69 ret = v->counter;
70 if (ret != u)
71 v->counter += a;
72 local_irq_restore(flags);
73
74 return ret != u;
75}
76#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* Atomic operations are already serializing on SH */
79#define smp_mb__before_atomic_dec() barrier()
80#define smp_mb__after_atomic_dec() barrier()
81#define smp_mb__before_atomic_inc() barrier()
82#define smp_mb__after_atomic_inc() barrier()
83
Christoph Lameterd3cb4872006-01-06 00:11:20 -080084#include <asm-generic/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif /* __ASM_SH_ATOMIC_H */