Skip to contentSkip to conversation

lib: only test atomic64_dec_if_positive on archs having it

PATCH In review Luca Barbieri 1 participant 4 messages Feb 26, 2010 – Mar 1, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lib: only test atomic64_dec_if_positive on archs having it

Only supported by ppc, mips and x86-32 with my patchset.

---
 lib/atomic64_test.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
lib/atomic64_test.c +4 -0
index 4ff649e..0effcac 100644
@@ -112,6 +112,7 @@ static __init int test_atomic64(void)
 	r += one;
 	BUG_ON(v.counter != r);
 
+#if defined(CONFIG_X86_32) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
 	INIT(onestwos);
 	BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
 	r -= one;
@@ -124,6 +125,9 @@ static __init int test_atomic64(void)
 	INIT(-one);
 	BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
 	BUG_ON(v.counter != r);
+#else
+#warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above
+#endif
 
 	INIT(onestwos);
 	BUG_ON(atomic64_inc_not_zero(&v));
Signature · 1 line
1.6.6.1.476.g01ddb
#1 lore raw

PATCH 2/2 x86: implement atomic[64]_dec_if_positive

They were missing.

atomic64_dec_if_positive for x86-32 is implemented in my atomic64 improvements for x86-32 patchset.

---
 arch/x86/include/asm/atomic.h      |   23 +++++++++++++++++++++++
 arch/x86/include/asm/atomic64_64.h |   23 +++++++++++++++++++++++
 lib/atomic64_test.c                |    2 +-
 3 files changed, 47 insertions(+), 1 deletions(-)
arch/x86/include/asm/atomic.h +23 -0
index 8f8217b..706c694 100644
@@ -246,6 +246,29 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 
 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
 
+/*
+ * 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
arch/x86/include/asm/atomic64_64.h +23 -0
index 51c5b40..4d6e2cd 100644
@@ -221,4 +221,27 @@ static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
 
 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
 
+/*
+ * atomic64_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 long atomic64_dec_if_positive(atomic64_t *v)
+{
+	long c, old, dec;
+	c = atomic64_read(v);
+	for (;;) {
+		dec = c - 1;
+		if (unlikely(dec < 0))
+			break;
+		old = atomic64_cmpxchg((v), c, dec);
+		if (likely(old == c))
+			break;
+		c = old;
+	}
+	return dec;
+}
+
 #endif /* _ASM_X86_ATOMIC64_64_H */
lib/atomic64_test.c +1 -1
index 0effcac..58efdab 100644
@@ -112,7 +112,7 @@ static __init int test_atomic64(void)
 	r += one;
 	BUG_ON(v.counter != r);
 
-#if defined(CONFIG_X86_32) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
+#if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
 	INIT(onestwos);
 	BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
 	r -= one;
Signature · 1 line
1.6.6.1.476.g01ddb
#2 lore raw

[tip:x86/atomic] x86: Implement atomic[64]_dec_if_positive()

Commit-ID:  d7f6de1e9c4a12e11ba7186c70f0f40caa76f590
Gitweb:     http://git.kernel.org/tip/d7f6de1e9c4a12e11ba7186c70f0f40caa76f590
Author:     Luca Barbieri <[email protected]>
AuthorDate: Fri, 26 Feb 2010 12:22:41 +0100
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 1 Mar 2010 11:38:42 -0800

x86: Implement atomic[64]_dec_if_positive()

Add support for atomic_dec_if_positive(), and
atomic64_dec_if_positive() for x86-64.

atomic64_dec_if_positive() for x86-32 was already implemented in a previous patch.

LKML-Reference: <[email protected]>

---
 arch/x86/include/asm/atomic.h      |   23 +++++++++++++++++++++++
 arch/x86/include/asm/atomic64_64.h |   23 +++++++++++++++++++++++
 lib/atomic64_test.c                |    2 +-
 3 files changed, 47 insertions(+), 1 deletions(-)
arch/x86/include/asm/atomic.h +23 -0
index 8f8217b..706c694 100644
@@ -246,6 +246,29 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 
 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
 
+/*
+ * 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
arch/x86/include/asm/atomic64_64.h +23 -0
index 51c5b40..4d6e2cd 100644
@@ -221,4 +221,27 @@ static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
 
 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
 
+/*
+ * atomic64_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 long atomic64_dec_if_positive(atomic64_t *v)
+{
+	long c, old, dec;
+	c = atomic64_read(v);
+	for (;;) {
+		dec = c - 1;
+		if (unlikely(dec < 0))
+			break;
+		old = atomic64_cmpxchg((v), c, dec);
+		if (likely(old == c))
+			break;
+		c = old;
+	}
+	return dec;
+}
+
 #endif /* _ASM_X86_ATOMIC64_64_H */
lib/atomic64_test.c +1 -1
index 0effcac..58efdab 100644
@@ -112,7 +112,7 @@ static __init int test_atomic64(void)
 	r += one;
 	BUG_ON(v.counter != r);
 
-#if defined(CONFIG_X86_32) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
+#if defined(CONFIG_X86) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
 	INIT(onestwos);
 	BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
 	r -= one;
#3 lore raw

[tip:x86/atomic] lib: Only test atomic64_dec_if_positive on archs having it

In reply to Luca Barbieri
Commit-ID:  8f4f202b335144bf5be5c9e5b1bc9477ecdae958
Gitweb:     http://git.kernel.org/tip/8f4f202b335144bf5be5c9e5b1bc9477ecdae958
Author:     Luca Barbieri <[email protected]>
AuthorDate: Fri, 26 Feb 2010 12:22:40 +0100
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Mon, 1 Mar 2010 11:37:55 -0800

lib: Only test atomic64_dec_if_positive on archs having it

Currently atomic64_dec_if_positive() is only supported by PowerPC, MIPS and x86-32.

LKML-Reference: <[email protected]>

---
 lib/atomic64_test.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
lib/atomic64_test.c +4 -0
index 4ff649e..0effcac 100644
@@ -112,6 +112,7 @@ static __init int test_atomic64(void)
 	r += one;
 	BUG_ON(v.counter != r);
 
+#if defined(CONFIG_X86_32) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(_ASM_GENERIC_ATOMIC64_H)
 	INIT(onestwos);
 	BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
 	r -= one;
@@ -124,6 +125,9 @@ static __init int test_atomic64(void)
 	INIT(-one);
 	BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
 	BUG_ON(v.counter != r);
+#else
+#warning Please implement atomic64_dec_if_positive for your architecture, and add it to the IF above
+#endif
 
 	INIT(onestwos);
 	BUG_ON(atomic64_inc_not_zero(&v));