Skip to contentSkip to conversation

Add glib support to main loop

RFC PATCH In review Anthony Liguori 4 participants 7 messages Jul 27, 2011

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Add glib support to main loop

This allows GSources to be used to register callback events in QEMU. This is useful as it allows us to take greater advantage of glib and also because it allows us to write code that is more easily testable outside of QEMU since we can make use of glib's main loop in unit tests.

All new code should use glib's callback mechanisms for registering fd events which are very well documented at:

http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html

And:

http://developer.gnome.org/gio/stable/

---
 vl.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 74 insertions(+), 0 deletions(-)
vl.c +74 -0
index 4b6688b..19774ac 100644
@@ -111,6 +111,8 @@ int main(int argc, char **argv)
 #define main qemu_main
 #endif /* CONFIG_COCOA */
 
+#include <glib.h>
+
 #include "hw/hw.h"
 #include "hw/boards.h"
 #include "hw/usb.h"
@@ -1309,6 +1311,75 @@ void qemu_system_vmstop_request(int reason)
     qemu_notify_event();
 }
 
+static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
+static int n_poll_fds;
+static int max_priority;
+
+static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
+                             fd_set *xfds, struct timeval *tv)
+{
+    GMainContext *context = g_main_context_default();
+    int i;
+    int timeout = 0, cur_timeout;
+
+    g_main_context_prepare(context, &max_priority);
+
+    n_poll_fds = g_main_context_query(context, max_priority, &timeout,
+                                      poll_fds, ARRAY_SIZE(poll_fds));
+    g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
+
+    for (i = 0; i < n_poll_fds; i++) {
+        GPollFD *p = &poll_fds[i];
+
+        if ((p->events & G_IO_IN)) {
+            FD_SET(p->fd, rfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events & G_IO_OUT)) {
+            FD_SET(p->fd, wfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events & G_IO_ERR)) {
+            FD_SET(p->fd, xfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+    }
+
+    cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
+    if (timeout >= 0 && timeout < cur_timeout) {
+        tv->tv_sec = timeout / 1000;
+        tv->tv_usec = (timeout % 1000) * 1000;
+    }
+}
+
+static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
+                             bool err)
+{
+    GMainContext *context = g_main_context_default();
+
+    if (!err) {
+        int i;
+
+        for (i = 0; i < n_poll_fds; i++) {
+            GPollFD *p = &poll_fds[i];
+
+            if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
+                p->revents |= G_IO_IN;
+            }
+            if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
+                p->revents |= G_IO_OUT;
+            }
+            if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
+                p->revents |= G_IO_ERR;
+            }
+        }
+    }
+
+    if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
+        g_main_context_dispatch(context);
+    }
+}
+
 void main_loop_wait(int nonblocking)
 {
     fd_set rfds, wfds, xfds;
@@ -1334,8 +1405,10 @@ void main_loop_wait(int nonblocking)
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&xfds);
+
     qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
     slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
+    glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
 
     qemu_mutex_unlock_iothread();
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
@@ -1343,6 +1416,7 @@ void main_loop_wait(int nonblocking)
 
     qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
     slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
+    glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
 
     qemu_run_all_timers();
 
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 42 lines · Anthony Liguori

This allows GSources to be used to register callback events in QEMU.  This is useful as it allows us to take greater advantage of glib and also because it allows us to write code that is more easily testable outside of QEMU since we can make use of glib's main loop in unit tests.

All new code should use glib's callback mechanisms for registering fd events which are very well documented at:

http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html

And:

http://developer.gnome.org/gio/stable/

---
 vl.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 1 files changed, 74 insertions(+), 0 deletions(-)
vl.c +8 -0
index 4b6688b..19774ac 100644
@@ -111,6 +111,8 @@ int main(int argc, char **argv)
  #define main qemu_main
  #endif /* CONFIG_COCOA */
 
