From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47520) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEws-000619-EN for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:46:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoEuj-0008GI-Sp for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:44:22 -0400 Received: from lists.gnu.org ([2001:4830:134:3::11]:58532) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEuj-0008G8-Po for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:44:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56276) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEAK-0002g4-QO for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:56:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoDnR-0003X1-L5 for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:32:48 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:54453) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoDnR-0003WE-Fg for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:32:45 -0400 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id D0AF32134F for ; Thu, 7 Apr 2016 13:32:44 -0400 (EDT) From: "Emilio G. Cota" Date: Thu, 7 Apr 2016 13:32:33 -0400 Message-Id: <1460050358-25025-9-git-send-email-cota@braap.org> In-Reply-To: <1460050358-25025-1-git-send-email-cota@braap.org> References: <1460050358-25025-1-git-send-email-cota@braap.org> Subject: [Qemu-devel] [PATCH v2 08/13] qemu-thread: optimize spin_lock for uncontended locks List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: QEMU Developers , MTTCG Devel Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Paolo Bonzini , Peter Crosthwaite , Richard Henderson , Peter Maydell , Sergey Fedorov This way we can acquire the lock with xchg+test, instead of test+xchg+test. Most spinlocks should be uncontended so this should result in a ne performance gain. Before: 4ad957: eb 09 jmp 4ad962 4ad959: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 4ad960: f3 90 pause 4ad962: 8b 03 mov (%rbx),%eax 4ad964: 85 c0 test %eax,%eax 4ad966: 75 f8 jne 4ad960 4ad968: 89 f8 mov %edi,%eax 4ad96a: 87 03 xchg %eax,(%rbx) 4ad96c: 85 c0 test %eax,%eax 4ad96e: 75 f2 jne 4ad962 After: 4ad980: 89 f8 mov %edi,%eax 4ad982: 87 03 xchg %eax,(%rbx) 4ad984: 85 c0 test %eax,%eax 4ad986: 74 12 je 4ad99a 4ad988: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1) 4ad98f: 00 4ad990: 8b 03 mov (%rbx),%eax 4ad992: 85 c0 test %eax,%eax 4ad994: 74 ea je 4ad980 4ad996: f3 90 pause 4ad998: eb f6 jmp 4ad990 Signed-off-by: Emilio G. Cota --- include/qemu/thread.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 599965e..e2af57c 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -74,11 +74,11 @@ static inline void qemu_spin_init(QemuSpin *spin) static inline void qemu_spin_lock(QemuSpin *spin) { - do { + while (atomic_xchg(&spin->value, true)) { while (atomic_read(&spin->value)) { cpu_relax(); } - } while (atomic_xchg(&spin->value, true)); + } } static inline int qemu_spin_trylock(QemuSpin *spin) -- 2.5.0