From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38212) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b37Uz-00075S-Jy for qemu-devel@nongnu.org; Wed, 18 May 2016 15:51:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b37Uu-00083J-Jy for qemu-devel@nongnu.org; Wed, 18 May 2016 15:51:16 -0400 Received: from mail-lf0-x243.google.com ([2a00:1450:4010:c07::243]:34802) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b37Uu-00083F-Bc for qemu-devel@nongnu.org; Wed, 18 May 2016 15:51:12 -0400 Received: by mail-lf0-x243.google.com with SMTP id m101so3881546lfi.1 for ; Wed, 18 May 2016 12:51:12 -0700 (PDT) References: <1463196873-17737-1-git-send-email-cota@braap.org> <1463196873-17737-8-git-send-email-cota@braap.org> From: Sergey Fedorov Message-ID: <573CC7AD.2040108@gmail.com> Date: Wed, 18 May 2016 22:51:09 +0300 MIME-Version: 1.0 In-Reply-To: <1463196873-17737-8-git-send-email-cota@braap.org> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v5 07/18] qemu-thread: add simple test-and-set spinlock List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Emilio G. Cota" , QEMU Developers , MTTCG Devel Cc: =?UTF-8?Q?Alex_Benn=c3=a9e?= , Paolo Bonzini , Peter Crosthwaite , Richard Henderson On 14/05/16 06:34, Emilio G. Cota wrote: > +static inline void qemu_spin_lock(QemuSpin *spin) > +{ > + while (atomic_test_and_set_acquire(&spin->value)) { A possible optimization might be using unlikely() here, copmare: spin.o: file format elf64-littleaarch64 Disassembly of section .text: 0000000000000000 : 0: 52800022 mov w2, #0x1 // #1 4: 885ffc01 ldaxr w1, [x0] 8: 88037c02 stxr w3, w2, [x0] c: 35ffffc3 cbnz w3, 4 10: 340000a1 cbz w1, 24 14: b9400001 ldr w1, [x0] 18: 34ffff61 cbz w1, 4 1c: d503203f yield 20: 17fffffd b 14 24: d65f03c0 ret 0000000000000028 : 28: 52800022 mov w2, #0x1 // #1 2c: 885ffc01 ldaxr w1, [x0] 30: 88037c02 stxr w3, w2, [x0] 34: 35ffffc3 cbnz w3, 2c 38: 35000061 cbnz w1, 44 3c: d65f03c0 ret 40: d503203f yield 44: b9400001 ldr w1, [x0] 48: 35ffffc1 cbnz w1, 40 4c: 17fffff8 b 2c spin_lock__hint(), the one where unlikely() used, gives a bit more CPU-pipeline-friendly fast-path. > + while (atomic_read(&spin->value)) { > + cpu_relax(); > + } > + } > +} > + > +static inline int qemu_spin_trylock(QemuSpin *spin) > +{ > + if (atomic_test_and_set_acquire(&spin->value)) { > + return -EBUSY; > + } > + return 0; > +} Here we could also benefit from unlikely(), I think. Kind regards, Sergey