+#include <glib.h>
+
  #include "hw/hw.h"
  #include "hw/boards.h"
  #include "hw/usb.h"
@@ -1309,6 +1311,75 @@ void qemu_system_vmstop_request(int reason)
     qemu_notify_event();
  }
 
+static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
+static int n_poll_fds;
+static int max_priority;
+
+static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
+                             fd_set *xfds, struct timeval *tv)

vl.c void qemu_system_vmstop_request(int reason) · line 1329

Quoted 10 lines
vl.c +10 -0
+{
+    GMainContext *context = g_main_context_default();
+    int i;
+    int timeout = 0, cur_timeout;
+
+    g_main_context_prepare(context, &max_priority);
+
+    n_poll_fds = g_main_context_query(context, max_priority, &timeout,
+                                      poll_fds, ARRAY_SIZE(poll_fds));
+    g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));

The use of g_assert() means that production code should define G_DISABLE_ASSERT in addition to NDEBUG. Should we make both default for stable versions?

Quoted 43 lines
vl.c +43 -0
+
+    for (i = 0; i < n_poll_fds; i++) {
+        GPollFD *p = &poll_fds[i];
+
+        if ((p->events & G_IO_IN)) {
+            FD_SET(p->fd, rfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events & G_IO_OUT)) {
+            FD_SET(p->fd, wfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events & G_IO_ERR)) {
+            FD_SET(p->fd, xfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+    }
+
+    cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
+    if (timeout >= 0 && timeout < cur_timeout) {
+        tv->tv_sec = timeout / 1000;
+        tv->tv_usec = (timeout % 1000) * 1000;
+    }
+}
+
+static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
+                             bool err)
+{
+    GMainContext *context = g_main_context_default();
+
+    if (!err) {
+        int i;
+
+        for (i = 0; i < n_poll_fds; i++) {
+            GPollFD *p = &poll_fds[i];
+
+            if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
+                p->revents |= G_IO_IN;
+            }
+            if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
+                p->revents |= G_IO_OUT;
+            }
+            if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {

vl.c void qemu_system_vmstop_request(int reason) · line 1382

Quoted 10 lines
vl.c +10 -0
+                p->revents |= G_IO_ERR;
+            }
+        }
+    }
+
+    if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
+        g_main_context_dispatch(context);
+    }
+}
+

The above functions do not seem to be referenced anywhere.

Quoted 27 lines
vl.c +3 -1
  void main_loop_wait(int nonblocking)
  {
     fd_set rfds, wfds, xfds;
@@ -1334,8 +1405,10 @@ void main_loop_wait(int nonblocking)
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&xfds);
+
     qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
     slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
+    glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
 
     qemu_mutex_unlock_iothread();
     ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
@@ -1343,6 +1416,7 @@ void main_loop_wait(int nonblocking)
 
     qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
     slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
+    glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
 
     qemu_run_all_timers();
 
--

1.7.4.1

#2 lore raw
Quoted 49 lines · Blue Swirl
Quoted 48 lines · Anthony Liguori

This allows GSources to be used to register callback events in QEMU. This is useful as it allows us to take greater advantage of glib and also because it allows us to write code that is more easily testable outside of QEMU since we can make use of glib's main loop in unit tests.

All new code should use glib's callback mechanisms for registering fd events which are very well documented at:

http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html

And:

http://developer.gnome.org/gio/stable/

---
  vl.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 74 insertions(+), 0 deletions(-)
vl.c +14 -0
index 4b6688b..19774ac 100644
@@ -111,6 +111,8 @@ int main(int argc, char **argv)
  #define main qemu_main
  #endif /* CONFIG_COCOA */
 
+#include<glib.h>
+
  #include "hw/hw.h"
  #include "hw/boards.h"
  #include "hw/usb.h"
@@ -1309,6 +1311,75 @@ void qemu_system_vmstop_request(int reason)
     qemu_notify_event();
  }
 
