2011-07-26 17:09:08 -06:00
|
|
|
/* Atomic operations usable in machine independent code */
|
2010-11-11 15:05:08 -07:00
|
|
|
#ifndef _LINUX_ATOMIC_H
|
|
|
|
#define _LINUX_ATOMIC_H
|
|
|
|
#include <asm/atomic.h>
|
|
|
|
|
2011-07-26 17:09:07 -06:00
|
|
|
/**
|
|
|
|
* atomic_add_unless - add unless the number is already a given value
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
* @a: the amount to add to v...
|
|
|
|
* @u: ...unless v is equal to u.
|
|
|
|
*
|
|
|
|
* Atomically adds @a to @v, so long as @v was not already @u.
|
|
|
|
* Returns non-zero if @v was not @u, and zero otherwise.
|
|
|
|
*/
|
|
|
|
static inline int atomic_add_unless(atomic_t *v, int a, int u)
|
|
|
|
{
|
|
|
|
return __atomic_add_unless(v, a, u) != u;
|
|
|
|
}
|
|
|
|
|
2011-07-26 17:09:06 -06:00
|
|
|
/**
|
|
|
|
* atomic_inc_not_zero - increment unless the number is zero
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
*
|
|
|
|
* Atomically increments @v by 1, so long as @v is non-zero.
|
|
|
|
* Returns non-zero if @v was non-zero, and zero otherwise.
|
|
|
|
*/
|
2012-02-29 14:09:53 -07:00
|
|
|
#ifndef atomic_inc_not_zero
|
2011-07-26 17:09:06 -06:00
|
|
|
#define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
|
2012-02-29 14:09:53 -07:00
|
|
|
#endif
|
2011-07-26 17:09:06 -06:00
|
|
|
|
2010-11-11 15:05:08 -07:00
|
|
|
/**
|
|
|
|
* atomic_inc_not_zero_hint - increment if not null
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
* @hint: probable value of the atomic before the increment
|
|
|
|
*
|
|
|
|
* This version of atomic_inc_not_zero() gives a hint of probable
|
|
|
|
* value of the atomic. This helps processor to not read the memory
|
|
|
|
* before doing the atomic read/modify/write cycle, lowering
|
|
|
|
* number of bus transactions on some arches.
|
|
|
|
*
|
|
|
|
* Returns: 0 if increment was not done, 1 otherwise.
|
|
|
|
*/
|
|
|
|
#ifndef atomic_inc_not_zero_hint
|
|
|
|
static inline int atomic_inc_not_zero_hint(atomic_t *v, int hint)
|
|
|
|
{
|
|
|
|
int val, c = hint;
|
|
|
|
|
|
|
|
/* sanity test, should be removed by compiler if hint is a constant */
|
|
|
|
if (!hint)
|
|
|
|
return atomic_inc_not_zero(v);
|
|
|
|
|
|
|
|
do {
|
|
|
|
val = atomic_cmpxchg(v, c, c + 1);
|
|
|
|
if (val == c)
|
|
|
|
return 1;
|
|
|
|
c = val;
|
|
|
|
} while (c);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-20 08:52:57 -06:00
|
|
|
#ifndef atomic_inc_unless_negative
|
|
|
|
static inline int atomic_inc_unless_negative(atomic_t *p)
|
|
|
|
{
|
|
|
|
int v, v1;
|
|
|
|
for (v = 0; v >= 0; v = v1) {
|
|
|
|
v1 = atomic_cmpxchg(p, v, v + 1);
|
|
|
|
if (likely(v1 == v))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef atomic_dec_unless_positive
|
|
|
|
static inline int atomic_dec_unless_positive(atomic_t *p)
|
|
|
|
{
|
|
|
|
int v, v1;
|
|
|
|
for (v = 0; v <= 0; v = v1) {
|
|
|
|
v1 = atomic_cmpxchg(p, v, v - 1);
|
|
|
|
if (likely(v1 == v))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-08 17:32:18 -06:00
|
|
|
/*
|
|
|
|
* atomic_dec_if_positive - decrement by 1 if old value positive
|
|
|
|
* @v: pointer of type atomic_t
|
|
|
|
*
|
|
|
|
* The function returns the old value of *v minus 1, even if
|
|
|
|
* the atomic variable, v, was not decremented.
|
|
|
|
*/
|
|
|
|
#ifndef atomic_dec_if_positive
|
|
|
|
static inline int atomic_dec_if_positive(atomic_t *v)
|
|
|
|
{
|
|
|
|
int c, old, dec;
|
|
|
|
c = atomic_read(v);
|
|
|
|
for (;;) {
|
|
|
|
dec = c - 1;
|
|
|
|
if (unlikely(dec < 0))
|
|
|
|
break;
|
|
|
|
old = atomic_cmpxchg((v), c, dec);
|
|
|
|
if (likely(old == c))
|
|
|
|
break;
|
|
|
|
c = old;
|
|
|
|
}
|
|
|
|
return dec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-05-11 06:33:33 -06:00
|
|
|
#ifndef CONFIG_ARCH_HAS_ATOMIC_OR
|
|
|
|
static inline void atomic_or(int i, atomic_t *v)
|
|
|
|
{
|
|
|
|
int old;
|
|
|
|
int new;
|
|
|
|
|
|
|
|
do {
|
|
|
|
old = atomic_read(v);
|
|
|
|
new = old | i;
|
|
|
|
} while (atomic_cmpxchg(v, old, new) != old);
|
|
|
|
}
|
|
|
|
#endif /* #ifndef CONFIG_ARCH_HAS_ATOMIC_OR */
|
|
|
|
|
2011-07-26 17:09:08 -06:00
|
|
|
#include <asm-generic/atomic-long.h>
|
|
|
|
#ifdef CONFIG_GENERIC_ATOMIC64
|
|
|
|
#include <asm-generic/atomic64.h>
|
|
|
|
#endif
|
2010-11-11 15:05:08 -07:00
|
|
|
#endif /* _LINUX_ATOMIC_H */
|