Fix some xgselect-vs-pselect bugs.
[bpt/emacs.git] / src / xgselect.c
1 /* Function for handling the GLib event loop.
2
3 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21
22 #include "xgselect.h"
23
24 #ifdef HAVE_GLIB
25
26 #include <glib.h>
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <timespec.h>
30 #include "frame.h"
31
32 int
33 xg_select (int fds_lim, fd_set *rfds, fd_set *wfds, fd_set *efds,
34 struct timespec const *timeout, sigset_t const *sigmask)
35 {
36 fd_set all_rfds, all_wfds, all_efds;
37 struct timespec tmo;
38 struct timespec const *tmop = timeout;
39
40 GMainContext *context;
41 GPollFD gfds_buf[128];
42 GPollFD *gfds = gfds_buf;
43 int gfds_size = sizeof gfds_buf / sizeof *gfds_buf;
44 int n_gfds, retval = 0, all_lim = fds_lim;
45 int i, nfds, tmo_in_millisec;
46 bool need_to_dispatch;
47 USE_SAFE_ALLOCA;
48
49 /* Do not try to optimize with an initial check with g_main_context_pending
50 and a call to pselect if it returns false. If Gdk has a timeout for 0.01
51 second, and Emacs has a timeout for 1 second, g_main_context_pending will
52 return false, but the timeout will be 1 second, thus missing the gdk
53 timeout with a lot. */
54
55 context = g_main_context_default ();
56
57 if (rfds) all_rfds = *rfds;
58 else FD_ZERO (&all_rfds);
59 if (wfds) all_wfds = *wfds;
60 else FD_ZERO (&all_wfds);
61 if (efds) all_efds = *efds;
62 else FD_ZERO (&all_efds);
63
64 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
65 gfds, gfds_size);
66 if (gfds_size < n_gfds)
67 {
68 SAFE_NALLOCA (gfds, sizeof *gfds, n_gfds);
69 gfds_size = n_gfds;
70 n_gfds = g_main_context_query (context, G_PRIORITY_LOW, &tmo_in_millisec,
71 gfds, gfds_size);
72 }
73
74 for (i = 0; i < n_gfds; ++i)
75 if (gfds[i].events & (G_IO_IN | G_IO_OUT | G_IO_PRI))
76 {
77 int fd = gfds[i].fd;
78 for (; all_lim <= fd; all_lim++)
79 {
80 FD_CLR (all_lim, &all_rfds);
81 FD_CLR (all_lim, &all_wfds);
82 FD_CLR (all_lim, &all_efds);
83 }
84 if (gfds[i].events & G_IO_IN)
85 FD_SET (fd, &all_rfds);
86 if (gfds[i].events & G_IO_OUT)
87 FD_SET (fd, &all_wfds);
88 if (gfds[i].events & G_IO_PRI)
89 FD_SET (fd, &all_efds);
90 }
91
92 SAFE_FREE ();
93
94 if (tmo_in_millisec >= 0)
95 {
96 tmo = make_timespec (tmo_in_millisec / 1000,
97 1000 * 1000 * (tmo_in_millisec % 1000));
98 if (!timeout || timespec_cmp (tmo, *timeout) < 0)
99 tmop = &tmo;
100 }
101
102 nfds = pselect (all_lim, &all_rfds, &all_wfds, &all_efds, tmop, sigmask);
103
104 if (nfds < 0)
105 retval = nfds;
106 else
107 {
108 for (i = 0; i < fds_lim; ++i)
109 {
110 if (rfds && FD_ISSET (i, rfds))
111 {
112 if (FD_ISSET (i, &all_rfds))
113 retval++;
114 else
115 FD_CLR (i, rfds);
116 }
117 if (wfds && FD_ISSET (i, wfds))
118 {
119 if (FD_ISSET (i, &all_wfds))
120 retval++;
121 else
122 FD_CLR (i, wfds);
123 }
124 if (efds && FD_ISSET (i, efds))
125 {
126 if (FD_ISSET (i, &all_efds))
127 retval++;
128 else
129 FD_CLR (i, efds);
130 }
131 }
132 }
133
134 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
135 unless retval is zero. */
136 #ifdef USE_GTK
137 need_to_dispatch = retval == 0;
138 #else
139 need_to_dispatch = true;
140 #endif
141 if (need_to_dispatch)
142 {
143 int pselect_errno = errno;
144 while (g_main_context_pending (context))
145 g_main_context_dispatch (context);
146 errno = pselect_errno;
147 }
148
149 /* To not have to recalculate timeout, return like this. */
150 if (retval == 0 && (0 < nfds || tmop == &tmo))
151 {
152 retval = -1;
153 errno = EINTR;
154 }
155
156 return retval;
157 }
158 #endif /* HAVE_GLIB */