+static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
+static int n_poll_fds;
+static int max_priority;
+
+static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
+                             fd_set *xfds, struct timeval *tv)
+{
+    GMainContext *context = g_main_context_default();
+    int i;
+    int timeout = 0, cur_timeout;
+
+    g_main_context_prepare(context,&max_priority);
Quoted 8 lines

vl.c void qemu_system_vmstop_request(int reason)

Quoted 4 lines
vl.c +4 -0
+
+    n_poll_fds = g_main_context_query(context, max_priority,&timeout,
+                                      poll_fds, ARRAY_SIZE(poll_fds));
+    g_assert(n_poll_fds<= ARRAY_SIZE(poll_fds));

The use of g_assert() means that production code should define G_DISABLE_ASSERT in addition to NDEBUG. Should we make both default for stable versions?

Oh, this is just me being lazy. There's a way to do this without using the assert. I put the assert there to make sure that I didn't leave a latent bug.

But in general, I don't think we should ever define NDEBUG.

Quoted 47 lines
Quoted 47 lines
vl.c +47 -0
+
+    for (i = 0; i<  n_poll_fds; i++) {
+        GPollFD *p =&poll_fds[i];
+
+        if ((p->events&  G_IO_IN)) {
+            FD_SET(p->fd, rfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events&  G_IO_OUT)) {
+            FD_SET(p->fd, wfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events&  G_IO_ERR)) {
+            FD_SET(p->fd, xfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+    }
+
+    cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
+    if (timeout>= 0&&  timeout<  cur_timeout) {
+        tv->tv_sec = timeout / 1000;
+        tv->tv_usec = (timeout % 1000) * 1000;
+    }
+}
+
+static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
+                             bool err)
+{
+    GMainContext *context = g_main_context_default();
+
+    if (!err) {
+        int i;
+
+        for (i = 0; i<  n_poll_fds; i++) {
+            GPollFD *p =&poll_fds[i];
+
+            if ((p->events&  G_IO_IN)&&  FD_ISSET(p->fd, rfds)) {
+                p->revents |= G_IO_IN;
+            }
+            if ((p->events&  G_IO_OUT)&&  FD_ISSET(p->fd, wfds)) {
+                p->revents |= G_IO_OUT;
+            }
+            if ((p->events&  G_IO_ERR)&&  FD_ISSET(p->fd, xfds)) {
+                p->revents |= G_IO_ERR;
+            }
+        }
+    }
Quoted 8 lines

vl.c void qemu_system_vmstop_request(int reason) · line 1382

Quoted 6 lines
vl.c +6 -0
+
+    if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
+        g_main_context_dispatch(context);
+    }
+}
+

The above functions do not seem to be referenced anywhere.

They are in the code below :-)

Quoted 16 lines
Quoted 16 lines
vl.c +2 -0
  void main_loop_wait(int nonblocking)
  {
     fd_set rfds, wfds, xfds;
@@ -1334,8 +1405,10 @@ void main_loop_wait(int nonblocking)
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&xfds);
+
     qemu_iohandler_fill(&nfds,&rfds,&wfds,&xfds);
     slirp_select_fill(&nfds,&rfds,&wfds,&xfds);
+    glib_select_fill(&nfds,&rfds,&wfds,&xfds,&tv);
 
     qemu_mutex_unlock_iothread();
     ret = select(nfds + 1,&rfds,&wfds,&xfds,&tv);
@@ -1343,6 +1416,7 @@ void main_loop_wait(int nonblocking)
 
Quoted 5 lines

vl.c void main_loop_wait(int nonblocking) · line 1421

Quoted 5 lines
vl.c +1 -0
     qemu_iohandler_poll(&rfds,&wfds,&xfds, ret);
     slirp_select_poll(&rfds,&wfds,&xfds, (ret<  0));
+    glib_select_poll(&rfds,&wfds,&xfds, (ret<  0));
 
     qemu_run_all_timers();

Regards,

