1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gpoll.c: poll(2) abstraction
5 * Copyright 1998 Owen Taylor
6 * Copyright 2008 Red Hat, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GLib Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 #include "glibconfig.h"
37 #include "giochannel.h"
39 /* Uncomment the next line (and the corresponding line in gmain.c) to
40 * enable debugging printouts if the environment variable
41 * G_MAIN_POLL_DEBUG is set to some value.
43 /* #define G_MAIN_POLL_DEBUG */
46 /* Always enable debugging printout on Windows, as it is more often
49 #define G_MAIN_POLL_DEBUG
52 #include <sys/types.h>
55 #ifdef HAVE_SYS_TIME_H
57 #endif /* HAVE_SYS_TIME_H */
58 #ifdef GLIB_HAVE_SYS_POLL_H
59 # include <sys/poll.h>
61 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
62 * so we prefer our own poll emulation.
64 #if defined(_POLL_EMUL_H_) || defined(BROKEN_POLL)
68 #endif /* GLIB_HAVE_SYS_POLL_H */
71 #endif /* G_OS_UNIX */
77 #endif /* G_OS_WIN32 */
85 #ifdef G_MAIN_POLL_DEBUG
86 extern gboolean _g_main_poll_debug;
93 * @fds: file descriptors to poll
94 * @nfds: the number of file descriptors in @fds
95 * @timeout: amount of time to wait, in milliseconds, or -1 to wait forever
97 * Polls @fds, as with the poll() system call, but portably. (On
98 * systems that don't have poll(), it is emulated using select().)
99 * This is used internally by #GMainContext, but it can be called
100 * directly if you need to block until a file descriptor is ready, but
101 * don't want to run the full main loop.
103 * Each element of @fds is a #GPollFD describing a single file
104 * descriptor to poll. The %fd field indicates the file descriptor,
105 * and the %events field indicates the events to poll for. On return,
106 * the %revents fields will be filled with the events that actually
109 * On POSIX systems, the file descriptors in @fds can be any sort of
110 * file descriptor, but the situation is much more complicated on
111 * Windows. If you need to use g_poll() in code that has to run on
112 * Windows, the easiest solution is to construct all of your
113 * #GPollFD<!-- -->s with g_io_channel_win32_make_pollfd().
115 * Return value: the number of entries in @fds whose %revents fields
116 * were filled in, or 0 if the operation timed out, or -1 on error or
117 * if the call was interrupted.
122 g_poll (GPollFD *fds,
126 return poll ((struct pollfd *)fds, nfds, timeout);
129 #else /* !HAVE_POLL */
134 poll_rest (gboolean poll_msgs,
147 /* Wait for either messages or handles
148 * -> Use MsgWaitForMultipleObjectsEx
150 if (_g_main_poll_debug)
151 g_print (" MsgWaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
153 ready = MsgWaitForMultipleObjectsEx (nhandles, handles, timeout,
154 QS_ALLINPUT, MWMO_ALERTABLE);
156 if (ready == WAIT_FAILED)
158 gchar *emsg = g_win32_error_message (GetLastError ());
159 g_warning ("MsgWaitForMultipleObjectsEx failed: %s", emsg);
163 else if (nhandles == 0)
165 /* No handles to wait for, just the timeout */
166 if (timeout == INFINITE)
170 SleepEx (timeout, TRUE);
171 ready = WAIT_TIMEOUT;
176 /* Wait for just handles
177 * -> Use WaitForMultipleObjectsEx
179 if (_g_main_poll_debug)
180 g_print (" WaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
182 ready = WaitForMultipleObjectsEx (nhandles, handles, FALSE, timeout, TRUE);
183 if (ready == WAIT_FAILED)
185 gchar *emsg = g_win32_error_message (GetLastError ());
186 g_warning ("WaitForMultipleObjectsEx failed: %s", emsg);
191 if (_g_main_poll_debug)
192 g_print (" wait returns %ld%s\n",
194 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
195 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
196 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
198 if (ready == WAIT_FAILED)
200 else if (ready == WAIT_TIMEOUT ||
201 ready == WAIT_IO_COMPLETION)
203 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
205 for (f = fds; f < &fds[nfds]; ++f)
206 if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN)
207 f->revents |= G_IO_IN;
209 /* If we have a timeout, or no handles to poll, be satisfied
210 * with just noticing we have messages waiting.
212 if (timeout != 0 || nhandles == 0)
215 /* If no timeout and handles to poll, recurse to poll them,
218 recursed_result = poll_rest (FALSE, handles, nhandles, fds, nfds, 0);
219 return (recursed_result == -1) ? -1 : 1 + recursed_result;
221 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
223 for (f = fds; f < &fds[nfds]; ++f)
225 if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0])
227 f->revents = f->events;
228 if (_g_main_poll_debug)
229 g_print (" got event %p\n", (HANDLE) f->fd);
233 /* If no timeout and polling several handles, recurse to poll
236 if (timeout == 0 && nhandles > 1)
238 /* Remove the handle that fired */
240 if (ready < nhandles - 1)
241 for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++)
242 handles[i-1] = handles[i];
244 recursed_result = poll_rest (FALSE, handles, nhandles, fds, nfds, 0);
245 return (recursed_result == -1) ? -1 : 1 + recursed_result;
254 g_poll (GPollFD *fds,
258 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
259 gboolean poll_msgs = FALSE;
264 if (_g_main_poll_debug)
265 g_print ("g_poll: waiting for");
267 for (f = fds; f < &fds[nfds]; ++f)
268 if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN))
270 if (_g_main_poll_debug && !poll_msgs)
276 /* Don't add the same handle several times into the array, as
277 * docs say that is not allowed, even if it actually does seem
282 for (i = 0; i < nhandles; i++)
283 if (handles[i] == (HANDLE) f->fd)
288 if (nhandles == MAXIMUM_WAIT_OBJECTS)
290 g_warning ("Too many handles to wait for!\n");
295 if (_g_main_poll_debug)
296 g_print (" %p", (HANDLE) f->fd);
297 handles[nhandles++] = (HANDLE) f->fd;
302 if (_g_main_poll_debug)
305 for (f = fds; f < &fds[nfds]; ++f)
311 /* Polling for several things? */
312 if (nhandles > 1 || (nhandles > 0 && poll_msgs))
314 /* First check if one or several of them are immediately
317 retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, 0);
319 /* If not, and we have a significant timeout, poll again with
320 * timeout then. Note that this will return indication for only
321 * one event, or only for messages. We ignore timeouts less than
322 * ten milliseconds as they are mostly pointless on Windows, the
323 * MsgWaitForMultipleObjectsEx() call will timeout right away
326 if (retval == 0 && (timeout == INFINITE || timeout >= 10))
327 retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout);
331 /* Just polling for one thing, so no need to check first if
332 * available immediately
334 retval = poll_rest (poll_msgs, handles, nhandles, fds, nfds, timeout);
338 for (f = fds; f < &fds[nfds]; ++f)
344 #else /* !G_OS_WIN32 */
346 /* The following implementation of poll() comes from the GNU C Library.
347 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
350 #include <string.h> /* for bzero on BSD systems */
352 #ifdef HAVE_SYS_SELECT_H
353 #include <sys/select.h>
354 #endif /* HAVE_SYS_SELECT_H */
357 # define SELECT_MASK fd_set
358 #else /* !NO_FD_SET */
360 typedef long fd_mask;
363 # define SELECT_MASK void
365 # define SELECT_MASK int
366 # endif /* !_IBMR2 */
367 #endif /* !NO_FD_SET */
370 g_poll (GPollFD *fds,
375 SELECT_MASK rset, wset, xset;
384 for (f = fds; f < &fds[nfds]; ++f)
387 if (f->events & G_IO_IN)
388 FD_SET (f->fd, &rset);
389 if (f->events & G_IO_OUT)
390 FD_SET (f->fd, &wset);
391 if (f->events & G_IO_PRI)
392 FD_SET (f->fd, &xset);
393 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
397 tv.tv_sec = timeout / 1000;
398 tv.tv_usec = (timeout % 1000) * 1000;
400 ready = select (maxfd + 1, &rset, &wset, &xset,
401 timeout == -1 ? NULL : &tv);
403 for (f = fds; f < &fds[nfds]; ++f)
408 if (FD_ISSET (f->fd, &rset))
409 f->revents |= G_IO_IN;
410 if (FD_ISSET (f->fd, &wset))
411 f->revents |= G_IO_OUT;
412 if (FD_ISSET (f->fd, &xset))
413 f->revents |= G_IO_PRI;
420 #endif /* !G_OS_WIN32 */
422 #endif /* !HAVE_POLL */