Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* $Id: atomic.h,v 1.22 2001/07/11 23:56:07 davem Exp $ |
2 | * atomic.h: Thankfully the V9 is at least reasonable for this | |
3 | * stuff. | |
4 | * | |
5 | * Copyright (C) 1996, 1997, 2000 David S. Miller (davem@redhat.com) | |
6 | */ | |
7 | ||
8 | #ifndef __ARCH_SPARC64_ATOMIC__ | |
9 | #define __ARCH_SPARC64_ATOMIC__ | |
10 | ||
1da177e4 LT |
11 | #include <linux/types.h> |
12 | ||
13 | typedef struct { volatile int counter; } atomic_t; | |
14 | typedef struct { volatile __s64 counter; } atomic64_t; | |
15 | ||
16 | #define ATOMIC_INIT(i) { (i) } | |
17 | #define ATOMIC64_INIT(i) { (i) } | |
18 | ||
19 | #define atomic_read(v) ((v)->counter) | |
20 | #define atomic64_read(v) ((v)->counter) | |
21 | ||
22 | #define atomic_set(v, i) (((v)->counter) = i) | |
23 | #define atomic64_set(v, i) (((v)->counter) = i) | |
24 | ||
25 | extern void atomic_add(int, atomic_t *); | |
26 | extern void atomic64_add(int, atomic64_t *); | |
27 | extern void atomic_sub(int, atomic_t *); | |
28 | extern void atomic64_sub(int, atomic64_t *); | |
29 | ||
30 | extern int atomic_add_ret(int, atomic_t *); | |
31 | extern int atomic64_add_ret(int, atomic64_t *); | |
32 | extern int atomic_sub_ret(int, atomic_t *); | |
33 | extern int atomic64_sub_ret(int, atomic64_t *); | |
34 | ||
35 | #define atomic_dec_return(v) atomic_sub_ret(1, v) | |
36 | #define atomic64_dec_return(v) atomic64_sub_ret(1, v) | |
37 | ||
38 | #define atomic_inc_return(v) atomic_add_ret(1, v) | |
39 | #define atomic64_inc_return(v) atomic64_add_ret(1, v) | |
40 | ||
41 | #define atomic_sub_return(i, v) atomic_sub_ret(i, v) | |
42 | #define atomic64_sub_return(i, v) atomic64_sub_ret(i, v) | |
43 | ||
44 | #define atomic_add_return(i, v) atomic_add_ret(i, v) | |
45 | #define atomic64_add_return(i, v) atomic64_add_ret(i, v) | |
46 | ||
47 | /* | |
48 | * atomic_inc_and_test - increment and test | |
49 | * @v: pointer of type atomic_t | |
50 | * | |
51 | * Atomically increments @v by 1 | |
52 | * and returns true if the result is zero, or false for all | |
53 | * other cases. | |
54 | */ | |
55 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) | |
7c72aaf2 | 56 | #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0) |
1da177e4 LT |
57 | |
58 | #define atomic_sub_and_test(i, v) (atomic_sub_ret(i, v) == 0) | |
59 | #define atomic64_sub_and_test(i, v) (atomic64_sub_ret(i, v) == 0) | |
60 | ||
61 | #define atomic_dec_and_test(v) (atomic_sub_ret(1, v) == 0) | |
62 | #define atomic64_dec_and_test(v) (atomic64_sub_ret(1, v) == 0) | |
63 | ||
64 | #define atomic_inc(v) atomic_add(1, v) | |
65 | #define atomic64_inc(v) atomic64_add(1, v) | |
66 | ||
67 | #define atomic_dec(v) atomic_sub(1, v) | |
68 | #define atomic64_dec(v) atomic64_sub(1, v) | |
69 | ||
70 | #define atomic_add_negative(i, v) (atomic_add_ret(i, v) < 0) | |
71 | #define atomic64_add_negative(i, v) (atomic64_add_ret(i, v) < 0) | |
72 | ||
4a6dae6d | 73 | #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n))) |
ffbf670f | 74 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
4a6dae6d | 75 | |
8426e1f6 NP |
76 | #define atomic_add_unless(v, a, u) \ |
77 | ({ \ | |
78 | int c, old; \ | |
79 | c = atomic_read(v); \ | |
0b2fcfdb NP |
80 | for (;;) { \ |
81 | if (unlikely(c == (u))) \ | |
82 | break; \ | |
83 | old = atomic_cmpxchg((v), c, c + (a)); \ | |
84 | if (likely(old == c)) \ | |
85 | break; \ | |
8426e1f6 | 86 | c = old; \ |
0b2fcfdb NP |
87 | } \ |
88 | likely(c != (u)); \ | |
8426e1f6 NP |
89 | }) |
90 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) | |
91 | ||
1da177e4 LT |
92 | /* Atomic operations are already serializing */ |
93 | #ifdef CONFIG_SMP | |
4f07118f DM |
94 | #define smp_mb__before_atomic_dec() membar_storeload_loadload(); |
95 | #define smp_mb__after_atomic_dec() membar_storeload_storestore(); | |
96 | #define smp_mb__before_atomic_inc() membar_storeload_loadload(); | |
97 | #define smp_mb__after_atomic_inc() membar_storeload_storestore(); | |
1da177e4 LT |
98 | #else |
99 | #define smp_mb__before_atomic_dec() barrier() | |
100 | #define smp_mb__after_atomic_dec() barrier() | |
101 | #define smp_mb__before_atomic_inc() barrier() | |
102 | #define smp_mb__after_atomic_inc() barrier() | |
103 | #endif | |
104 | ||
d3cb4871 | 105 | #include <asm-generic/atomic.h> |
1da177e4 | 106 | #endif /* !(__ARCH_SPARC64_ATOMIC__) */ |