Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 12 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | typedef struct { volatile int counter; } atomic_t; |
| 15 | typedef struct { volatile __s64 counter; } atomic64_t; |
| 16 | |
| 17 | #define ATOMIC_INIT(i) { (i) } |
| 18 | #define ATOMIC64_INIT(i) { (i) } |
| 19 | |
| 20 | #define atomic_read(v) ((v)->counter) |
| 21 | #define atomic64_read(v) ((v)->counter) |
| 22 | |
| 23 | #define atomic_set(v, i) (((v)->counter) = i) |
| 24 | #define atomic64_set(v, i) (((v)->counter) = i) |
| 25 | |
| 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 *); |
| 30 | |
| 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 *); |
| 35 | |
| 36 | #define atomic_dec_return(v) atomic_sub_ret(1, v) |
| 37 | #define atomic64_dec_return(v) atomic64_sub_ret(1, v) |
| 38 | |
| 39 | #define atomic_inc_return(v) atomic_add_ret(1, v) |
| 40 | #define atomic64_inc_return(v) atomic64_add_ret(1, v) |
| 41 | |
| 42 | #define atomic_sub_return(i, v) atomic_sub_ret(i, v) |
| 43 | #define atomic64_sub_return(i, v) atomic64_sub_ret(i, v) |
| 44 | |
| 45 | #define atomic_add_return(i, v) atomic_add_ret(i, v) |
| 46 | #define atomic64_add_return(i, v) atomic64_add_ret(i, v) |
| 47 | |
| 48 | /* |
| 49 | * atomic_inc_and_test - increment and test |
| 50 | * @v: pointer of type atomic_t |
| 51 | * |
| 52 | * Atomically increments @v by 1 |
| 53 | * and returns true if the result is zero, or false for all |
| 54 | * other cases. |
| 55 | */ |
| 56 | #define atomic_inc_and_test(v) (atomic_inc_return(v) == 0) |
Hugh Dickins | 7c72aaf | 2005-11-23 13:37:40 -0800 | [diff] [blame] | 57 | #define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 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) |
| 61 | |
| 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) |
| 64 | |
| 65 | #define atomic_inc(v) atomic_add(1, v) |
| 66 | #define atomic64_inc(v) atomic64_add(1, v) |
| 67 | |
| 68 | #define atomic_dec(v) atomic_sub(1, v) |
| 69 | #define atomic64_dec(v) atomic64_sub(1, v) |
| 70 | |
| 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) |
| 73 | |
Mathieu Desnoyers | 2549c85 | 2007-05-08 00:34:34 -0700 | [diff] [blame] | 74 | #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n))) |
Ingo Molnar | ffbf670 | 2006-01-09 15:59:17 -0800 | [diff] [blame] | 75 | #define atomic_xchg(v, new) (xchg(&((v)->counter), new)) |
Nick Piggin | 4a6dae6 | 2005-11-13 16:07:24 -0800 | [diff] [blame] | 76 | |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 77 | static __inline__ int atomic_add_unless(atomic_t *v, int a, int u) |
| 78 | { |
| 79 | int c, old; |
| 80 | c = atomic_read(v); |
| 81 | for (;;) { |
| 82 | if (unlikely(c == (u))) |
| 83 | break; |
| 84 | old = atomic_cmpxchg((v), c, c + (a)); |
| 85 | if (likely(old == c)) |
| 86 | break; |
| 87 | c = old; |
| 88 | } |
| 89 | return c != (u); |
| 90 | } |
| 91 | |
Nick Piggin | 8426e1f | 2005-11-13 16:07:25 -0800 | [diff] [blame] | 92 | #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0) |
| 93 | |
Mathieu Desnoyers | 2549c85 | 2007-05-08 00:34:34 -0700 | [diff] [blame] | 94 | #define atomic64_cmpxchg(v, o, n) \ |
| 95 | ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n))) |
| 96 | #define atomic64_xchg(v, new) (xchg(&((v)->counter), new)) |
| 97 | |
Mathieu Desnoyers | 2856f5e | 2007-05-08 00:34:38 -0700 | [diff] [blame] | 98 | static __inline__ int atomic64_add_unless(atomic64_t *v, long a, long u) |
| 99 | { |
| 100 | long c, old; |
| 101 | c = atomic64_read(v); |
| 102 | for (;;) { |
| 103 | if (unlikely(c == (u))) |
| 104 | break; |
| 105 | old = atomic64_cmpxchg((v), c, c + (a)); |
| 106 | if (likely(old == c)) |
| 107 | break; |
| 108 | c = old; |
| 109 | } |
| 110 | return c != (u); |
| 111 | } |
| 112 | |
Mathieu Desnoyers | 2549c85 | 2007-05-08 00:34:34 -0700 | [diff] [blame] | 113 | #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0) |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* Atomic operations are already serializing */ |
| 116 | #ifdef CONFIG_SMP |
David S. Miller | 4f07118 | 2005-08-29 12:46:22 -0700 | [diff] [blame] | 117 | #define smp_mb__before_atomic_dec() membar_storeload_loadload(); |
| 118 | #define smp_mb__after_atomic_dec() membar_storeload_storestore(); |
| 119 | #define smp_mb__before_atomic_inc() membar_storeload_loadload(); |
| 120 | #define smp_mb__after_atomic_inc() membar_storeload_storestore(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | #else |
| 122 | #define smp_mb__before_atomic_dec() barrier() |
| 123 | #define smp_mb__after_atomic_dec() barrier() |
| 124 | #define smp_mb__before_atomic_inc() barrier() |
| 125 | #define smp_mb__after_atomic_inc() barrier() |
| 126 | #endif |
| 127 | |
Christoph Lameter | d3cb487 | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 128 | #include <asm-generic/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | #endif /* !(__ARCH_SPARC64_ATOMIC__) */ |