select.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __ESP_SYS_SELECT_H__
  7. #define __ESP_SYS_SELECT_H__
  8. /* Newlib 2.2.0 does not provide sys/select.h, and fd_set is defined in sys/types.h */
  9. #include <sys/types.h>
  10. #ifndef fd_set
  11. #include_next <sys/select.h>
  12. #else // fd_set
  13. #include <sys/time.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout);
  18. #ifdef __cplusplus
  19. } // extern "C"
  20. #endif
  21. #endif // fd_set
  22. #if defined(FD_ISSET) || defined(FD_SET) || defined(FD_CLR)
  23. #undef FD_SET
  24. #undef FD_CLR
  25. #undef FD_ISSET
  26. #define __FD_SAFE_SET(n, code) do { if ((unsigned)(n) < FD_SETSIZE) { code; } } while(0)
  27. #define __FD_SAFE_GET(n, code) (((unsigned)(n) < FD_SETSIZE) ? (code) : 0)
  28. #define FD_SET(n, p) __FD_SAFE_SET(n, ((p)->fds_bits[(n) / NFDBITS] |= (1L << ((n) % NFDBITS))))
  29. #define FD_CLR(n, p) __FD_SAFE_SET(n, ((p)->fds_bits[(n) / NFDBITS] &= ~(1L << ((n) % NFDBITS))))
  30. #define FD_ISSET(n, p) __FD_SAFE_GET(n, ((p)->fds_bits[(n) / NFDBITS] & (1L << ((n) % NFDBITS))))
  31. #endif // FD_ISSET || FD_SET || FD_CLR
  32. #endif //__ESP_SYS_SELECT_H__