X-Git-Url: https://code.delx.au/gnu-emacs/blobdiff_plain/f5a312939f6b5105eb0a1397a831d5e67481965f..7e09ef09a479731d01b1ca46e94ddadd73ac98e3:/src/xgselect.c diff --git a/src/xgselect.c b/src/xgselect.c index 7decca34ed..bb2226e07c 100644 --- a/src/xgselect.c +++ b/src/xgselect.c @@ -1,6 +1,6 @@ /* Function for handling the GLib event loop. -Copyright (C) 2009-2014 Free Software Foundation, Inc. +Copyright (C) 2009-2015 Free Software Foundation, Inc. This file is part of GNU Emacs. @@ -28,41 +28,55 @@ along with GNU Emacs. If not, see . */ #include #include #include "frame.h" +#include "blockinput.h" + +/* `xg_select' is a `pselect' replacement. Why do we need a separate function? + 1. Timeouts. Glib and Gtk rely on timer events. If we did pselect + with a greater timeout then the one scheduled by Glib, we would + not allow Glib to process its timer events. We want Glib to + work smoothly, so we need to reduce our timeout to match Glib. + 2. Descriptors. Glib may listen to more file descriptors than we do. + So we add Glib descriptors to our pselect pool, but we don't change + the value returned by the function. The return value matches only + the descriptors passed as arguments, making it compatible with + plain pselect. */ int xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timespec const *timeout, sigset_t const *sigmask) { - fd_set all_rfds, all_wfds, all_efds; + fd_set all_rfds, all_wfds; struct timespec tmo; struct timespec const *tmop = timeout; GMainContext *context; + int have_wfds = wfds != NULL; GPollFD gfds_buf[128]; GPollFD *gfds = gfds_buf; - int gfds_size = sizeof gfds_buf / sizeof *gfds_buf; - int n_gfds, retval = 0, all_lim = fds_lim; + int gfds_size = ARRAYELTS (gfds_buf); + int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1; + bool context_acquired = false; int i, nfds, tmo_in_millisec; bool need_to_dispatch; USE_SAFE_ALLOCA; - /* Do not try to optimize with an initial check with g_main_context_pending - and a call to pselect if it returns false. If Gdk has a timeout for 0.01 - second, and Emacs has a timeout for 1 second, g_main_context_pending will - return false, but the timeout will be 1 second, thus missing the gdk - timeout with a lot. */ - context = g_main_context_default (); + context_acquired = g_main_context_acquire (context); + /* FIXME: If we couldn't acquire the context, we just silently proceed + because this function handles more than just glib file descriptors. + Note that, as implemented, this failure is completely silent: there is + no feedback to the caller. */ if (rfds) all_rfds = *rfds; else FD_ZERO (&all_rfds); if (wfds) all_wfds = *wfds; else FD_ZERO (&all_wfds); - if (efds) all_efds = *efds; - else FD_ZERO (&all_efds); - n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec, - gfds, gfds_size); + n_gfds = (context_acquired + ? g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec, + gfds, gfds_size) + : -1); + if (gfds_size < n_gfds) { SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds); @@ -72,26 +86,23 @@ xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds, } for (i = 0; i < n_gfds; ++i) - if (gfds[i].events & (G_IO_IN | G_IO_OUT | G_IO_PRI)) - { - int fd = gfds[i].fd; - for (; all_lim <= fd; all_lim++) - { - FD_CLR (all_lim, &all_rfds); - FD_CLR (all_lim, &all_wfds); - FD_CLR (all_lim, &all_efds); - } - if (gfds[i].events & G_IO_IN) - FD_SET (fd, &all_rfds); - if (gfds[i].events & G_IO_OUT) - FD_SET (fd, &all_wfds); - if (gfds[i].events & G_IO_PRI) - FD_SET (fd, &all_efds); - } + { + if (gfds[i].events & G_IO_IN) + { + FD_SET (gfds[i].fd, &all_rfds); + if (gfds[i].fd > max_fds) max_fds = gfds[i].fd; + } + if (gfds[i].events & G_IO_OUT) + { + FD_SET (gfds[i].fd, &all_wfds); + if (gfds[i].fd > max_fds) max_fds = gfds[i].fd; + have_wfds = 1; + } + } SAFE_FREE (); - if (tmo_in_millisec >= 0) + if (n_gfds >= 0 && tmo_in_millisec >= 0) { tmo = make_timespec (tmo_in_millisec / 1000, 1000 * 1000 * (tmo_in_millisec % 1000)); @@ -99,35 +110,34 @@ xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds, tmop = &tmo; } - nfds = pselect (all_lim, &all_rfds, &all_wfds, &all_efds, tmop, sigmask); + fds_lim = max_fds + 1; + nfds = pselect (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL, + efds, tmop, sigmask); if (nfds < 0) retval = nfds; - else + else if (nfds > 0) { for (i = 0; i < fds_lim; ++i) { - if (rfds && FD_ISSET (i, rfds)) - { - if (FD_ISSET (i, &all_rfds)) - retval++; - else - FD_CLR (i, rfds); - } - if (wfds && FD_ISSET (i, wfds)) - { - if (FD_ISSET (i, &all_wfds)) - retval++; - else - FD_CLR (i, wfds); - } - if (efds && FD_ISSET (i, efds)) - { - if (FD_ISSET (i, &all_efds)) - retval++; - else - FD_CLR (i, efds); - } + if (FD_ISSET (i, &all_rfds)) + { + if (rfds && FD_ISSET (i, rfds)) ++retval; + else ++our_fds; + } + else if (rfds) + FD_CLR (i, rfds); + + if (have_wfds && FD_ISSET (i, &all_wfds)) + { + if (wfds && FD_ISSET (i, wfds)) ++retval; + else ++our_fds; + } + else if (wfds) + FD_CLR (i, wfds); + + if (efds && FD_ISSET (i, efds)) + ++retval; } } @@ -141,13 +151,21 @@ xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds, if (need_to_dispatch) { int pselect_errno = errno; + /* Prevent g_main_dispatch recursion, that would occur without + block_input wrapper, because event handlers call + unblock_input. Event loop recursion was causing Bug#15801. */ + block_input (); while (g_main_context_pending (context)) - g_main_context_dispatch (context); + g_main_context_dispatch (context); + unblock_input (); errno = pselect_errno; } + if (context_acquired) + g_main_context_release (context); + /* To not have to recalculate timeout, return like this. */ - if (retval == 0 && (0 < nfds || tmop == &tmo)) + if ((our_fds > 0 || (nfds == 0 && tmop == &tmo)) && (retval == 0)) { retval = -1; errno = EINTR;