pselect.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright (C) 1996-1998,2001,2002,2003,2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <stddef.h> /* For NULL. */
  19. #include <sys/time.h>
  20. #include <sys/select.h>
  21. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  22. #include <sysdep-cancel.h>
  23. #endif
  24. libc_hidden_proto(sigprocmask)
  25. libc_hidden_proto(select)
  26. /* Check the first NFDS descriptors each in READFDS (if not NULL) for read
  27. readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
  28. (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
  29. after waiting the interval specified therein. Additionally set the sigmask
  30. SIGMASK for this call. Returns the number of ready descriptors, or -1 for
  31. errors. */
  32. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  33. static int
  34. __pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  35. #else
  36. int
  37. pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  38. #endif
  39. const struct timespec *timeout, const sigset_t *sigmask)
  40. {
  41. struct timeval tval;
  42. int retval;
  43. sigset_t savemask;
  44. /* Change nanosecond number to microseconds. This might mean losing
  45. precision and therefore the `pselect` should be available. But
  46. for now it is hardly found. */
  47. if (timeout != NULL)
  48. TIMESPEC_TO_TIMEVAL (&tval, timeout);
  49. /* The setting and restoring of the signal mask and the select call
  50. should be an atomic operation. This can't be done without kernel
  51. help. */
  52. if (sigmask != NULL)
  53. sigprocmask (SIG_SETMASK, sigmask, &savemask);
  54. /* Note the pselect() is a cancellation point. But since we call
  55. select() which itself is a cancellation point we do not have
  56. to do anything here. */
  57. retval = select (nfds, readfds, writefds, exceptfds,
  58. timeout != NULL ? &tval : NULL);
  59. if (sigmask != NULL)
  60. sigprocmask (SIG_SETMASK, &savemask, NULL);
  61. return retval;
  62. }
  63. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  64. int
  65. pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  66. const struct timespec *timeout, const sigset_t *sigmask)
  67. {
  68. if (SINGLE_THREAD_P)
  69. return __pselect (nfds, readfds, writefds, exceptfds,
  70. timeout, sigmask);
  71. int oldtype = LIBC_CANCEL_ASYNC ();
  72. int result = __pselect (nfds, readfds, writefds, exceptfds,
  73. timeout, sigmask);
  74. LIBC_CANCEL_RESET (oldtype);
  75. return result;
  76. }
  77. #endif