atomic: using memory_order_relaxed for refcnt inc/dec ops
PATCH 1/1 atomic: using memory_order_relaxed for refcnt inc/dec ops
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.
--- include/qemu/atomic.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/qemu/atomic.h +14 -0
@@ -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))
Signature · 1 line
1.8.1.4
Quoted 3 lines · Liu Ping Fan
Refcnt's atomic inc/dec ops are frequent and its idiom need no
seq_cstorder. So to get better performance, it worth to adopt_relaxedother than_seq_cstmemory model on them.
You'd need to update the documentation then. As it stands, what you've written looks like a bug.
Quoted 1 line
+#ifndef
_GLIBCXX_ATOMIC_BUILTINS
This will never be defined. It's private to the libstdc++ implementation. See how we've defined things using __atomic elsewhere in the file, looking at one of the __ATOMIC defines.
And in either case, it's better form to use positive tests than negative ones. I.e. #ifdef rather than #ifndef
Quoted 2 lines
#define
atomic_fetch_inc(ptr)__sync_fetch_and_add(ptr, 1)#defineatomic_fetch_dec(ptr)__sync_fetch_and_add(ptr, -1)
I'd prefer atomic_fetch_inc_relaxed, as that's more self-documenting.
But I'll re-iterate the necessity of documentation in this area.
r~
Quoted 8 lines · Richard Henderson
Quoted 3 lines · Liu Ping Fan
Refcnt's atomic inc/dec ops are frequent and its idiom need no
seq_cstorder. So to get better performance, it worth to adopt_relaxedother than_seq_cstmemory model on them.You'd need to update the documentation then. As it stands, what you've written looks like a bug.
Ok, will update atomic.txt
Quoted 6 lines
Quoted 1 line
+#ifndef
_GLIBCXX_ATOMIC_BUILTINSThis will never be defined. It's private to the libstdc++ implementation. See how we've defined things using
__atomicelsewhere in the file, looking at one of the__ATOMICdefines.
Oh, I misunderstood the description of _GLIBCXX_ATOMIC_BUILTINS. Got it, thanks.
Quoted 3 lines
And in either case, it's better form to use positive tests than negative ones. I.e. #ifdef rather than #ifndef
Will fix.
Quoted 5 lines
Quoted 2 lines
#define
atomic_fetch_inc(ptr)__sync_fetch_and_add(ptr, 1)#defineatomic_fetch_dec(ptr)__sync_fetch_and_add(ptr, -1)I'd prefer
atomic_fetch_inc_relaxed, as that's more self-documenting.
But if _relaxed not supported, it will fall back on _seq_cst, then atomic_fetch_inc_relaxed will be wrong named?
Thanks and regards,
Pingfan
Quoted 4 lines
But I'll re-iterate the necessity of documentation in this area.
r~