From: Anthony Liguori <aliguori@us.ibm.com>
To: Anthony Liguori <aliguori@us.ibm.com>
Cc: Blue Swirl <blauwirbel@gmail.com>,
Paolo Bonzini <pbonzini@redhat.com>,
qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/2] Add glib support to main loop
Date: Thu, 01 Sep 2011 13:54:29 -0500 [thread overview]
Message-ID: <4E5FD4E5.3010104@us.ibm.com> (raw)
In-Reply-To: <1314018774-27482-1-git-send-email-aliguori@us.ibm.com>
On 08/22/2011 08:12 AM, Anthony Liguori wrote:
> 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/
>
> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
Applied both of these.
Regards,
Anthony Liguori
> ---
> vl.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 74 insertions(+), 0 deletions(-)
>
> diff --git a/vl.c b/vl.c
> index 06a6f80..d661e8e 100644
> --- a/vl.c
> +++ b/vl.c
> @@ -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"
> @@ -1321,6 +1323,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;
> @@ -1346,8 +1417,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);
> @@ -1355,6 +1428,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();
>
prev parent reply other threads:[~2011-09-01 18:54 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-22 13:12 [Qemu-devel] [PATCH 1/2] Add glib support to main loop Anthony Liguori
2011-08-22 13:12 ` [Qemu-devel] [PATCH 2/2] main: switch qemu_set_fd_handler to g_io_add_watch Anthony Liguori
2011-08-22 13:40 ` Paolo Bonzini
2011-08-22 13:45 ` Anthony Liguori
2011-08-22 13:47 ` Paolo Bonzini
2011-09-06 14:31 ` Paolo Bonzini
2011-09-06 15:59 ` Anthony Liguori
2011-09-07 7:03 ` Paolo Bonzini
2011-09-07 8:08 ` Jan Kiszka
2011-09-07 12:42 ` Anthony Liguori
2011-09-07 14:40 ` Paolo Bonzini
2011-09-07 14:53 ` Anthony Liguori
2011-09-07 15:26 ` Paolo Bonzini
2011-11-24 17:11 ` Fabien Chouteau
2011-11-24 17:30 ` Paolo Bonzini
2011-11-25 10:24 ` Fabien Chouteau
2011-11-25 10:46 ` Paolo Bonzini
2011-11-25 14:46 ` Fabien Chouteau
2011-11-25 14:49 ` Paolo Bonzini
2011-11-25 15:33 ` Fabien Chouteau
2011-11-25 15:48 ` Paolo Bonzini
2011-11-25 16:56 ` Fabien Chouteau
2011-11-25 19:36 ` Paolo Bonzini
2011-11-28 9:13 ` Fabien Chouteau
2011-09-04 14:03 ` Avi Kivity
2011-09-04 14:51 ` Anthony Liguori
2011-09-04 15:01 ` Anthony Liguori
2011-09-07 12:54 ` Avi Kivity
2011-09-05 9:46 ` Avi Kivity
2011-09-01 18:54 ` Anthony Liguori [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4E5FD4E5.3010104@us.ibm.com \
--to=aliguori@us.ibm.com \
--cc=blauwirbel@gmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).