use xmalloc_atomic for many pointerless objects
[bpt/emacs.git] / src / xgselect.c
index c7348d2..bf889a9 100644 (file)
@@ -1,6 +1,6 @@
 /* Function for handling the GLib event loop.
 
-Copyright (C) 2009-201 Free Software Foundation, Inc.
+Copyright (C) 2009-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -15,61 +15,66 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs.  If not, see <httpยง://www.gnu.org/licenses/>.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 
-#include <setjmp.h>
 #include "xgselect.h"
 
-#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
+#ifdef HAVE_GLIB
 
 #include <glib.h>
 #include <errno.h>
-#include <setjmp.h>
-#include "xterm.h"
-
-static GPollFD *gfds;
-static ptrdiff_t gfds_size;
+#include <stdbool.h>
+#include <timespec.h>
+#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, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
-          EMACS_TIME *timeout, sigset_t *sigmask)
+xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds,
+          struct timespec const *timeout, sigset_t const *sigmask)
 {
-  SELECT_TYPE all_rfds, all_wfds;
-  EMACS_TIME tmo, *tmop = timeout;
+  fd_set all_rfds, all_wfds;
+  struct timespec tmo;
+  struct timespec const *tmop = timeout;
 
   GMainContext *context;
   int have_wfds = wfds != NULL;
-  int n_gfds = 0, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
+  GPollFD gfds_buf[128];
+  GPollFD *gfds = gfds_buf;
+  int gfds_size = ARRAYELTS (gfds_buf);
+  int n_gfds, retval = 0, our_fds = 0, max_fds = fds_lim - 1;
   int i, nfds, tmo_in_millisec;
+  bool need_to_dispatch;
+  USE_SAFE_ALLOCA;
 
-  if (!x_in_use)
-    return pselect (fds_lim, rfds, wfds, efds, tmop, sigmask);
+  context = g_main_context_default ();
 
-  if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds));
+  if (rfds) all_rfds = *rfds;
   else FD_ZERO (&all_rfds);
-  if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds));
+  if (wfds) all_wfds = *wfds;
   else FD_ZERO (&all_wfds);
 
-  /* Update event sources in GLib. */
-  context = g_main_context_default ();
-  g_main_context_pending (context);
-
-  do {
-    if (n_gfds > gfds_size)
-      {
-        xfree (gfds);
-       gfds = xpalloc (0, &gfds_size, n_gfds - gfds_size, INT_MAX,
-                       sizeof *gfds);
-      }
-
-    n_gfds = g_main_context_query (context,
-                                   G_PRIORITY_LOW,
-                                   &tmo_in_millisec,
-                                   gfds,
-                                   gfds_size);
-  } while (n_gfds > gfds_size);
+  n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
+                                gfds, gfds_size);
+  if (gfds_size < n_gfds)
+    {
+      SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
+      gfds_size = n_gfds;
+      n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
+                                    gfds, gfds_size);
+    }
 
   for (i = 0; i < n_gfds; ++i)
     {
@@ -86,11 +91,13 @@ xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
         }
     }
 
+  SAFE_FREE ();
+
   if (tmo_in_millisec >= 0)
     {
-      EMACS_SET_SECS_USECS (tmo, tmo_in_millisec/1000,
-                            1000 * (tmo_in_millisec % 1000));
-      if (!timeout || EMACS_TIME_LT (tmo, *timeout))
+      tmo = make_timespec (tmo_in_millisec / 1000,
+                          1000 * 1000 * (tmo_in_millisec % 1000));
+      if (!timeout || timespec_cmp (tmo, *timeout) < 0)
        tmop = &tmo;
     }
 
@@ -125,34 +132,33 @@ xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
         }
     }
 
-  if (our_fds > 0 || (nfds == 0 && tmop == &tmo))
-    {
-
-      /* If Gtk+ is in use eventually gtk_main_iteration will be called,
-         unless retval is zero.  */
+  /* If Gtk+ is in use eventually gtk_main_iteration will be called,
+     unless retval is zero.  */
 #ifdef USE_GTK
-      if (retval == 0)
+  need_to_dispatch = retval == 0;
+#else
+  need_to_dispatch = true;
 #endif
-        while (g_main_context_pending (context))
-          g_main_context_dispatch (context);
+  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);
+      unblock_input ();
+      errno = pselect_errno;
+    }
 
-      /* To not have to recalculate timeout, return like this.  */
-      if (retval == 0)
-        {
-          retval = -1;
-          errno = EINTR;
-        }
+  /* To not have to recalculate timeout, return like this.  */
+  if ((our_fds > 0 || (nfds == 0 && tmop == &tmo)) && (retval == 0))
+    {
+      retval = -1;
+      errno = EINTR;
     }
 
   return retval;
 }
-#endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */
-
-void
-xgselect_initialize (void)
-{
-#if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
-  gfds_size = 128;
-  gfds = xmalloc (sizeof (*gfds)*gfds_size);
-#endif
-}
+#endif /* HAVE_GLIB */