select.h 758 B

12345678910111213141516171819202122
  1. #ifndef _BITS_SYS_SELECT_H
  2. #define _BITS_SYS_SELECT_H
  3. #define FD_SETSIZE 1024
  4. typedef struct fd_set {
  5. unsigned long fds_bits[FD_SETSIZE / (8 * sizeof(unsigned long))];
  6. } fd_set;
  7. #define _FD_INDEX(fd) ((fd) / (8 * sizeof(unsigned long)))
  8. #define _FD_BITMASK(fd) (1UL << ((fd) & (8 * sizeof(unsigned long) - 1)))
  9. #define FD_ZERO(set) for (int i = 0; i < sizeof((set)->fds_bits) / sizeof(unsigned long); i += 1) { \
  10. (set)->fds_bits[i] = 0; \
  11. }
  12. #define FD_SET(fd, set) ((set)->fds_bits[_FD_INDEX(fd)] |= _FD_BITMASK(fd))
  13. #define FD_CLR(fd, set) ((set)->fds_bits[_FD_INDEX(fd)] &= ~(_FD_BITMASK(fd)))
  14. #define FD_ISSET(fd, set) (((set)->fds_bits[_FD_INDEX(fd)] & _FD_BITMASK(fd)) == _FD_BITMASK(fd))
  15. #endif