From fbcc3e5004f01653b2885965c59cade25e286c18 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Thu, 12 Jan 2017 19:07:54 +0100 Subject: qemu-thread: optimize QemuLockCnt with futexes on Linux This is complex, but I think it is reasonably documented in the source. Signed-off-by: Paolo Bonzini Reviewed-by: Fam Zheng Reviewed-by: Stefan Hajnoczi Message-id: 20170112180800.21085-5-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- include/qemu/futex.h | 36 ++++++++++++++++++++++++++++++++++++ include/qemu/thread.h | 2 ++ 2 files changed, 38 insertions(+) create mode 100644 include/qemu/futex.h (limited to 'include/qemu') diff --git a/include/qemu/futex.h b/include/qemu/futex.h new file mode 100644 index 0000000..bb7dc9e --- /dev/null +++ b/include/qemu/futex.h @@ -0,0 +1,36 @@ +/* + * Wrappers around Linux futex syscall + * + * Copyright Red Hat, Inc. 2017 + * + * Author: + * Paolo Bonzini + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include +#include + +#define qemu_futex(...) syscall(__NR_futex, __VA_ARGS__) + +static inline void qemu_futex_wake(void *f, int n) +{ + qemu_futex(f, FUTEX_WAKE, n, NULL, NULL, 0); +} + +static inline void qemu_futex_wait(void *f, unsigned val) +{ + while (qemu_futex(f, FUTEX_WAIT, (int) val, NULL, NULL, 0)) { + switch (errno) { + case EWOULDBLOCK: + return; + case EINTR: + break; /* get out of switch and retry */ + default: + abort(); + } + } +} diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 5f7de7b..9910f49 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -100,7 +100,9 @@ static inline void qemu_spin_unlock(QemuSpin *spin) } struct QemuLockCnt { +#ifndef CONFIG_LINUX QemuMutex mutex; +#endif unsigned count; }; -- cgit v1.1