1/* atomic.h: Thankfully the V9 is at least reasonable for this
2 *           stuff.
3 *
4 * Copyright (C) 1996, 1997, 2000, 2012 David S. Miller (davem@redhat.com)
5 */
6
7#ifndef __ARCH_SPARC64_ATOMIC__
8#define __ARCH_SPARC64_ATOMIC__
9
10#include <linux/types.h>
11#include <asm/cmpxchg.h>
12#include <asm/barrier.h>
13
14#define ATOMIC_INIT(i)		{ (i) }
15#define ATOMIC64_INIT(i)	{ (i) }
16
17#define atomic_read(v)		ACCESS_ONCE((v)->counter)
18#define atomic64_read(v)	ACCESS_ONCE((v)->counter)
19
20#define atomic_set(v, i)	(((v)->counter) = i)
21#define atomic64_set(v, i)	(((v)->counter) = i)
22
23#define ATOMIC_OP(op)							\
24void atomic_##op(int, atomic_t *);					\
25void atomic64_##op(long, atomic64_t *);
26
27#define ATOMIC_OP_RETURN(op)						\
28int atomic_##op##_return(int, atomic_t *);				\
29long atomic64_##op##_return(long, atomic64_t *);
30
31#define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op)
32
33ATOMIC_OPS(add)
34ATOMIC_OPS(sub)
35
36#undef ATOMIC_OPS
37#undef ATOMIC_OP_RETURN
38#undef ATOMIC_OP
39
40#define atomic_dec_return(v)   atomic_sub_return(1, v)
41#define atomic64_dec_return(v) atomic64_sub_return(1, v)
42
43#define atomic_inc_return(v)   atomic_add_return(1, v)
44#define atomic64_inc_return(v) atomic64_add_return(1, v)
45
46/*
47 * atomic_inc_and_test - increment and test
48 * @v: pointer of type atomic_t
49 *
50 * Atomically increments @v by 1
51 * and returns true if the result is zero, or false for all
52 * other cases.
53 */
54#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
55#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
56
57#define atomic_sub_and_test(i, v) (atomic_sub_return(i, v) == 0)
58#define atomic64_sub_and_test(i, v) (atomic64_sub_return(i, v) == 0)
59
60#define atomic_dec_and_test(v) (atomic_sub_return(1, v) == 0)
61#define atomic64_dec_and_test(v) (atomic64_sub_return(1, v) == 0)
62
63#define atomic_inc(v) atomic_add(1, v)
64#define atomic64_inc(v) atomic64_add(1, v)
65
66#define atomic_dec(v) atomic_sub(1, v)
67#define atomic64_dec(v) atomic64_sub(1, v)
68
69#define atomic_add_negative(i, v) (atomic_add_return(i, v) < 0)
70#define atomic64_add_negative(i, v) (atomic64_add_return(i, v) < 0)
71
72#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
73#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
74
75static inline int __atomic_add_unless(atomic_t *v, int a, int u)
76{
77	int c, old;
78	c = atomic_read(v);
79	for (;;) {
80		if (unlikely(c == (u)))
81			break;
82		old = atomic_cmpxchg((v), c, c + (a));
83		if (likely(old == c))
84			break;
85		c = old;
86	}
87	return c;
88}
89
90#define atomic64_cmpxchg(v, o, n) \
91	((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
92#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
93
94static inline long atomic64_add_unless(atomic64_t *v, long a, long u)
95{
96	long c, old;
97	c = atomic64_read(v);
98	for (;;) {
99		if (unlikely(c == (u)))
100			break;
101		old = atomic64_cmpxchg((v), c, c + (a));
102		if (likely(old == c))
103			break;
104		c = old;
105	}
106	return c != (u);
107}
108
109#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
110
111long atomic64_dec_if_positive(atomic64_t *v);
112
113#endif /* !(__ARCH_SPARC64_ATOMIC__) */
114