blob: 5fc87768774a25cd0b438550a39d753d27513c85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: atomic.h,v 1.3 2001/07/25 16:15:19 bjornw Exp $ */
2
3#ifndef __ASM_CRIS_ATOMIC__
4#define __ASM_CRIS_ATOMIC__
5
Andrew Morton95879972007-11-14 17:00:51 -08006#include <linux/compiler.h>
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/system.h>
Mikael Starvik8d20a542005-07-27 11:44:42 -07009#include <asm/arch/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11/*
12 * Atomic operations that C can't guarantee us. Useful for
13 * resource counting etc..
14 */
15
Mikael Starvik8d20a542005-07-27 11:44:42 -070016typedef struct { volatile int counter; } atomic_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define ATOMIC_INIT(i) { (i) }
19
20#define atomic_read(v) ((v)->counter)
21#define atomic_set(v,i) (((v)->counter) = (i))
22
23/* These should be written in asm but we do it in C for now. */
24
Adrian Bunkd9b54442005-11-07 00:58:44 -080025static inline void atomic_add(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070028 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 v->counter += i;
Mikael Starvik8d20a542005-07-27 11:44:42 -070030 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
Adrian Bunkd9b54442005-11-07 00:58:44 -080033static inline void atomic_sub(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070036 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 v->counter -= i;
Mikael Starvik8d20a542005-07-27 11:44:42 -070038 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Adrian Bunkd9b54442005-11-07 00:58:44 -080041static inline int atomic_add_return(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 unsigned long flags;
44 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -070045 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 retval = (v->counter += i);
Mikael Starvik8d20a542005-07-27 11:44:42 -070047 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return retval;
49}
50
51#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
52
Adrian Bunkd9b54442005-11-07 00:58:44 -080053static inline int atomic_sub_return(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 unsigned long flags;
56 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -070057 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 retval = (v->counter -= i);
Mikael Starvik8d20a542005-07-27 11:44:42 -070059 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return retval;
61}
62
Adrian Bunkd9b54442005-11-07 00:58:44 -080063static inline int atomic_sub_and_test(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 int retval;
66 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070067 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 retval = (v->counter -= i) == 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -070069 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return retval;
71}
72
Adrian Bunkd9b54442005-11-07 00:58:44 -080073static inline void atomic_inc(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070076 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 (v->counter)++;
Mikael Starvik8d20a542005-07-27 11:44:42 -070078 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Adrian Bunkd9b54442005-11-07 00:58:44 -080081static inline void atomic_dec(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070084 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 (v->counter)--;
Mikael Starvik8d20a542005-07-27 11:44:42 -070086 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Adrian Bunkd9b54442005-11-07 00:58:44 -080089static inline int atomic_inc_return(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 unsigned long flags;
92 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -070093 cris_atomic_save(v, flags);
Jesper Nilsson3c1d9302008-01-21 17:01:31 +010094 retval = ++(v->counter);
Mikael Starvik8d20a542005-07-27 11:44:42 -070095 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return retval;
97}
98
Adrian Bunkd9b54442005-11-07 00:58:44 -080099static inline int atomic_dec_return(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 unsigned long flags;
102 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700103 cris_atomic_save(v, flags);
Jesper Nilsson3c1d9302008-01-21 17:01:31 +0100104 retval = --(v->counter);
Mikael Starvik8d20a542005-07-27 11:44:42 -0700105 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return retval;
107}
Adrian Bunkd9b54442005-11-07 00:58:44 -0800108static inline int atomic_dec_and_test(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int retval;
111 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700112 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 retval = --(v->counter) == 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700114 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return retval;
116}
117
Adrian Bunkd9b54442005-11-07 00:58:44 -0800118static inline int atomic_inc_and_test(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int retval;
121 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700122 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 retval = ++(v->counter) == 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700124 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return retval;
126}
127
Nick Piggin4a6dae62005-11-13 16:07:24 -0800128static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
129{
130 int ret;
131 unsigned long flags;
132
133 cris_atomic_save(v, flags);
134 ret = v->counter;
135 if (likely(ret == old))
136 v->counter = new;
137 cris_atomic_restore(v, flags);
138 return ret;
139}
140
Ingo Molnarffbf6702006-01-09 15:59:17 -0800141#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
142
Nick Piggin8426e1f2005-11-13 16:07:25 -0800143static inline int atomic_add_unless(atomic_t *v, int a, int u)
144{
145 int ret;
146 unsigned long flags;
147
148 cris_atomic_save(v, flags);
149 ret = v->counter;
150 if (ret != u)
151 v->counter += a;
152 cris_atomic_restore(v, flags);
153 return ret != u;
154}
155#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Atomic operations are already serializing */
158#define smp_mb__before_atomic_dec() barrier()
159#define smp_mb__after_atomic_dec() barrier()
160#define smp_mb__before_atomic_inc() barrier()
161#define smp_mb__after_atomic_inc() barrier()
162
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800163#include <asm-generic/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif