Blame SOURCES/kvm-iothread-replace-init_done_cond-with-a-semaphore.patch

Pablo Greco e6a3ae
From 9db6f24509ee8a28818693d6a15257b873d9422a Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: Stefan Hajnoczi <stefanha@redhat.com>
Pablo Greco e6a3ae
Date: Thu, 15 Aug 2019 13:23:11 +0100
Pablo Greco e6a3ae
Subject: [PATCH 09/10] iothread: replace init_done_cond with a semaphore
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
Pablo Greco e6a3ae
Message-id: <20190815132311.22027-3-stefanha@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 89996
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH v2 2/2] iothread: replace init_done_cond with a semaphore
Pablo Greco e6a3ae
Bugzilla: 1687541
Pablo Greco e6a3ae
RH-Acked-by: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: John Snow <jsnow@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
From: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Only sending an init-done message using lock+cond seems an overkill to
Pablo Greco e6a3ae
me.  Replacing it with a simpler semaphore.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Meanwhile, init the semaphore unconditionally, then we can destroy it
Pablo Greco e6a3ae
unconditionally too in finalize which seems cleaner.
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Signed-off-by: Peter Xu <peterx@redhat.com>
Pablo Greco e6a3ae
Message-id: 20190306115532.23025-2-peterx@redhat.com
Pablo Greco e6a3ae
Message-Id: <20190306115532.23025-2-peterx@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit 21c4d15b4708b7d30c450041a560df670f36cac8)
Pablo Greco e6a3ae
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 include/sysemu/iothread.h |  3 +--
Pablo Greco e6a3ae
 iothread.c                | 17 ++++-------------
Pablo Greco e6a3ae
 2 files changed, 5 insertions(+), 15 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/include/sysemu/iothread.h b/include/sysemu/iothread.h
Pablo Greco e6a3ae
index 8a7ac2c..50411ba 100644
Pablo Greco e6a3ae
--- a/include/sysemu/iothread.h
Pablo Greco e6a3ae
+++ b/include/sysemu/iothread.h
Pablo Greco e6a3ae
@@ -27,8 +27,7 @@ typedef struct {
Pablo Greco e6a3ae
     GMainContext *worker_context;
Pablo Greco e6a3ae
     GMainLoop *main_loop;
Pablo Greco e6a3ae
     GOnce once;
Pablo Greco e6a3ae
-    QemuMutex init_done_lock;
Pablo Greco e6a3ae
-    QemuCond init_done_cond;    /* is thread initialization done? */
Pablo Greco e6a3ae
+    QemuSemaphore init_done_sem; /* is thread init done? */
Pablo Greco e6a3ae
     bool stopping;              /* has iothread_stop() been called? */
Pablo Greco e6a3ae
     bool running;               /* should iothread_run() continue? */
Pablo Greco e6a3ae
     int thread_id;
Pablo Greco e6a3ae
diff --git a/iothread.c b/iothread.c
Pablo Greco e6a3ae
index 2fb1cdf..b92232f 100644
Pablo Greco e6a3ae
--- a/iothread.c
Pablo Greco e6a3ae
+++ b/iothread.c
Pablo Greco e6a3ae
@@ -55,10 +55,8 @@ static void *iothread_run(void *opaque)
Pablo Greco e6a3ae
     rcu_register_thread();
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     my_iothread = iothread;
Pablo Greco e6a3ae
-    qemu_mutex_lock(&iothread->init_done_lock);
Pablo Greco e6a3ae
     iothread->thread_id = qemu_get_thread_id();
Pablo Greco e6a3ae
-    qemu_cond_signal(&iothread->init_done_cond);
Pablo Greco e6a3ae
-    qemu_mutex_unlock(&iothread->init_done_lock);
Pablo Greco e6a3ae
+    qemu_sem_post(&iothread->init_done_sem);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     while (iothread->running) {
Pablo Greco e6a3ae
         aio_poll(iothread->ctx, true);
Pablo Greco e6a3ae
@@ -111,6 +109,7 @@ static void iothread_instance_init(Object *obj)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     iothread->poll_max_ns = IOTHREAD_POLL_MAX_NS_DEFAULT;
Pablo Greco e6a3ae
     iothread->thread_id = -1;
Pablo Greco e6a3ae
+    qemu_sem_init(&iothread->init_done_sem, 0);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 static void iothread_instance_finalize(Object *obj)
Pablo Greco e6a3ae
@@ -119,10 +118,6 @@ static void iothread_instance_finalize(Object *obj)
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     iothread_stop(iothread);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    if (iothread->thread_id != -1) {
Pablo Greco e6a3ae
-        qemu_cond_destroy(&iothread->init_done_cond);
Pablo Greco e6a3ae
-        qemu_mutex_destroy(&iothread->init_done_lock);
Pablo Greco e6a3ae
-    }
Pablo Greco e6a3ae
     /*
Pablo Greco e6a3ae
      * Before glib2 2.33.10, there is a glib2 bug that GSource context
Pablo Greco e6a3ae
      * pointer may not be cleared even if the context has already been
Pablo Greco e6a3ae
@@ -141,6 +136,7 @@ static void iothread_instance_finalize(Object *obj)
Pablo Greco e6a3ae
         g_main_context_unref(iothread->worker_context);
Pablo Greco e6a3ae
         iothread->worker_context = NULL;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
+    qemu_sem_destroy(&iothread->init_done_sem);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 static void iothread_complete(UserCreatable *obj, Error **errp)
Pablo Greco e6a3ae
@@ -169,8 +165,6 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
Pablo Greco e6a3ae
         return;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    qemu_mutex_init(&iothread->init_done_lock);
Pablo Greco e6a3ae
-    qemu_cond_init(&iothread->init_done_cond);
Pablo Greco e6a3ae
     iothread->once = (GOnce) G_ONCE_INIT;
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* This assumes we are called from a thread with useful CPU affinity for us
Pablo Greco e6a3ae
@@ -184,12 +178,9 @@ static void iothread_complete(UserCreatable *obj, Error **errp)
Pablo Greco e6a3ae
     g_free(name);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* Wait for initialization to complete */
Pablo Greco e6a3ae
-    qemu_mutex_lock(&iothread->init_done_lock);
Pablo Greco e6a3ae
     while (iothread->thread_id == -1) {
Pablo Greco e6a3ae
-        qemu_cond_wait(&iothread->init_done_cond,
Pablo Greco e6a3ae
-                       &iothread->init_done_lock);
Pablo Greco e6a3ae
+        qemu_sem_wait(&iothread->init_done_sem);
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
-    qemu_mutex_unlock(&iothread->init_done_lock);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 typedef struct {
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae