bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH]: pselect: new module


From: Paul Eggert
Subject: [PATCH]: pselect: new module
Date: Thu, 30 Jun 2011 15:02:34 -0700
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Thunderbird/3.1.10

I pushed this; it's for Emacs, but I expect other applications
can use nanosecond-resolution 'select' as well.  No doubt there
will be porting glitches.  I've tested it on Solaris 8 (!) and
Fedora 14 for starters.

pselect: new module
* lib/sys_select.in.h: Include <signal.h>, for 'sigset_t'.
(pselect): New decls.
* m4/sys_select_h.m4 (gl_HEADER_SYS_SELECT): Require AC_C_RESTRICT,
since the standard pselect decl uses 'restrict'.
(gl_SYS_SELECT_H_DEFAULTS): Add defaults for GNULIB_PSELECT,
HAVE_PSELECT, REPLACE_PSELECT.
* modules/sys_select (sys/select.h): Substitute GNULIB_PSELECT,
HAVE_PSELECT, REPLACE_PSELECT.
* lib/pselect.c, m4/pselect.m4, modules/pselect: New files.
diff --git a/lib/pselect.c b/lib/pselect.c
new file mode 100644
index 0000000..7eec89c
--- /dev/null
+++ b/lib/pselect.c
@@ -0,0 +1,79 @@
+/* pselect - synchronous I/O multiplexing
+
+   Copyright 2011 Free Software Foundation, Inc.
+
+   This file is part of gnulib.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   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 this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* written by Paul Eggert */
+
+#include <config.h>
+
+#include <sys/select.h>
+
+#include <errno.h>
+#include <signal.h>
+
+#undef pselect
+
+/* Examine the size-NFDS file descriptor sets in RFDS, WFDS, and XFDS
+   to see whether some of their descriptors are ready for reading,
+   ready for writing, or have exceptions pending.  Wait for at most
+   TIMEOUT seconds, and use signal mask SIGMASK while waiting.  A null
+   pointer parameter stands for no descriptors, an infinite timeout,
+   or an unaffected signal mask.  */
+
+int
+rpl_pselect (int nfds, fd_set *restrict rfds,
+             fd_set *restrict wfds, fd_set *restrict xfds,
+             struct timespec const *restrict timeout,
+             sigset_t const *restrict sigmask)
+{
+  int select_result;
+  sigset_t origmask;
+  struct timeval tv, *tvp;
+
+  if (timeout)
+    {
+      if (! (0 <= timeout->tv_nsec && timeout->tv_nsec < 1000000000))
+        {
+          errno = EINVAL;
+          return -1;
+        }
+
+      tv.tv_sec = timeout->tv_sec;
+      tv.tv_usec = (timeout->tv_nsec + 999) / 1000;
+      tvp = &tv;
+    }
+  else
+    tvp = NULL;
+
+  /* Signal mask munging should be atomic, but this is the best we can
+     do in this emulation.  */
+  if (sigmask)
+    sigprocmask (SIG_SETMASK, sigmask, &origmask);
+
+  select_result = select (nfds, rfds, wfds, xfds, tvp);
+
+  if (sigmask)
+    {
+      int select_errno = errno;
+      sigprocmask (SIG_SETMASK, &origmask, NULL);
+      errno = select_errno;
+    }
+
+  return select_result;
+}
diff --git a/lib/sys_select.in.h b/lib/sys_select.in.h
index ef4c6f3..ada0311 100644
--- a/lib/sys_select.in.h
+++ b/lib/sys_select.in.h
@@ -85,11 +85,48 @@
 # endif
 #endif

+/* Get definition of 'sigset_t'.
+   But avoid namespace pollution on glibc systems.  */
+#if !(defined __GLIBC__ && !defined __UCLIBC__)
+# include <signal.h>
+#endif
+
 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */

 /* The definition of _GL_WARN_ON_USE is copied here.  */


+#if @GNULIB_PSELECT@
+# if @REPLACE_PSELECT@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef pselect
+#   define pselect rpl_pselect
+#  endif
+_GL_FUNCDECL_RPL (pselect, int,
+                  (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
+                   struct timespec const *restrict, const sigset_t *restrict));
+_GL_CXXALIAS_RPL (pselect, int,
+                  (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
+                   struct timespec const *restrict, const sigset_t *restrict));
+# else
+#  if address@hidden@
+_GL_FUNCDECL_SYS (pselect, int,
+                  (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
+                   struct timespec const *restrict, const sigset_t *restrict));
+#  endif
+_GL_CXXALIAS_SYS (pselect, int,
+                  (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
+                   struct timespec const *restrict, const sigset_t *restrict));
+# endif
+_GL_CXXALIASWARN (pselect);
+#elif defined GNULIB_POSIXCHECK
+# undef pselect
+# if HAVE_RAW_DECL_PSELECT
+_GL_WARN_ON_USE (pselect, "pselect is not portable - "
+                 "use gnulib module pselect for portability");
+# endif
+#endif
+
 #if @GNULIB_SELECT@
 # if @REPLACE_SELECT@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/m4/pselect.m4 b/m4/pselect.m4
new file mode 100644
index 0000000..5a76a7b
--- /dev/null
+++ b/m4/pselect.m4
@@ -0,0 +1,31 @@
+# pselect.m4
+dnl Copyright (C) 2011 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_PSELECT],
+[
+  AC_REQUIRE([gl_HEADER_SYS_SELECT])
+  AC_REQUIRE([AC_C_RESTRICT])
+  AC_CHECK_FUNCS_ONCE([pselect])
+
+  if test $ac_cv_func_pselect = yes; then
+    AC_CACHE_CHECK([whether signature of pselect conforms to POSIX],
+      gl_cv_sig_pselect,
+      [AC_LINK_IFELSE(
+        [AC_LANG_PROGRAM(
+             [[#include <sys/select.h>
+               ]],
+             [[int (*p) (int, fd_set *, fd_set *, fd_set *restrict,
+                         struct timespec const *restrict,
+                         sigset_t const *restrict) = pselect;
+               return !p;]])],
+        [gl_cv_sig_pselect=yes],
+        [gl_cv_sig_pselect=no])])
+  fi
+
+  if test $ac_cv_func_pselect = no || test $gl_cv_sig_pselect = no; then
+    REPLACE_PSELECT=1
+  fi
+])
diff --git a/m4/sys_select_h.m4 b/m4/sys_select_h.m4
index 5b51af4..7d5f477 100644
--- a/m4/sys_select_h.m4
+++ b/m4/sys_select_h.m4
@@ -1,4 +1,4 @@
-# sys_select_h.m4 serial 17
+# sys_select_h.m4 serial 18
 dnl Copyright (C) 2006-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,6 +6,7 @@ dnl with or without modifications, as long as this notice is 
preserved.

 AC_DEFUN([gl_HEADER_SYS_SELECT],
 [
+  AC_REQUIRE([AC_C_RESTRICT])
   AC_REQUIRE([gl_SYS_SELECT_H_DEFAULTS])
   AC_CACHE_CHECK([whether <sys/select.h> is self-contained],
     [gl_cv_header_sys_select_h_selfcontained],
@@ -77,7 +78,10 @@ AC_DEFUN([gl_SYS_SELECT_MODULE_INDICATOR],

 AC_DEFUN([gl_SYS_SELECT_H_DEFAULTS],
 [
+  GNULIB_PSELECT=0; AC_SUBST([GNULIB_PSELECT])
   GNULIB_SELECT=0; AC_SUBST([GNULIB_SELECT])
   dnl Assume proper GNU behavior unless another module says otherwise.
+  HAVE_PSELECT=1; AC_SUBST([HAVE_PSELECT])
+  REPLACE_PSELECT=0; AC_SUBST([REPLACE_PSELECT])
   REPLACE_SELECT=0; AC_SUBST([REPLACE_SELECT])
 ])
diff --git a/modules/pselect b/modules/pselect
new file mode 100644
index 0000000..b899198
--- /dev/null
+++ b/modules/pselect
@@ -0,0 +1,29 @@
+Description:
+pselect() function: synchronous I/O multiplexing.
+
+Files:
+lib/pselect.c
+m4/pselect.m4
+
+Depends-on:
+sys_select
+
+configure.ac:
+gl_FUNC_PSELECT
+if test $REPLACE_PSELECT = 1; then
+  AC_LIBOBJ([pselect])
+fi
+gl_SYS_SELECT_MODULE_INDICATOR([pselect])
+
+Makefile.am:
+
+Include:
+<sys/select.h>
+
+Link:
+
+License:
+LGPLv2+
+
+Maintainer:
+Paul Eggert
diff --git a/modules/sys_select b/modules/sys_select
index d0c0209..cb3cb99 100644
--- a/modules/sys_select
+++ b/modules/sys_select
@@ -31,8 +31,11 @@ sys/select.h: sys_select.in.h $(top_builddir)/config.status 
$(CXXDEFS_H) $(WARN_
              -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
              -e 's|@''NEXT_SYS_SELECT_H''@|$(NEXT_SYS_SELECT_H)|g' \
              -e 's|@''HAVE_SYS_SELECT_H''@|$(HAVE_SYS_SELECT_H)|g' \
+             -e 's/@''GNULIB_PSELECT''@/$(GNULIB_PSELECT)/g' \
              -e 's/@''GNULIB_SELECT''@/$(GNULIB_SELECT)/g' \
              -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \
+             -e 's|@''HAVE_PSELECT''@|$(HAVE_PSELECT)|g' \
+             -e 's|@''REPLACE_PSELECT''@|$(REPLACE_PSELECT)|g' \
              -e 's|@''REPLACE_SELECT''@|$(REPLACE_SELECT)|g' \
              -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
              -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \



reply via email to

[Prev in Thread] Current Thread [Next in Thread]