blob: c3feb3af2cfe2e453f310ab6645c6ad15a2ec73f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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 Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12
13typedef struct { volatile int counter; } atomic_t;
14typedef 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
25extern void atomic_add(int, atomic_t *);
26extern void atomic64_add(int, atomic64_t *);
27extern void atomic_sub(int, atomic_t *);
28extern void atomic64_sub(int, atomic64_t *);
29
30extern int atomic_add_ret(int, atomic_t *);
31extern int atomic64_add_ret(int, atomic64_t *);
32extern int atomic_sub_ret(int, atomic_t *);
33extern 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)
Hugh Dickins7c72aaf2005-11-23 13:37:40 -080056#define atomic64_inc_and_test(v) (atomic64_inc_return(v) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
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
Mathieu Desnoyers2549c852007-05-08 00:34:34 -070073#define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
Ingo Molnarffbf6702006-01-09 15:59:17 -080074#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
Nick Piggin4a6dae62005-11-13 16:07:24 -080075
Nick Piggin8426e1f2005-11-13 16:07:25 -080076#define atomic_add_unless(v, a, u) \
77({ \
Mathieu Desnoyers2549c852007-05-08 00:34:34 -070078 __typeof__((v)->counter) c, old; \
Nick Piggin8426e1f2005-11-13 16:07:25 -080079 c = atomic_read(v); \
Nick Piggin0b2fcfd2006-03-23 03:01:02 -080080 for (;;) { \
81 if (unlikely(c == (u))) \
82 break; \
83 old = atomic_cmpxchg((v), c, c + (a)); \
84 if (likely(old == c)) \
85 break; \
Nick Piggin8426e1f2005-11-13 16:07:25 -080086 c = old; \
Nick Piggin0b2fcfd2006-03-23 03:01:02 -080087 } \
88 likely(c != (u)); \
Nick Piggin8426e1f2005-11-13 16:07:25 -080089})
90#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
91
Mathieu Desnoyers2549c852007-05-08 00:34:34 -070092#define atomic64_cmpxchg(v, o, n) \
93 ((__typeof__((v)->counter))cmpxchg(&((v)->counter), (o), (n)))
94#define atomic64_xchg(v, new) (xchg(&((v)->counter), new))
95
96#define atomic64_add_unless(v, a, u) \
97({ \
98 __typeof__((v)->counter) c, old; \
99 c = atomic64_read(v); \
100 for (;;) { \
101 if (unlikely(c == (u))) \
102 break; \
103 old = atomic64_cmpxchg((v), c, c + (a)); \
104 if (likely(old == c)) \
105 break; \
106 c = old; \
107 } \
108 likely(c != (u)); \
109})
110#define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* Atomic operations are already serializing */
113#ifdef CONFIG_SMP
David S. Miller4f071182005-08-29 12:46:22 -0700114#define smp_mb__before_atomic_dec() membar_storeload_loadload();
115#define smp_mb__after_atomic_dec() membar_storeload_storestore();
116#define smp_mb__before_atomic_inc() membar_storeload_loadload();
117#define smp_mb__after_atomic_inc() membar_storeload_storestore();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#else
119#define smp_mb__before_atomic_dec() barrier()
120#define smp_mb__after_atomic_dec() barrier()
121#define smp_mb__before_atomic_inc() barrier()
122#define smp_mb__after_atomic_inc() barrier()
123#endif
124
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800125#include <asm-generic/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif /* !(__ARCH_SPARC64_ATOMIC__) */