qemu: refactor tcg cpu execution loop (Marcelo Tosatti)
[qemu] / vl.c
diff --git a/vl.c b/vl.c
index 92f3014..78320b2 100644 (file)
--- a/vl.c
+++ b/vl.c
@@ -3568,6 +3568,7 @@ static QEMUResetEntry *first_reset_entry;
 static int reset_requested;
 static int shutdown_requested;
 static int powerdown_requested;
+static int debug_requested;
 
 int qemu_shutdown_requested(void)
 {
@@ -3590,6 +3591,13 @@ int qemu_powerdown_requested(void)
     return r;
 }
 
+static int qemu_debug_requested(void)
+{
+    int r = debug_requested;
+    debug_requested = 0;
+    return r;
+}
+
 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 {
     QEMUResetEntry **pre, *re;
@@ -3750,6 +3758,9 @@ void qemu_cpu_kick(void *env)
     return;
 }
 
+#define qemu_mutex_lock_iothread() do { } while (0)
+#define qemu_mutex_unlock_iothread() do { } while (0)
+
 #ifdef _WIN32
 static void host_main_loop_wait(int *timeout)
 {
@@ -3842,7 +3853,9 @@ void main_loop_wait(int timeout)
         slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
     }
 #endif
+    qemu_mutex_unlock_iothread();
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
+    qemu_mutex_lock_iothread();
     if (ret > 0) {
         IOHandlerRecord **pioh;
 
@@ -3938,6 +3951,30 @@ static int qemu_cpu_exec(CPUState *env)
     return ret;
 }
 
+static void tcg_cpu_exec(void)
+{
+    int ret;
+
+    if (next_cpu == NULL)
+        next_cpu = first_cpu;
+    for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
+        CPUState *env = cur_cpu = next_cpu;
+
+        if (!vm_running)
+            break;
+        if (timer_alarm_pending) {
+            timer_alarm_pending = 0;
+            break;
+        }
+        ret = qemu_cpu_exec(env);
+        if (ret == EXCP_DEBUG) {
+            gdb_set_stop_cpu(env);
+            debug_requested = 1;
+            break;
+        }
+    }
+}
+
 static int cpu_has_work(CPUState *env)
 {
     if (!env->halted)
@@ -4014,35 +4051,20 @@ static int vm_can_run(void)
         return 0;
     if (shutdown_requested)
         return 0;
+    if (debug_requested)
+        return 0;
     return 1;
 }
 
 static void main_loop(void)
 {
-    int ret = 0;
-#ifdef CONFIG_PROFILER
-    int64_t ti;
-#endif
-
     for (;;) {
-        do {
-            if (next_cpu == NULL)
-                next_cpu = first_cpu;
-            for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
-                CPUState *env = cur_cpu = next_cpu;
 
-                if (!vm_running)
-                    break;
-                if (timer_alarm_pending) {
-                    timer_alarm_pending = 0;
-                    break;
-                }
-                ret = qemu_cpu_exec(env);
-                if (ret == EXCP_DEBUG) {
-                    gdb_set_stop_cpu(env);
-                    break;
-                }
-            }
+        do {
+#ifdef CONFIG_PROFILER
+            int64_t ti;
+#endif
+            tcg_cpu_exec();
 #ifdef CONFIG_PROFILER
             ti = profile_getclock();
 #endif
@@ -4050,11 +4072,10 @@ static void main_loop(void)
 #ifdef CONFIG_PROFILER
             dev_time += profile_getclock() - ti;
 #endif
-        } while (ret != EXCP_DEBUG && vm_can_run());
+        } while (vm_can_run());
 
-        if (ret == EXCP_DEBUG)
+        if (qemu_debug_requested())
             vm_stop(EXCP_DEBUG);
-
         if (qemu_shutdown_requested()) {
             if (no_shutdown) {
                 vm_stop(0);