blob: 0b51a87e5532e9025111b6c5177bf8c964d58f22 [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
Ingo Molnarffbf6702006-01-09 15:59:17 -0800139#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
140
Nick Piggin8426e1f2005-11-13 16:07:25 -0800141static inline int atomic_add_unless(atomic_t *v, int a, int u)
142{
143 int ret;
144 unsigned long flags;
145
146 cris_atomic_save(v, flags);
147 ret = v->counter;
148 if (ret != u)
149 v->counter += a;
150 cris_atomic_restore(v, flags);
151 return ret != u;
152}
153#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* Atomic operations are already serializing */
156#define smp_mb__before_atomic_dec() barrier()
157#define smp_mb__after_atomic_dec() barrier()
158#define smp_mb__before_atomic_inc() barrier()
159#define smp_mb__after_atomic_inc() barrier()
160
Christoph Lameterd3cb4872006-01-06 00:11:20 -0800161#include <asm-generic/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#endif