Anthony Liguori

Quoted 5 lines
Quoted 5 lines
vl.c +0 -1
--

1.7.4.1

#3 lore raw
Quoted 67 lines · Anthony Liguori
Quoted 57 lines · Blue Swirl
Quoted 54 lines · Anthony Liguori

This allows GSources to be used to register callback events in QEMU. This is
useful as it allows us to take greater advantage of glib and also because it
allows us to write code that is more easily testable outside of QEMU since we
can make use of glib's main loop in unit tests.

All new code should use glib's callback mechanisms for registering fd events
which are very well documented at:

http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html

And:

http://developer.gnome.org/gio/stable/

---
 vl.c |   74
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 1 files changed, 74 insertions(+), 0 deletions(-)
vl.c +14 -0
index 4b6688b..19774ac 100644
@@ -111,6 +111,8 @@ int main(int argc, char **argv)
  #define main qemu_main
  #endif /* CONFIG_COCOA */
 
+#include<glib.h>
+
  #include "hw/hw.h"
  #include "hw/boards.h"
  #include "hw/usb.h"
@@ -1309,6 +1311,75 @@ void qemu_system_vmstop_request(int reason)
     qemu_notify_event();
  }
 
+static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
+static int n_poll_fds;
+static int max_priority;
+
+static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
+                             fd_set *xfds, struct timeval *tv)
+{
+    GMainContext *context = g_main_context_default();
+    int i;
+    int timeout = 0, cur_timeout;
+
+    g_main_context_prepare(context,&max_priority);
Quoted 8 lines

vl.c void qemu_system_vmstop_request(int reason)

Quoted 4 lines
vl.c +4 -0
+
+    n_poll_fds = g_main_context_query(context, max_priority,&timeout,
+                                      poll_fds, ARRAY_SIZE(poll_fds));
+    g_assert(n_poll_fds<= ARRAY_SIZE(poll_fds));

The use of g_assert() means that production code should define G_DISABLE_ASSERT in addition to NDEBUG. Should we make both default for stable versions?

Quoted 5 lines

Oh, this is just me being lazy.  There's a way to do this without using the assert.  I put the assert there to make sure that I didn't leave a latent bug.

But in general, I don't think we should ever define NDEBUG.

Why? I think it's normal practice to define NDEBUG (and G_DISABLE_ASSERT if g_assert() is used) for production builds.

Quoted 57 lines
Quoted 48 lines
Quoted 48 lines
vl.c +48 -0
+
+    for (i = 0; i<  n_poll_fds; i++) {
+        GPollFD *p =&poll_fds[i];
+
+        if ((p->events&  G_IO_IN)) {
+            FD_SET(p->fd, rfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events&  G_IO_OUT)) {
+            FD_SET(p->fd, wfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+        if ((p->events&  G_IO_ERR)) {
+            FD_SET(p->fd, xfds);
+            *max_fd = MAX(*max_fd, p->fd);
+        }
+    }
+
+    cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
+    if (timeout>= 0&&  timeout<  cur_timeout) {
+        tv->tv_sec = timeout / 1000;
+        tv->tv_usec = (timeout % 1000) * 1000;
+    }
+}
+
+static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
+                             bool err)
+{
+    GMainContext *context = g_main_context_default();
+
+    if (!err) {
+        int i;
+
+        for (i = 0; i<  n_poll_fds; i++) {
+            GPollFD *p =&poll_fds[i];
+
+            if ((p->events&  G_IO_IN)&&  FD_ISSET(p->fd, rfds)) {
+                p->revents |= G_IO_IN;
+            }
+            if ((p->events&  G_IO_OUT)&&  FD_ISSET(p->fd, wfds)) {
+                p->revents |= G_IO_OUT;
+            }
+            if ((p->events&  G_IO_ERR)&&  FD_ISSET(p->fd, xfds)) {
+                p->revents |= G_IO_ERR;
+            }
+        }
+    }
+
Quoted 8 lines

vl.c void qemu_system_vmstop_request(int reason) · line 1382

Quoted 6 lines
vl.c +5 -0
+    if (g_main_context_check(context, max_priority, poll_fds,
 n_poll_fds)) {
+        g_main_context_dispatch(context);
+    }
+}
+

The above functions do not seem to be referenced anywhere.

Quoted 1 line

They are in the code below :-)

Sorry, internal compiler error.

Quoted 33 lines
Quoted 16 lines
Quoted 16 lines
vl.c +0 -0
  void main_loop_wait(int nonblocking)
  {

    fd_set rfds, wfds, xfds;

vl.c +2 -0
@@ -1334,8 +1405,10 @@ void main_loop_wait(int nonblocking)
     FD_ZERO(&rfds);
     FD_ZERO(&wfds);
     FD_ZERO(&xfds);
+
     qemu_iohandler_fill(&nfds,&rfds,&wfds,&xfds);
     slirp_select_fill(&nfds,&rfds,&wfds,&xfds);
+    glib_select_fill(&nfds,&rfds,&wfds,&xfds,&tv);
 
     qemu_mutex_unlock_iothread();
     ret = select(nfds + 1,&rfds,&wfds,&xfds,&tv);
@@ -1343,6 +1416,7 @@ void main_loop_wait(int nonblocking)
 
Quoted 5 lines

vl.c void main_loop_wait(int nonblocking) · line 1421

Quoted 5 lines
vl.c +1 -0
     qemu_iohandler_poll(&rfds,&wfds,&xfds, ret);
     slirp_select_poll(&rfds,&wfds,&xfds, (ret<  0));
+    glib_select_poll(&rfds,&wfds,&xfds, (ret<  0));
 
     qemu_run_all_timers();

Regards,

Anthony Liguori

Quoted 5 lines
Quoted 5 lines
vl.c +0 -1
--

1.7.4.1

#4 lore raw

Am 27.07.2011 23:12, schrieb Blue Swirl:

Quoted 69 lines · Anthony Liguori
Quoted 68 lines · Blue Swirl
Quoted 66 lines
Quoted 48 lines · Anthony Liguori

This allows GSources to be used to register callback events in QEMU.
This is
useful as it allows us to take greater advantage of glib and also because it
allows us to write code that is more easily testable outside of QEMU since we
can make use of glib's main loop in unit tests.

All new code should use glib's callback mechanisms for registering fd events
which are very well documented at:

http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html

And:

http://developer.gnome.org/gio/stable/

---
  vl.c |   74
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 74 insertions(+), 0 deletions(-)
vl.c +9 -0
index 4b6688b..19774ac 100644
@@ -111,6 +111,8 @@ int main(int argc, char **argv)
  #define main qemu_main
  #endif /* CONFIG_COCOA */
 
+#include<glib.h>
+
  #include "hw/hw.h"
  #include "hw/boards.h"
  #include "hw/usb.h"
@@ -1309,6 +1311,75 @@ void qemu_system_vmstop_request(int reason)
     qemu_notify_event();
  }
 
