Patchwork [4/5] QEMU Release vcpu and finally exit vcpu thread safely

login
register
mail settings
Submitter Sheldon
Date Nov. 29, 2011, 5:37 a.m.
Message ID <4ED46F85.4090101@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/128217/
State New
Headers show

Comments

Sheldon - Nov. 29, 2011, 5:37 a.m.
Ping Fan,

 IMO, QEMU should Release vcpu and finally exit vcpu thread safely in
tcg mode?
---
 cpus.c |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)

Patch

diff --git a/cpus.c b/cpus.c
index 82530c4..cc52327 100644
--- a/cpus.c
+++ b/cpus.c
@@ -753,7 +753,7 @@  static void tcg_exec_all(void);
 static void *qemu_tcg_cpu_thread_fn(void *arg)
 {
     CPUState *env = arg;
-
+    CPUState *prev = NULL;
     qemu_tcg_init_cpu_signals();
     qemu_thread_get_self(env->thread);
 
@@ -775,10 +775,29 @@  static void *qemu_tcg_cpu_thread_fn(void *arg)
         if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
             qemu_notify_event();
         }
+        /*1,try to zap; 2, can safe to destroy*/
+        if (env->state == CPU_STATE_ZAPPED) {
+            goto zapout;
+        }
         qemu_tcg_wait_io_event();
     }
 
     return NULL;
+zapout:
+    prev = first_cpu;
+    if (prev == env) {
+        first_cpu = env->next_cpu;
+    } else {
+        while (prev != NULL) {
+            if (prev->next_cpu == env) {
+                break;
+            }
+            prev = prev->next_cpu;
+        }
+        prev->next_cpu = env->next_cpu;
+    }
+    cpu_free(env);
+    return NULL;
 }
 
 static void qemu_cpu_kick_thread(CPUState *env)