public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
From: Nick Piggin <nickpiggin@yahoo.com.au>
To: Dipankar Sarma <dipankar@in.ibm.com>,
	linux-arch@vger.kernel.org, Andrew Morton <akpm@osdl.org>,
	Linus Torvalds <torvalds@osdl.org>
Subject: [PATCH 2/3][RFC] atomic_cmpxchg, atomic_inc_not_zero
Date: Tue, 13 Sep 2005 00:53:14 +1000	[thread overview]
Message-ID: <4325965A.2010608@yahoo.com.au> (raw)
In-Reply-To: <432595D5.1090502@yahoo.com.au>

[-- Attachment #1: Type: text/plain, Size: 384 bytes --]

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.


[-- Attachment #2: atomic_inc_not_zero.patch --]
[-- Type: text/plain, Size: 894 bytes --]

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)
 

  reply	other threads:[~2005-09-12 14:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-12 14:51 [PATCH 1/3][RFC] atomic_cmpxchg, atomic_inc_not_zero Nick Piggin
2005-09-12 14:53 ` Nick Piggin [this message]
2005-09-12 14:55   ` [PATCH 3/3][RFC] remove rcuref.h Nick Piggin
2005-09-12 15:34     ` Dipankar Sarma
2005-09-12 15:30 ` [PATCH 1/3][RFC] atomic_cmpxchg, atomic_inc_not_zero Dipankar Sarma
2005-09-12 15:42   ` Nick Piggin
2005-09-12 16:11     ` Ralf Baechle
2005-09-12 20:46     ` David S. Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4325965A.2010608@yahoo.com.au \
    --to=nickpiggin@yahoo.com.au \
    --cc=akpm@osdl.org \
    --cc=dipankar@in.ibm.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox