qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Akihiko Odaki <akihiko.odaki@daynix.com>
To: Paolo Bonzini <pbonzini@redhat.com>, Stefan Weil <sw@weilnetz.de>,
	 Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>,
	 Hailiang Zhang <zhanghailiang@xfusion.com>
Cc: "Phil Dennis-Jordan" <phil@philjordan.eu>,
	qemu-devel@nongnu.org, devel@daynix.com,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Akihiko Odaki" <akihiko.odaki@daynix.com>
Subject: [PATCH v4 05/11] qemu-thread: Avoid futex abstraction for non-Linux
Date: Mon, 26 May 2025 14:29:15 +0900	[thread overview]
Message-ID: <20250526-event-v4-5-5b784cc8e1de@daynix.com> (raw)
In-Reply-To: <20250526-event-v4-0-5b784cc8e1de@daynix.com>

qemu-thread used to abstract pthread primitives into futex for the
QemuEvent implementation of POSIX systems other than Linux. However,
this abstraction has one key difference: unlike futex, pthread
primitives require an explicit destruction, and it must be ordered after
wait and wake operations.

It would be easier to perform destruction if a wait operation ensures
the corresponding wake operation finishes as POSIX semaphore does, but
that requires to protect state accesses in qemu_event_set() and
qemu_event_wait() with a mutex. On the other hand, real futex does not
need such a protection but needs complex barrier and atomic operations
to ensure ordering between the two functions.

Add special implementations of qemu_event_set() and qemu_event_wait()
using pthread primitives. qemu_event_wait() will ensure qemu_event_set()
finishes, and these functions will avoid complex barrier and atomic
operations to ensure ordering between them.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Tested-by: Phil Dennis-Jordan <phil@philjordan.eu>
Reviewed-by: Phil Dennis-Jordan <phil@philjordan.eu>
---
 util/qemu-thread-posix.c | 41 ++++++++++++++++-------------------------
 1 file changed, 16 insertions(+), 25 deletions(-)

diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 0d6344f4d216..be7f25f9b31d 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -319,28 +319,6 @@ void qemu_sem_wait(QemuSemaphore *sem)
 
 #ifdef CONFIG_LINUX
 #include "qemu/futex.h"
-#else
-static inline void qemu_futex_wake(QemuEvent *ev, int n)
-{
-    assert(ev->initialized);
-    pthread_mutex_lock(&ev->lock);
-    if (n == 1) {
-        pthread_cond_signal(&ev->cond);
-    } else {
-        pthread_cond_broadcast(&ev->cond);
-    }
-    pthread_mutex_unlock(&ev->lock);
-}
-
-static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
-{
-    assert(ev->initialized);
-    pthread_mutex_lock(&ev->lock);
-    if (ev->value == val) {
-        pthread_cond_wait(&ev->cond, &ev->lock);
-    }
-    pthread_mutex_unlock(&ev->lock);
-}
 #endif
 
 /* Valid transitions:
@@ -386,10 +364,17 @@ void qemu_event_set(QemuEvent *ev)
 {
     assert(ev->initialized);
 
+#ifdef CONFIG_LINUX
     if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
         /* There were waiters, wake them up.  */
         qemu_futex_wake_all(ev);
     }
+#else
+    pthread_mutex_lock(&ev->lock);
+    qatomic_store_release(&ev->value, EV_SET);
+    pthread_cond_broadcast(&ev->cond);
+    pthread_mutex_unlock(&ev->lock);
+#endif
 }
 
 void qemu_event_reset(QemuEvent *ev)
@@ -411,17 +396,16 @@ void qemu_event_reset(QemuEvent *ev)
 
 void qemu_event_wait(QemuEvent *ev)
 {
-    unsigned value;
-
     assert(ev->initialized);
 
+#ifdef CONFIG_LINUX
     while (true) {
         /*
          * qemu_event_wait must synchronize with qemu_event_set even if it does
          * not go down the slow path, so this load-acquire is needed that
          * synchronizes with the store-release in qemu_event_set().
          */
-        value = qatomic_load_acquire(&ev->value);
+        unsigned value = qatomic_load_acquire(&ev->value);
         if (value == EV_SET) {
             break;
         }
@@ -445,6 +429,13 @@ void qemu_event_wait(QemuEvent *ev)
 
         qemu_futex_wait(ev, EV_BUSY);
     }
+#else
+    pthread_mutex_lock(&ev->lock);
+    while (qatomic_read(&ev->value) != EV_SET) {
+        pthread_cond_wait(&ev->cond, &ev->lock);
+    }
+    pthread_mutex_unlock(&ev->lock);
+#endif
 }
 
 static __thread NotifierList thread_exit;

-- 
2.49.0



  parent reply	other threads:[~2025-05-26  5:30 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-26  5:29 [PATCH v4 00/11] Improve futex usage Akihiko Odaki
2025-05-26  5:29 ` [PATCH v4 01/11] futex: Check value after qemu_futex_wait() Akihiko Odaki
2025-05-26  5:29 ` [PATCH v4 02/11] futex: Support Windows Akihiko Odaki
2025-05-26  5:29 ` [PATCH v4 03/11] qemu-thread: Remove qatomic_read() in qemu_event_set() Akihiko Odaki
2025-05-26  5:29 ` [PATCH v4 04/11] qemu-thread: Replace __linux__ with CONFIG_LINUX Akihiko Odaki
2025-05-26  9:09   ` Philippe Mathieu-Daudé
2025-05-26  5:29 ` Akihiko Odaki [this message]
2025-05-26  5:29 ` [PATCH v4 06/11] qemu-thread: Use futex for QemuEvent on Windows Akihiko Odaki
2025-05-26  9:23   ` Philippe Mathieu-Daudé
2025-05-26  5:29 ` [PATCH v4 07/11] qemu-thread: Use futex if available for QemuLockCnt Akihiko Odaki
2025-05-26  5:29 ` [PATCH v4 08/11] migration: Replace QemuSemaphore with QemuEvent Akihiko Odaki
2025-05-26  9:24   ` Philippe Mathieu-Daudé
2025-05-26  5:29 ` [PATCH v4 09/11] migration/colo: " Akihiko Odaki
2025-05-26  9:24   ` Philippe Mathieu-Daudé
2025-05-26  5:29 ` [PATCH v4 10/11] migration/postcopy: " Akihiko Odaki
2025-05-26  5:29 ` [PATCH v4 11/11] hw/display/apple-gfx: " Akihiko Odaki
2025-05-26  9:27   ` Philippe Mathieu-Daudé
2025-05-26  9:29     ` Philippe Mathieu-Daudé
2025-05-29  4:49       ` Akihiko Odaki
2025-05-26 14:48 ` [PATCH v4 00/11] Improve futex usage Peter Xu
2025-05-27  2:09   ` Akihiko Odaki
2025-05-27 13:46     ` Peter Xu
2025-05-28  3:30       ` Akihiko Odaki
2025-05-26 16:51 ` Paolo Bonzini
2025-05-27  3:00   ` Akihiko Odaki
2025-05-27 15:01     ` Paolo Bonzini
2025-05-28  3:26       ` Akihiko Odaki

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=20250526-event-v4-5-5b784cc8e1de@daynix.com \
    --to=akihiko.odaki@daynix.com \
    --cc=berrange@redhat.com \
    --cc=devel@daynix.com \
    --cc=farosas@suse.de \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=phil@philjordan.eu \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sw@weilnetz.de \
    --cc=zhanghailiang@xfusion.com \
    /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).