[PATCH 4/7] migration: Simplify migration-threads API

Peter Xu posted 7 patches 1 month, 3 weeks ago
[PATCH 4/7] migration: Simplify migration-threads API
Posted by Peter Xu 1 month, 3 weeks ago
There's no need to return a thread struct, because both the add/remove APIs
must be used in the working threads so tid would work.

Similar to the fact we don't need to pass in tid in each call sites, we
don't need the thread struct for removal too because tid is always in the
context.  Remove it in both add & remove APIs.  Instead making sure when
remove a thread the tid is always there.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 migration/threadinfo.h |  4 ++--
 migration/migration.c  |  5 ++---
 migration/multifd.c    |  5 ++---
 migration/threadinfo.c | 19 ++++++++++++++-----
 4 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/migration/threadinfo.h b/migration/threadinfo.h
index d0e4ab0aa3..7c86ae8763 100644
--- a/migration/threadinfo.h
+++ b/migration/threadinfo.h
@@ -21,5 +21,5 @@ struct MigrationThread {
     QLIST_ENTRY(MigrationThread) node;
 };
 
-MigrationThread *migration_threads_add(const char *name);
-void migration_threads_remove(MigrationThread *info);
+void migration_threads_add(const char *name);
+void migration_threads_remove(void);
diff --git a/migration/migration.c b/migration/migration.c
index 1ddcf54a70..74b2c1c627 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -3460,14 +3460,13 @@ static void qemu_savevm_wait_unplug(MigrationState *s, int old_state,
 static void *migration_thread(void *opaque)
 {
     MigrationState *s = opaque;
-    MigrationThread *thread = NULL;
     int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
     MigThrError thr_error;
     bool urgent = false;
     Error *local_err = NULL;
     int ret;
 
-    thread = migration_threads_add(MIGRATION_THREAD_SRC_MAIN);
+    migration_threads_add(MIGRATION_THREAD_SRC_MAIN);
 
     rcu_register_thread();
 
@@ -3566,7 +3565,7 @@ out:
     migration_iteration_finish(s);
     object_unref(OBJECT(s));
     rcu_unregister_thread();
-    migration_threads_remove(thread);
+    migration_threads_remove();
     return NULL;
 }
 
diff --git a/migration/multifd.c b/migration/multifd.c
index 04db886c7e..2738d78407 100644
--- a/migration/multifd.c
+++ b/migration/multifd.c
@@ -570,12 +570,11 @@ int multifd_send_sync_main(void)
 static void *multifd_send_thread(void *opaque)
 {
     MultiFDSendParams *p = opaque;
-    MigrationThread *thread = NULL;
     Error *local_err = NULL;
     int ret = 0;
     bool use_packets = multifd_use_packets();
 
-    thread = migration_threads_add(p->name);
+    migration_threads_add(p->name);
 
     trace_multifd_send_thread_start(p->id);
     rcu_register_thread();
@@ -669,7 +668,7 @@ out:
     }
 
     rcu_unregister_thread();
-    migration_threads_remove(thread);
+    migration_threads_remove();
     trace_multifd_send_thread_end(p->id, p->packets_sent);
 
     return NULL;
diff --git a/migration/threadinfo.c b/migration/threadinfo.c
index 8069413091..25e77404e2 100644
--- a/migration/threadinfo.c
+++ b/migration/threadinfo.c
@@ -23,7 +23,7 @@ static void __attribute__((constructor)) migration_threads_init(void)
     qemu_mutex_init(&migration_threads_lock);
 }
 
-MigrationThread *migration_threads_add(const char *name)
+void migration_threads_add(const char *name)
 {
     MigrationThread *thread =  g_new0(MigrationThread, 1);
 
@@ -33,17 +33,26 @@ MigrationThread *migration_threads_add(const char *name)
     WITH_QEMU_LOCK_GUARD(&migration_threads_lock) {
         QLIST_INSERT_HEAD(&migration_threads, thread, node);
     }
-
-    return thread;
 }
 
-void migration_threads_remove(MigrationThread *thread)
+void migration_threads_remove(void)
 {
+    int tid = qemu_get_thread_id();
+    MigrationThread *thread;
+
     QEMU_LOCK_GUARD(&migration_threads_lock);
-    if (thread) {
+
+    QLIST_FOREACH(thread, &migration_threads, node) {
+        if (tid != thread->thread_id) {
+            continue;
+        }
+
         QLIST_REMOVE(thread, node);
         g_free(thread);
+        return;
     }
+
+    g_assert_not_reached();
 }
 
 MigrationThreadInfoList *qmp_query_migrationthreads(Error **errp)
-- 
2.45.0