Skip to content

Commit

Permalink
atomic: implement generic atomic_dec_if_positive()
Browse files Browse the repository at this point in the history
The x86 implementation of atomic_dec_if_positive is quite generic, so make
it available to all architectures.

This is needed for "swap: add a simple detector for inappropriate swapin
readahead".

[akpm@linux-foundation.org: do the "#define foo foo" trick in the conventional manner]
Signed-off-by: Shaohua Li <shli@fusionio.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Rik van Riel <riel@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Michal Simek <monstr@monstr.eu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Shaohua Li authored and Linus Torvalds committed Oct 9, 2012
1 parent 435b405 commit e79bee2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions arch/microblaze/include/asm/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ static inline int atomic_dec_if_positive(atomic_t *v)

return res;
}
#define atomic_dec_if_positive atomic_dec_if_positive

#endif /* _ASM_MICROBLAZE_ATOMIC_H */
1 change: 1 addition & 0 deletions arch/powerpc/include/asm/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ static __inline__ int atomic_dec_if_positive(atomic_t *v)

return t;
}
#define atomic_dec_if_positive atomic_dec_if_positive

#define smp_mb__before_atomic_dec() smp_mb()
#define smp_mb__after_atomic_dec() smp_mb()
Expand Down
24 changes: 0 additions & 24 deletions arch/x86/include/asm/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,30 +240,6 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)
return c;
}


/*
* 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.
*/
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;
}

/**
* atomic_inc_short - increment of a short integer
* @v: pointer to type int
Expand Down
25 changes: 25 additions & 0 deletions include/linux/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ static inline int atomic_dec_unless_positive(atomic_t *p)
}
#endif

/*
* 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

#ifndef CONFIG_ARCH_HAS_ATOMIC_OR
static inline void atomic_or(int i, atomic_t *v)
{
Expand Down

0 comments on commit e79bee2

Please sign in to comment.