2/3 is a sample atomic_inc_not_zero for ppc64 implemented with
atomic_cmpxchg. I couldn't be bothered to go through the motions
without a first round of comments...
Ideally, ppc64 will implement this with ll/sc instructions and
hopefully shave a cycle or so. But anyone who can implement
atomic_cmpxchg can implement atomic_inc_not_zero as demonstrated.
--
SUSE Labs, Novell Inc.
Index: linux-2.6/include/asm-ppc64/atomic.h
===================================================================
--- linux-2.6.orig/include/asm-ppc64/atomic.h
+++ linux-2.6/include/asm-ppc64/atomic.h
@@ -164,6 +164,23 @@ static __inline__ int atomic_dec_return(
#define atomic_cmpxchg(v, o, n) ((int)cmpxchg(&((v)->counter), (o), (n)))
+/**
+ * atomic_inc_not_zero - increment if not zero
+ * @v: pointer of type atomic_t
+ *
+ * Atomically increments @v by 1, so long as it was not 0.
+ * Returns non-zero on successful increment and zero otherwise.
+ */
+#define atomic_inc_not_zero(v) \
+({ \
+ int c, old; \
+ c = atomic_read(v); \
+ while (c && (old = atomic_cmpxchg((v), c, c + 1)) != c) \
+ c = old; \
+ c; \
+})
+
+
#define atomic_sub_and_test(a, v) (atomic_sub_return((a), (v)) == 0)
#define atomic_dec_and_test(v) (atomic_dec_return((v)) == 0)