select.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef _SYS_SELECT_H
  2. #define _SYS_SELECT_H
  3. /* We don't define fd_set and friends if we are compiling POSIX
  4. source, or if we have included (or may include as indicated
  5. by __USE_W32_SOCKETS) the W32api winsock[2].h header which
  6. defines Windows versions of them. Note that a program which
  7. includes the W32api winsock[2].h header must know what it is doing;
  8. it must not call the Cygwin select function.
  9. */
  10. # if !(defined (_WINSOCK_H) || defined (_WINSOCKAPI_) || defined (__USE_W32_SOCKETS))
  11. #include <sys/cdefs.h>
  12. #include <sys/_sigset.h>
  13. #include <sys/_timeval.h>
  14. #include <sys/timespec.h>
  15. #if !defined(_SIGSET_T_DECLARED)
  16. #define _SIGSET_T_DECLARED
  17. typedef __sigset_t sigset_t;
  18. #endif
  19. # define _SYS_TYPES_FD_SET
  20. /*
  21. * Select uses bit masks of file descriptors in longs.
  22. * These macros manipulate such bit fields (the filesystem macros use chars).
  23. * FD_SETSIZE may be defined by the user, but the default here
  24. * should be enough for most uses.
  25. */
  26. #ifndef FD_SETSIZE
  27. # ifdef __CYGWIN__
  28. # define FD_SETSIZE 1024
  29. # elif defined(__rtems__)
  30. # define FD_SETSIZE 256
  31. # else
  32. # define FD_SETSIZE 64
  33. # endif
  34. #endif
  35. typedef unsigned long __fd_mask;
  36. #if __BSD_VISIBLE
  37. typedef __fd_mask fd_mask;
  38. #endif
  39. #define _NFDBITS ((int)sizeof(__fd_mask) * 8) /* bits per mask */
  40. #if __BSD_VISIBLE
  41. #define NFDBITS _NFDBITS
  42. #endif
  43. #ifndef _howmany
  44. #define _howmany(x,y) (((x) + ((y) - 1)) / (y))
  45. #endif
  46. typedef struct fd_set {
  47. __fd_mask __fds_bits[_howmany(FD_SETSIZE, _NFDBITS)];
  48. } fd_set;
  49. #if __BSD_VISIBLE
  50. #define fds_bits __fds_bits
  51. #endif
  52. #define __fdset_mask(n) ((__fd_mask)1 << ((n) % _NFDBITS))
  53. #define FD_CLR(n, p) ((p)->__fds_bits[(n)/_NFDBITS] &= ~__fdset_mask(n))
  54. #if __BSD_VISIBLE
  55. #define FD_COPY(f, t) (void)(*(t) = *(f))
  56. #endif
  57. #define FD_ISSET(n, p) (((p)->__fds_bits[(n)/_NFDBITS] & __fdset_mask(n)) != 0)
  58. #define FD_SET(n, p) ((p)->__fds_bits[(n)/_NFDBITS] |= __fdset_mask(n))
  59. #define FD_ZERO(p) do { \
  60. fd_set *_p; \
  61. __size_t _n; \
  62. \
  63. _p = (p); \
  64. _n = _howmany(FD_SETSIZE, _NFDBITS); \
  65. while (_n > 0) \
  66. _p->__fds_bits[--_n] = 0; \
  67. } while (0)
  68. #if !defined (__INSIDE_CYGWIN_NET__)
  69. __BEGIN_DECLS
  70. int select __P ((int __n, fd_set *__readfds, fd_set *__writefds,
  71. fd_set *__exceptfds, struct timeval *__timeout));
  72. #if __POSIX_VISIBLE >= 200112
  73. int pselect __P ((int __n, fd_set *__readfds, fd_set *__writefds,
  74. fd_set *__exceptfds, const struct timespec *__timeout,
  75. const sigset_t *__set));
  76. #endif
  77. __END_DECLS
  78. #endif /* !__INSIDE_CYGWIN_NET__ */
  79. #endif /* !(_WINSOCK_H || _WINSOCKAPI_ || __USE_W32_SOCKETS) */
  80. #endif /* sys/select.h */