Patchwork [1/9] main-loop: introduce a main_loop_quit() function

login
register
mail settings
Submitter Anthony Liguori
Date Feb. 20, 2013, 3:32 p.m.
Message ID <1361374369-19024-2-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/222103/
State New
Headers show

Comments

Anthony Liguori - Feb. 20, 2013, 3:32 p.m.
Today the only way the main loop exits is upon a system shutdown
request.  This is baked into the logic of the main loop.

Introduce a main_loop_quit function to explicitly request that
the main loop exits and refactor shutdown to use it.  This is
closer to how most main loops work.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 include/qemu/main-loop.h |  3 +++
 main-loop.c              | 13 +++++++++++++
 vl.c                     |  9 +++++----
 3 files changed, 21 insertions(+), 4 deletions(-)
Paolo Bonzini - Feb. 20, 2013, 3:36 p.m.
Il 20/02/2013 16:32, Anthony Liguori ha scritto:
> diff --git a/main-loop.c b/main-loop.c
> index 6f52ac3..d433c45 100644
> --- a/main-loop.c
> +++ b/main-loop.c
> @@ -392,6 +392,19 @@ static int os_host_main_loop_wait(uint32_t timeout)
>  }
>  #endif
>  
> +static bool do_main_loop_quit;
> +
> +bool main_loop_should_quit(void)
> +{
> +    return do_main_loop_quit;
> +}
> +
> +void main_loop_quit(void)
> +{
> +    do_main_loop_quit = true;
> +    qemu_notify_event();
> +}
> +
>  int main_loop_wait(int nonblocking)
>  {
>      int ret;

Why not keep this as main_loop_should_exit() in vl.c?

qemu-nbd.c for example has its own logic to exit the loop.

Paolo
Anthony Liguori - Feb. 20, 2013, 4:13 p.m.
Paolo Bonzini <pbonzini@redhat.com> writes:

> Il 20/02/2013 16:32, Anthony Liguori ha scritto:
>> diff --git a/main-loop.c b/main-loop.c
>> index 6f52ac3..d433c45 100644
>> --- a/main-loop.c
>> +++ b/main-loop.c
>> @@ -392,6 +392,19 @@ static int os_host_main_loop_wait(uint32_t timeout)
>>  }
>>  #endif
>>  
>> +static bool do_main_loop_quit;
>> +
>> +bool main_loop_should_quit(void)
>> +{
>> +    return do_main_loop_quit;
>> +}
>> +
>> +void main_loop_quit(void)
>> +{
>> +    do_main_loop_quit = true;
>> +    qemu_notify_event();
>> +}
>> +
>>  int main_loop_wait(int nonblocking)
>>  {
>>      int ret;
>
> Why not keep this as main_loop_should_exit() in vl.c?
>
> qemu-nbd.c for example has its own logic to exit the loop.

We should standardize on main_loop_quit() as the mechanism to exit the
main loop.

This is in line with glib and pretty much every other main loop
implementation out there.

Regards,

Anthony Liguori

>
> Paolo

Patch

diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h
index e8059c3..758b435 100644
--- a/include/qemu/main-loop.h
+++ b/include/qemu/main-loop.h
@@ -303,4 +303,7 @@  void qemu_iohandler_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds, int rc
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
 void qemu_bh_schedule_idle(QEMUBH *bh);
 
+bool main_loop_should_quit(void);
+void main_loop_quit(void);
+
 #endif
diff --git a/main-loop.c b/main-loop.c
index 6f52ac3..d433c45 100644
--- a/main-loop.c
+++ b/main-loop.c
@@ -392,6 +392,19 @@  static int os_host_main_loop_wait(uint32_t timeout)
 }
 #endif
 
+static bool do_main_loop_quit;
+
+bool main_loop_should_quit(void)
+{
+    return do_main_loop_quit;
+}
+
+void main_loop_quit(void)
+{
+    do_main_loop_quit = true;
+    qemu_notify_event();
+}
+
 int main_loop_wait(int nonblocking)
 {
     int ret;
diff --git a/vl.c b/vl.c
index c5b0eea..d6c5626 100644
--- a/vl.c
+++ b/vl.c
@@ -1942,7 +1942,7 @@  void qemu_system_vmstop_request(RunState state)
     qemu_notify_event();
 }
 
-static bool main_loop_should_exit(void)
+static void main_loop_junk(void)
 {
     RunState r;
     if (qemu_debug_requested()) {
@@ -1957,7 +1957,8 @@  static bool main_loop_should_exit(void)
         if (no_shutdown) {
             vm_stop(RUN_STATE_SHUTDOWN);
         } else {
-            return true;
+            main_loop_quit();
+            return;
         }
     }
     if (qemu_reset_requested()) {
@@ -1983,7 +1984,6 @@  static bool main_loop_should_exit(void)
     if (qemu_vmstop_requested(&r)) {
         vm_stop(r);
     }
-    return false;
 }
 
 static void main_loop(void)
@@ -2002,7 +2002,8 @@  static void main_loop(void)
 #ifdef CONFIG_PROFILER
         dev_time += profile_getclock() - ti;
 #endif
-    } while (!main_loop_should_exit());
+        main_loop_junk();
+    } while (!main_loop_should_quit());
 }
 
 static void version(void)