blob: 683b05a57d88068527bea8d99343c2425503318c [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
6#include <asm/system.h>
Mikael Starvik8d20a542005-07-27 11:44:42 -07007#include <asm/arch/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9/*
10 * Atomic operations that C can't guarantee us. Useful for
11 * resource counting etc..
12 */
13
Mikael Starvik8d20a542005-07-27 11:44:42 -070014typedef struct { volatile int counter; } atomic_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define ATOMIC_INIT(i) { (i) }
17
18#define atomic_read(v) ((v)->counter)
19#define atomic_set(v,i) (((v)->counter) = (i))
20
21/* These should be written in asm but we do it in C for now. */
22
Adrian Bunkd9b54442005-11-07 00:58:44 -080023static inline void atomic_add(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070026 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 v->counter += i;
Mikael Starvik8d20a542005-07-27 11:44:42 -070028 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
Adrian Bunkd9b54442005-11-07 00:58:44 -080031static inline void atomic_sub(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070034 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 v->counter -= i;
Mikael Starvik8d20a542005-07-27 11:44:42 -070036 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Adrian Bunkd9b54442005-11-07 00:58:44 -080039static inline int atomic_add_return(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 unsigned long flags;
42 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -070043 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 retval = (v->counter += i);
Mikael Starvik8d20a542005-07-27 11:44:42 -070045 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return retval;
47}
48
49#define atomic_add_negative(a, v) (atomic_add_return((a), (v)) < 0)
50
Adrian Bunkd9b54442005-11-07 00:58:44 -080051static inline int atomic_sub_return(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 unsigned long flags;
54 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -070055 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 retval = (v->counter -= i);
Mikael Starvik8d20a542005-07-27 11:44:42 -070057 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return retval;
59}
60
Adrian Bunkd9b54442005-11-07 00:58:44 -080061static inline int atomic_sub_and_test(int i, volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 int retval;
64 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070065 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 retval = (v->counter -= i) == 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -070067 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return retval;
69}
70
Adrian Bunkd9b54442005-11-07 00:58:44 -080071static inline void atomic_inc(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070074 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 (v->counter)++;
Mikael Starvik8d20a542005-07-27 11:44:42 -070076 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Adrian Bunkd9b54442005-11-07 00:58:44 -080079static inline void atomic_dec(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -070082 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 (v->counter)--;
Mikael Starvik8d20a542005-07-27 11:44:42 -070084 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Adrian Bunkd9b54442005-11-07 00:58:44 -080087static inline int atomic_inc_return(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 unsigned long flags;
90 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -070091 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 retval = (v->counter)++;
Mikael Starvik8d20a542005-07-27 11:44:42 -070093 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return retval;
95}
96
Adrian Bunkd9b54442005-11-07 00:58:44 -080097static inline int atomic_dec_return(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 unsigned long flags;
100 int retval;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700101 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 retval = (v->counter)--;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700103 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return retval;
105}
Adrian Bunkd9b54442005-11-07 00:58:44 -0800106static inline int atomic_dec_and_test(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 int retval;
109 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700110 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 retval = --(v->counter) == 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700112 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return retval;
114}
115
Adrian Bunkd9b54442005-11-07 00:58:44 -0800116static inline int atomic_inc_and_test(volatile atomic_t *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 int retval;
119 unsigned long flags;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700120 cris_atomic_save(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 retval = ++(v->counter) == 0;
Mikael Starvik8d20a542005-07-27 11:44:42 -0700122 cris_atomic_restore(v, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return retval;
124}
125
Nick Piggin4a6dae62005-11-13 16:07:24 -0800126static inline int atomic_cmpxchg(atomic_t *v, int old, int new)
127{
128 int ret;
129 unsigned long flags;
130
131 cris_atomic_save(v, flags);
132 ret = v->counter;
133 if (likely(ret == old))
134 v->counter = new;
135 cris_atomic_restore(v, flags);
136 return ret;
137}
138
Nick Piggin8426e1f2005-11-13 16:07:25 -0800139static inline int atomic_add_unless(atomic_t *v, int a, int u)
140{
141 int ret;
142 unsigned long flags;
143
144 cris_atomic_save(v, flags);
145 ret = v->counter;
146 if (ret != u)
147 v->counter += a;
148 cris_atomic_restore(v, flags);
149 return ret != u;
150}
151#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Atomic operations are already serializing */
154#define smp_mb__before_atomic_dec() barrier()
155#define smp_mb__after_atomic_dec() barrier()
156#define smp_mb__before_atomic_inc() barrier()
157#define smp_mb__after_atomic_inc() barrier()
158
159#endif