From: Liu Ping Fan <qemulist@gmail.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PATCH] atomic: using memory_order_relaxed for refcnt inc/dec ops
Date: Fri, 12 Jul 2013 14:32:51 +0800 [thread overview]
Message-ID: <1373610771-11819-1-git-send-email-pingfank@linux.vnet.ibm.com> (raw)
Refcnt's atomic inc/dec ops are frequent and its idiom need no seq_cst
order. So to get better performance, it worth to adopt _relaxed
other than _seq_cst memory model on them.
We resort to gcc builtins. If gcc supports C11 memory model, __atomic_*
buitlins is used, otherwise __sync_* builtins.
Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
---
include/qemu/atomic.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h
index 0aa8913..1f474b7 100644
--- a/include/qemu/atomic.h
+++ b/include/qemu/atomic.h
@@ -183,8 +183,15 @@
#endif
/* Provide shorter names for GCC atomic builtins. */
+#ifndef _GLIBCXX_ATOMIC_BUILTINS
+/* close to C11 memory_order_seq_cst */
#define atomic_fetch_inc(ptr) __sync_fetch_and_add(ptr, 1)
#define atomic_fetch_dec(ptr) __sync_fetch_and_add(ptr, -1)
+#else
+/* C11 memory_order_relaxed */
+#define atomic_fetch_inc(ptr) __atomic_fetch_add(ptr, 1, __ATOMIC_RELAXED)
+#define atomic_fetch_dec(ptr) __atomic_fetch_add(ptr, -1, __ATOMIC_RELAXED)
+#endif
#define atomic_fetch_add __sync_fetch_and_add
#define atomic_fetch_sub __sync_fetch_and_sub
#define atomic_fetch_and __sync_fetch_and_and
@@ -192,8 +199,15 @@
#define atomic_cmpxchg __sync_val_compare_and_swap
/* And even shorter names that return void. */
+#ifndef _GLIBCXX_ATOMIC_BUILTINS
+/* close to C11 memory_order_seq_cst */
#define atomic_inc(ptr) ((void) __sync_fetch_and_add(ptr, 1))
#define atomic_dec(ptr) ((void) __sync_fetch_and_add(ptr, -1))
+#else
+/* C11 memory_order_relaxed */
+#define atomic_inc(ptr) ((void) __atomic_fetch_add(ptr, 1, __ATOMIC_RELAXED))
+#define atomic_dec(ptr) ((void) __atomic_fetch_add(ptr, -1, __ATOMIC_RELAXED))
+#endif
#define atomic_add(ptr, n) ((void) __sync_fetch_and_add(ptr, n))
#define atomic_sub(ptr, n) ((void) __sync_fetch_and_sub(ptr, n))
#define atomic_and(ptr, n) ((void) __sync_fetch_and_and(ptr, n))
--
1.8.1.4
next reply other threads:[~2013-07-12 6:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-12 6:32 Liu Ping Fan [this message]
2013-07-12 16:24 ` [Qemu-devel] [PATCH] atomic: using memory_order_relaxed for refcnt inc/dec ops Richard Henderson
2013-07-14 2:18 ` liu ping fan
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=1373610771-11819-1-git-send-email-pingfank@linux.vnet.ibm.com \
--to=qemulist@gmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.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;
as well as URLs for NNTP newsgroup(s).