Blame glib/gpoll.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * gpoll.c: poll(2) abstraction
Packit ae235b
 * Copyright 1998 Owen Taylor
Packit ae235b
 * Copyright 2008 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "glibconfig.h"
Packit ae235b
#include "giochannel.h"
Packit ae235b
Packit ae235b
/* Uncomment the next line (and the corresponding line in gmain.c) to
Packit ae235b
 * enable debugging printouts if the environment variable
Packit ae235b
 * G_MAIN_POLL_DEBUG is set to some value.
Packit ae235b
 */
Packit ae235b
/* #define G_MAIN_POLL_DEBUG */
Packit ae235b
Packit ae235b
#ifdef _WIN32
Packit ae235b
/* Always enable debugging printout on Windows, as it is more often
Packit ae235b
 * needed there...
Packit ae235b
 */
Packit ae235b
#define G_MAIN_POLL_DEBUG
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <time.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#ifdef HAVE_SYS_TIME_H
Packit ae235b
#include <sys/time.h>
Packit ae235b
#endif /* HAVE_SYS_TIME_H */
Packit ae235b
#ifdef HAVE_POLL
Packit ae235b
#  include <poll.h>
Packit ae235b
Packit ae235b
/* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
Packit ae235b
 * so we prefer our own poll emulation.
Packit ae235b
 */
Packit ae235b
#if defined(_POLL_EMUL_H_) || defined(BROKEN_POLL)
Packit ae235b
#undef HAVE_POLL
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#endif /* GLIB_HAVE_SYS_POLL_H */
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif /* G_OS_UNIX */
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#define STRICT
Packit ae235b
#include <windows.h>
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#include "gpoll.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include "gprintf.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_MAIN_POLL_DEBUG
Packit ae235b
extern gboolean _g_main_poll_debug;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef HAVE_POLL
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_poll:
Packit ae235b
 * @fds: file descriptors to poll
Packit ae235b
 * @nfds: the number of file descriptors in @fds
Packit ae235b
 * @timeout: amount of time to wait, in milliseconds, or -1 to wait forever
Packit ae235b
 *
Packit ae235b
 * Polls @fds, as with the poll() system call, but portably. (On
Packit ae235b
 * systems that don't have poll(), it is emulated using select().)
Packit ae235b
 * This is used internally by #GMainContext, but it can be called
Packit ae235b
 * directly if you need to block until a file descriptor is ready, but
Packit ae235b
 * don't want to run the full main loop.
Packit ae235b
 *
Packit ae235b
 * Each element of @fds is a #GPollFD describing a single file
Packit ae235b
 * descriptor to poll. The @fd field indicates the file descriptor,
Packit ae235b
 * and the @events field indicates the events to poll for. On return,
Packit ae235b
 * the @revents fields will be filled with the events that actually
Packit ae235b
 * occurred.
Packit ae235b
 *
Packit ae235b
 * On POSIX systems, the file descriptors in @fds can be any sort of
Packit ae235b
 * file descriptor, but the situation is much more complicated on
Packit ae235b
 * Windows. If you need to use g_poll() in code that has to run on
Packit ae235b
 * Windows, the easiest solution is to construct all of your
Packit ae235b
 * #GPollFDs with g_io_channel_win32_make_pollfd().
Packit ae235b
 *
Packit ae235b
 * Returns: the number of entries in @fds whose @revents fields
Packit ae235b
 * were filled in, or 0 if the operation timed out, or -1 on error or
Packit ae235b
 * if the call was interrupted.
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_poll (GPollFD *fds,
Packit ae235b
	guint    nfds,
Packit ae235b
	gint     timeout)
Packit ae235b
{
Packit ae235b
  return poll ((struct pollfd *)fds, nfds, timeout);
Packit ae235b
}
Packit ae235b
Packit ae235b
#else	/* !HAVE_POLL */
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
static int
Packit ae235b
poll_rest (GPollFD *msg_fd,
Packit ae235b
           HANDLE  *handles,
Packit ae235b
           GPollFD *handle_to_fd[],
Packit ae235b
           gint     nhandles,
Packit ae235b
           gint     timeout)
Packit ae235b
{
Packit ae235b
  DWORD ready;
Packit ae235b
  GPollFD *f;
Packit ae235b
  int recursed_result;
Packit ae235b
Packit ae235b
  if (msg_fd != NULL)
Packit ae235b
    {
Packit ae235b
      /* Wait for either messages or handles
Packit ae235b
       * -> Use MsgWaitForMultipleObjectsEx
Packit ae235b
       */
Packit ae235b
      if (_g_main_poll_debug)
Packit ae235b
	g_print ("  MsgWaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
Packit ae235b
Packit ae235b
      ready = MsgWaitForMultipleObjectsEx (nhandles, handles, timeout,
Packit ae235b
					   QS_ALLINPUT, MWMO_ALERTABLE);
Packit ae235b
Packit ae235b
      if (ready == WAIT_FAILED)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
	  g_warning ("MsgWaitForMultipleObjectsEx failed: %s", emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else if (nhandles == 0)
Packit ae235b
    {
Packit ae235b
      /* No handles to wait for, just the timeout */
Packit ae235b
      if (timeout == INFINITE)
Packit ae235b
	ready = WAIT_FAILED;
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* Wait for the current process to die, more efficient than SleepEx(). */
Packit ae235b
          WaitForSingleObjectEx (GetCurrentProcess (), timeout, TRUE);
Packit ae235b
          ready = WAIT_TIMEOUT;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* Wait for just handles
Packit ae235b
       * -> Use WaitForMultipleObjectsEx
Packit ae235b
       */
Packit ae235b
      if (_g_main_poll_debug)
Packit ae235b
	g_print ("  WaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
Packit ae235b
Packit ae235b
      ready = WaitForMultipleObjectsEx (nhandles, handles, FALSE, timeout, TRUE);
Packit ae235b
      if (ready == WAIT_FAILED)
Packit ae235b
	{
Packit ae235b
	  gchar *emsg = g_win32_error_message (GetLastError ());
Packit ae235b
	  g_warning ("WaitForMultipleObjectsEx failed: %s", emsg);
Packit ae235b
	  g_free (emsg);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_main_poll_debug)
Packit ae235b
    g_print ("  wait returns %ld%s\n",
Packit ae235b
	     ready,
Packit ae235b
	     (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
Packit ae235b
	      (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
Packit ae235b
	       (msg_fd != NULL && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
Packit ae235b
Packit ae235b
  if (ready == WAIT_FAILED)
Packit ae235b
    return -1;
Packit ae235b
  else if (ready == WAIT_TIMEOUT ||
Packit ae235b
	   ready == WAIT_IO_COMPLETION)
Packit ae235b
    return 0;
Packit ae235b
  else if (msg_fd != NULL && ready == WAIT_OBJECT_0 + nhandles)
Packit ae235b
    {
Packit ae235b
      msg_fd->revents |= G_IO_IN;
Packit ae235b
Packit ae235b
      /* If we have a timeout, or no handles to poll, be satisfied
Packit ae235b
       * with just noticing we have messages waiting.
Packit ae235b
       */
Packit ae235b
      if (timeout != 0 || nhandles == 0)
Packit ae235b
	return 1;
Packit ae235b
Packit ae235b
      /* If no timeout and handles to poll, recurse to poll them,
Packit ae235b
       * too.
Packit ae235b
       */
Packit ae235b
      recursed_result = poll_rest (NULL, handles, handle_to_fd, nhandles, 0);
Packit ae235b
      return (recursed_result == -1) ? -1 : 1 + recursed_result;
Packit ae235b
    }
Packit ae235b
  else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
Packit ae235b
    {
Packit ae235b
      f = handle_to_fd[ready - WAIT_OBJECT_0];
Packit ae235b
      f->revents = f->events;
Packit ae235b
      if (_g_main_poll_debug)
Packit ae235b
        g_print ("  got event %p\n", (HANDLE) f->fd);
Packit ae235b
Packit ae235b
      /* If no timeout and polling several handles, recurse to poll
Packit ae235b
       * the rest of them.
Packit ae235b
       */
Packit ae235b
      if (timeout == 0 && nhandles > 1)
Packit ae235b
	{
Packit ae235b
	  /* Poll the handles with index > ready */
Packit ae235b
          HANDLE  *shorter_handles;
Packit ae235b
          GPollFD **shorter_handle_to_fd;
Packit ae235b
          gint     shorter_nhandles;
Packit ae235b
Packit ae235b
          shorter_handles = &handles[ready - WAIT_OBJECT_0 + 1];
Packit ae235b
          shorter_handle_to_fd = &handle_to_fd[ready - WAIT_OBJECT_0 + 1];
Packit ae235b
          shorter_nhandles = nhandles - (ready - WAIT_OBJECT_0 + 1);
Packit ae235b
Packit ae235b
	  recursed_result = poll_rest (NULL, shorter_handles, shorter_handle_to_fd, shorter_nhandles, 0);
Packit ae235b
	  return (recursed_result == -1) ? -1 : 1 + recursed_result;
Packit ae235b
	}
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_poll (GPollFD *fds,
Packit ae235b
	guint    nfds,
Packit ae235b
	gint     timeout)
Packit ae235b
{
Packit ae235b
  HANDLE handles[MAXIMUM_WAIT_OBJECTS];
Packit ae235b
  GPollFD *handle_to_fd[MAXIMUM_WAIT_OBJECTS];
Packit ae235b
  GPollFD *msg_fd = NULL;
Packit ae235b
  GPollFD *f;
Packit ae235b
  gint nhandles = 0;
Packit ae235b
  int retval;
Packit ae235b
Packit ae235b
  if (_g_main_poll_debug)
Packit ae235b
    g_print ("g_poll: waiting for");
Packit ae235b
Packit ae235b
  for (f = fds; f < &fds[nfds]; ++f)
Packit ae235b
    {
Packit ae235b
      if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN))
Packit ae235b
        {
Packit ae235b
          if (_g_main_poll_debug && msg_fd == NULL)
Packit ae235b
            g_print (" MSG");
Packit ae235b
          msg_fd = f;
Packit ae235b
        }
Packit ae235b
      else if (f->fd > 0)
Packit ae235b
        {
Packit ae235b
          if (nhandles == MAXIMUM_WAIT_OBJECTS)
Packit ae235b
            {
Packit ae235b
              g_warning ("Too many handles to wait for!\n");
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              if (_g_main_poll_debug)
Packit ae235b
                g_print (" %p", (HANDLE) f->fd);
Packit ae235b
              handle_to_fd[nhandles] = f;
Packit ae235b
              handles[nhandles++] = (HANDLE) f->fd;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      f->revents = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (_g_main_poll_debug)
Packit ae235b
    g_print ("\n");
Packit ae235b
Packit ae235b
  if (timeout == -1)
Packit ae235b
    timeout = INFINITE;
Packit ae235b
Packit ae235b
  /* Polling for several things? */
Packit ae235b
  if (nhandles > 1 || (nhandles > 0 && msg_fd != NULL))
Packit ae235b
    {
Packit ae235b
      /* First check if one or several of them are immediately
Packit ae235b
       * available
Packit ae235b
       */
Packit ae235b
      retval = poll_rest (msg_fd, handles, handle_to_fd, nhandles, 0);
Packit ae235b
Packit ae235b
      /* If not, and we have a significant timeout, poll again with
Packit ae235b
       * timeout then. Note that this will return indication for only
Packit ae235b
       * one event, or only for messages.
Packit ae235b
       */
Packit ae235b
      if (retval == 0 && (timeout == INFINITE || timeout > 0))
Packit ae235b
	retval = poll_rest (msg_fd, handles, handle_to_fd, nhandles, timeout);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* Just polling for one thing, so no need to check first if
Packit ae235b
       * available immediately
Packit ae235b
       */
Packit ae235b
      retval = poll_rest (msg_fd, handles, handle_to_fd, nhandles, timeout);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (retval == -1)
Packit ae235b
    for (f = fds; f < &fds[nfds]; ++f)
Packit ae235b
      f->revents = 0;
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
#else  /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
/* The following implementation of poll() comes from the GNU C Library.
Packit ae235b
 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <string.h> /* for bzero on BSD systems */
Packit ae235b
Packit ae235b
#ifdef HAVE_SYS_SELECT_H
Packit ae235b
#include <sys/select.h>
Packit ae235b
#endif /* HAVE_SYS_SELECT_H */
Packit ae235b
Packit ae235b
#ifndef NO_FD_SET
Packit ae235b
#  define SELECT_MASK fd_set
Packit ae235b
#else /* !NO_FD_SET */
Packit ae235b
#  ifndef _AIX
Packit ae235b
typedef long fd_mask;
Packit ae235b
#  endif /* _AIX */
Packit ae235b
#  ifdef _IBMR2
Packit ae235b
#    define SELECT_MASK void
Packit ae235b
#  else /* !_IBMR2 */
Packit ae235b
#    define SELECT_MASK int
Packit ae235b
#  endif /* !_IBMR2 */
Packit ae235b
#endif /* !NO_FD_SET */
Packit ae235b
Packit ae235b
gint
Packit ae235b
g_poll (GPollFD *fds,
Packit ae235b
	guint    nfds,
Packit ae235b
	gint     timeout)
Packit ae235b
{
Packit ae235b
  struct timeval tv;
Packit ae235b
  SELECT_MASK rset, wset, xset;
Packit ae235b
  GPollFD *f;
Packit ae235b
  int ready;
Packit ae235b
  int maxfd = 0;
Packit ae235b
Packit ae235b
  FD_ZERO (&rset);
Packit ae235b
  FD_ZERO (&wset);
Packit ae235b
  FD_ZERO (&xset);
Packit ae235b
Packit ae235b
  for (f = fds; f < &fds[nfds]; ++f)
Packit ae235b
    if (f->fd >= 0)
Packit ae235b
      {
Packit ae235b
	if (f->events & G_IO_IN)
Packit ae235b
	  FD_SET (f->fd, &rset);
Packit ae235b
	if (f->events & G_IO_OUT)
Packit ae235b
	  FD_SET (f->fd, &wset);
Packit ae235b
	if (f->events & G_IO_PRI)
Packit ae235b
	  FD_SET (f->fd, &xset);
Packit ae235b
	if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
Packit ae235b
	  maxfd = f->fd;
Packit ae235b
      }
Packit ae235b
Packit ae235b
  tv.tv_sec = timeout / 1000;
Packit ae235b
  tv.tv_usec = (timeout % 1000) * 1000;
Packit ae235b
Packit ae235b
  ready = select (maxfd + 1, &rset, &wset, &xset,
Packit ae235b
		  timeout == -1 ? NULL : &tv;;
Packit ae235b
  if (ready > 0)
Packit ae235b
    for (f = fds; f < &fds[nfds]; ++f)
Packit ae235b
      {
Packit ae235b
	f->revents = 0;
Packit ae235b
	if (f->fd >= 0)
Packit ae235b
	  {
Packit ae235b
	    if (FD_ISSET (f->fd, &rset))
Packit ae235b
	      f->revents |= G_IO_IN;
Packit ae235b
	    if (FD_ISSET (f->fd, &wset))
Packit ae235b
	      f->revents |= G_IO_OUT;
Packit ae235b
	    if (FD_ISSET (f->fd, &xset))
Packit ae235b
	      f->revents |= G_IO_PRI;
Packit ae235b
	  }
Packit ae235b
      }
Packit ae235b
Packit ae235b
  return ready;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif /* !G_OS_WIN32 */
Packit ae235b
Packit ae235b
#endif	/* !HAVE_POLL */