1 #ifndef __ARCH_I386_ATOMIC__
2 #define __ARCH_I386_ATOMIC__
14 #define LOCK "lock ; "
24 typedef struct {
volatile int counter; }
atomic_t;
26 #define ATOMIC_INIT(i) { (i) }
35 #define atomic_read(v) ((v)->counter)
45 #define atomic_set(v,i) (((v)->counter) = (i))
55 static __inline__
void atomic_add(
int i,
atomic_t *v)
60 :
"ir" (i),
"m" (v->counter));
71 static __inline__
void atomic_sub(
int i,
atomic_t *v)
76 :
"ir" (i),
"m" (v->counter));
89 static __inline__
int atomic_sub_and_test(
int i,
atomic_t *v)
94 LOCK
"subl %2,%0; sete %1"
95 :
"=m" (v->counter),
"=qm" (c)
96 :
"ir" (i),
"m" (v->counter) :
"memory");
107 static __inline__
void atomic_inc(
atomic_t *v)
109 __asm__ __volatile__(
122 static __inline__
void atomic_dec(
atomic_t *v)
124 __asm__ __volatile__(
139 static __inline__
int atomic_dec_and_test(
atomic_t *v)
143 __asm__ __volatile__(
144 LOCK
"decl %0; sete %1"
145 :
"=m" (v->counter),
"=qm" (c)
146 :
"m" (v->counter) :
"memory");
159 static __inline__
int atomic_inc_and_test(
atomic_t *v)
163 __asm__ __volatile__(
164 LOCK
"incl %0; sete %1"
165 :
"=m" (v->counter),
"=qm" (c)
166 :
"m" (v->counter) :
"memory");
180 static __inline__
int atomic_add_negative(
int i,
atomic_t *v)
184 __asm__ __volatile__(
185 LOCK
"addl %2,%0; sets %1"
186 :
"=m" (v->counter),
"=qm" (c)
187 :
"ir" (i),
"m" (v->counter) :
"memory");
192 static inline int atomic_inc_if_nonzero(
atomic_t *v)
194 int val = atomic_read(v);
198 __asm__ __volatile__(
199 LOCK
"cmpxchgl %3,%1 ; setz %2"
200 :
"=a" (val),
"=m" (v->counter),
"=qm" (c)
201 :
"r" (val + 1),
"0" (val),
"m" (v->counter)
210 #define atomic_clear_mask(mask, addr) \
211 __asm__ __volatile__(LOCK "andl %0,%1" \
212 : : "r" (~(mask)),"m" (*addr) : "memory")
214 #define atomic_set_mask(mask, addr) \
215 __asm__ __volatile__(LOCK "orl %0,%1" \
216 : : "r" (mask),"m" (*addr) : "memory")
219 #define smp_mb__before_atomic_dec() barrier()
220 #define smp_mb__after_atomic_dec() barrier()
221 #define smp_mb__before_atomic_inc() barrier()
222 #define smp_mb__after_atomic_inc() barrier()