Skip to content

Commit e79bee2

Browse files
Shaohua Litorvalds
authored andcommitted
atomic: implement generic atomic_dec_if_positive()
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>
1 parent 435b405 commit e79bee2

4 files changed

Lines changed: 27 additions & 24 deletions

File tree

‎arch/microblaze/include/asm/atomic.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ static inline int atomic_dec_if_positive(atomic_t *v)
2222

2323
return res;
2424
}
25+
#define atomic_dec_if_positive atomic_dec_if_positive
2526

2627
#endif /* _ASM_MICROBLAZE_ATOMIC_H */

‎arch/powerpc/include/asm/atomic.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ static __inline__ int atomic_dec_if_positive(atomic_t *v)
268268

269269
return t;
270270
}
271+
#define atomic_dec_if_positive atomic_dec_if_positive
271272

272273
#define smp_mb__before_atomic_dec() smp_mb()
273274
#define smp_mb__after_atomic_dec() smp_mb()

‎arch/x86/include/asm/atomic.h‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -240,30 +240,6 @@ static inline int __atomic_add_unless(atomic_t *v, int a, int u)
240240
return c;
241241
}
242242

243-
244-
/*
245-
* atomic_dec_if_positive - decrement by 1 if old value positive
246-
* @v: pointer of type atomic_t
247-
*
248-
* The function returns the old value of *v minus 1, even if
249-
* the atomic variable, v, was not decremented.
250-
*/
251-
static inline int atomic_dec_if_positive(atomic_t *v)
252-
{
253-
int c, old, dec;
254-
c = atomic_read(v);
255-
for (;;) {
256-
dec = c - 1;
257-
if (unlikely(dec < 0))
258-
break;
259-
old = atomic_cmpxchg((v), c, dec);
260-
if (likely(old == c))
261-
break;
262-
c = old;
263-
}
264-
return dec;
265-
}
266-
267243
/**
268244
* atomic_inc_short - increment of a short integer
269245
* @v: pointer to type int

‎include/linux/atomic.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ static inline int atomic_dec_unless_positive(atomic_t *p)
8686
}
8787
#endif
8888

89+
/*
90+
* atomic_dec_if_positive - decrement by 1 if old value positive
91+
* @v: pointer of type atomic_t
92+
*
93+
* The function returns the old value of *v minus 1, even if
94+
* the atomic variable, v, was not decremented.
95+
*/
96+
#ifndef atomic_dec_if_positive
97+
static inline int atomic_dec_if_positive(atomic_t *v)
98+
{
99+
int c, old, dec;
100+
c = atomic_read(v);
101+
for (;;) {
102+
dec = c - 1;
103+
if (unlikely(dec < 0))
104+
break;
105+
old = atomic_cmpxchg((v), c, dec);
106+
if (likely(old == c))
107+
break;
108+
c = old;
109+
}
110+
return dec;
111+
}
112+
#endif
113+
89114
#ifndef CONFIG_ARCH_HAS_ATOMIC_OR
90115
static inline void atomic_or(int i, atomic_t *v)
91116
{

0 commit comments

Comments
 (0)