+static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
+static int n_poll_fds;
+static int max_priority;
+
+static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
+                             fd_set *xfds, struct timeval *tv)
+{

vl.c void qemu_system_vmstop_request(int reason) · line 1330

Quoted 10 lines
vl.c +9 -0
+    GMainContext *context = g_main_context_default();
+    int i;
+    int timeout = 0, cur_timeout;
+
+    g_main_context_prepare(context,&max_priority);
+
+    n_poll_fds = g_main_context_query(context, max_priority,&timeout,
+                                      poll_fds, ARRAY_SIZE(poll_fds));
+    g_assert(n_poll_fds<= ARRAY_SIZE(poll_fds));
 

The use of g_assert() means that production code should define G_DISABLE_ASSERT in addition to NDEBUG. Should we make both default for stable versions?

Quoted 9 lines
Quoted 6 lines

Oh, this is just me being lazy. There's a way to do this without using the assert. I put the assert there to make sure that I didn't leave a latent bug.

But in general, I don't think we should ever define NDEBUG.

Why? I think it's normal practice to define NDEBUG (and G_DISABLE_ASSERT if g_assert() is used) for production builds.

NDEBUG is normally set automatically for production code, and it disables assertions which is not always good.

Assertions are useful even in production code unless you are sure that the code is bug free or the assertions cost to much resources - either memory or execution time.

I don't think this is the case for QEMU, therefore I'd prefer having assertions in production code, too. This simply means undefining NDEBUG before including assert.h.

Regards,
Stefan

#5 lore raw
Quoted 10 lines · Stefan Weil

NDEBUG is normally set automatically for production code, and it disables assertions which is not always good.

Assertions are useful even in production code unless you are sure that the code is bug free or the assertions cost to much resources - either memory or execution time.

I don't think this is the case for QEMU, therefore I'd prefer having assertions in production code, too. This simply means undefining NDEBUG before including assert.h.

I don't know what constitutes "production builds" but the software that most people use in production (normal distributions packages) don't define NDEBUG.

Regards,

Anthony Liguori

Quoted 4 lines

Regards,
Stefan

#6 lore raw
In reply to Blue Swirl
Quoted 68 lines · Blue Swirl
Quoted 67 lines · Anthony Liguori
Quoted 65 lines · Blue Swirl
Quoted 48 lines · Anthony Liguori

This allows GSources to be used to register callback events in QEMU.
This is
useful as it allows us to take greater advantage of glib and also because it
allows us to write code that is more easily testable outside of QEMU since we
can make use of glib's main loop in unit tests.

All new code should use glib's callback mechanisms for registering fd events
which are very well documented at:

http://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html

And:

http://developer.gnome.org/gio/stable/

---
  vl.c |   74
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 74 insertions(+), 0 deletions(-)
vl.c +8 -0
index 4b6688b..19774ac 100644
@@ -111,6 +111,8 @@ int main(int argc, char **argv)
  #define main qemu_main
  #endif /* CONFIG_COCOA */
 
+#include<glib.h>
+
  #include "hw/hw.h"
  #include "hw/boards.h"
  #include "hw/usb.h"
@@ -1309,6 +1311,75 @@ void qemu_system_vmstop_request(int reason)
     qemu_notify_event();
  }
 
+static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
+static int n_poll_fds;
+static int max_priority;
+
+static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
+                             fd_set *xfds, struct timeval *tv)

vl.c void qemu_system_vmstop_request(int reason) · line 1329

Quoted 10 lines
vl.c +10 -0
+{
+    GMainContext *context = g_main_context_default();
+    int i;
+    int timeout = 0, cur_timeout;
+
+    g_main_context_prepare(context,&max_priority);
+
+    n_poll_fds = g_main_context_query(context, max_priority,&timeout,
+                                      poll_fds, ARRAY_SIZE(poll_fds));
+    g_assert(n_poll_fds<= ARRAY_SIZE(poll_fds));

The use of g_assert() means that production code should define G_DISABLE_ASSERT in addition to NDEBUG. Should we make both default for stable versions?

Quoted 8 lines
Quoted 5 lines

Oh, this is just me being lazy. There's a way to do this without using the assert. I put the assert there to make sure that I didn't leave a latent bug.

But in general, I don't think we should ever define NDEBUG.

Why? I think it's normal practice to define NDEBUG (and G_DISABLE_ASSERT if g_assert() is used) for production builds.

Heh, I guess this is a stylistic thing.

The theory is that if you do all of your testing with !defined(NDEBUG), then you wouldn't want to ship something that potentially introduces different code paths to the end user.

Besides, if an assert would trigger, I'd rather it drop core and have a predictable error verses an unpredictable behavior.

Regards,

Anthony Liguori