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
5 * Copyright (C) 1996, 1997, 2000 David S. Miller (davem@redhat.com)
8 #ifndef __ARCH_SPARC64_ATOMIC__
9 #define __ARCH_SPARC64_ATOMIC__
11 #include <linux/config.h>
12 #include <linux/types.h>
14 typedef struct { volatile int counter; } atomic_t;
15 typedef struct { volatile __s64 counter; } atomic64_t;
17 #define ATOMIC_INIT(i) { (i) }
18 #define ATOMIC64_INIT(i) { (i) }
20 #define atomic_read(v) ((v)->counter)
21 #define atomic64_read(v) ((v)->counter)
23 #define atomic_set(v, i) (((v)->counter) = i)
24 #define atomic64_set(v, i) (((v)->counter) = i)
26 extern void atomic_add(int, atomic_t *);
27 extern void atomic64_add(int, atomic64_t *);
28 extern void atomic_sub(int, atomic_t *);
29 extern void atomic64_sub(int, atomic64_t *);
31 extern int atomic_add_ret(int, atomic_t *);
32 extern int atomic64_add_ret(int, atomic64_t *);
33 extern int atomic_sub_ret(int, atomic_t *);
34 extern int atomic64_sub_ret(int, atomic64_t *);
36 #define atomic_dec_return(v) atomic_sub_ret(1, v)
37 #define atomic64_dec_return(v) atomic64_sub_ret(1, v)
39 #define atomic_inc_return(v) atomic_add_ret(1, v)
40 #define atomic64_inc_return(v) atomic64_add_ret(1, v)
42 #define atomic_sub_return(i, v) atomic_sub_ret(i, v)
43 #define atomic64_sub_return(i, v) atomic64_sub_ret(i, v)
45 #define atomic_add_return(i, v) atomic_add_ret(i, v)
46 #define atomic64_add_return(i, v) atomic64_add_ret(i, v)
49 * atomic_inc_and_test - increment and test
50 * @v: pointer of type atomic_t
52 * Atomically increments @v by 1
53 * and returns true if the result is zero, or false for all
56 #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
57 #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
59 #define atomic_sub_and_test(i, v) (atomic_sub_ret(i, v) == 0)
60 #define atomic64_sub_and_test(i, v) (atomic64_sub_ret(i, v) == 0)
62 #define atomic_dec_and_test(v) (atomic_sub_ret(1, v) == 0)
63 #define atomic64_dec_and_test(v) (atomic64_sub_ret(1, v) == 0)
65 #define atomic_inc(v) atomic_add(1, v)
66 #define atomic64_inc(v) atomic64_add(1, v)
68 #define atomic_dec(v) atomic_sub(1, v)
69 #define atomic64_dec(v) atomic64_sub(1, v)
71 #define atomic_add_negative(i, v) (atomic_add_ret(i, v) < 0)
72 #define atomic64_add_negative(i, v) (atomic64_add_ret(i, v) < 0)
74 #define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
75 #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
77 #define atomic_add_unless(v, a, u) \
81 while (c != (u) && (old = atomic_cmpxchg((v), c, c + (a))) != c) \
85 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
87 /* Atomic operations are already serializing */
89 #define smp_mb__before_atomic_dec() membar_storeload_loadload();
90 #define smp_mb__after_atomic_dec() membar_storeload_storestore();
91 #define smp_mb__before_atomic_inc() membar_storeload_loadload();
92 #define smp_mb__after_atomic_inc() membar_storeload_storestore();
94 #define smp_mb__before_atomic_dec() barrier()
95 #define smp_mb__after_atomic_dec() barrier()
96 #define smp_mb__before_atomic_inc() barrier()
97 #define smp_mb__after_atomic_inc() barrier()
100 #include <asm-generic/atomic.h>
101 #endif /* !(__ARCH_SPARC64_ATOMIC__) */