select.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef _SYS_SELECT_H_
  2. #define _SYS_SELECT_H_
  3. /*
  4. * Copyright (C) 2013 Ole Reinhardt <ole.reinhardt@embedded-it.de>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. /*!
  36. * \verbatim
  37. * $Id: $
  38. * \endverbatim
  39. */
  40. #include "cfg/crt.h"
  41. #include <compiler.h>
  42. #include <io.h>
  43. #include <time.h>
  44. #include <string.h>
  45. #include <sys/types.h>
  46. #include <stdint.h>
  47. /*!
  48. * \addtogroup xgCrtLowio
  49. */
  50. /*@{*/
  51. /*!
  52. * \brief waitqueue flags: wait for read / write / exception
  53. */
  54. #define WQ_FLAG_READ 0x01
  55. #define WQ_FLAG_WRITE 0x02
  56. #define WQ_FLAG_EXCEPT 0x04
  57. /*!
  58. * \brief waitqueue list structure type.
  59. */
  60. typedef struct _WQLIST WQLIST;
  61. /*!
  62. * \struct _WQLIST device.h sys/device.h
  63. * \brief File waitqueue list structure.
  64. */
  65. struct _WQLIST {
  66. WQLIST *next;
  67. HANDLE *wq;
  68. uint_fast8_t flags;
  69. };
  70. /*!
  71. * \brief Select commands, internally used.
  72. */
  73. typedef enum {
  74. SELECT_CMD_NOP = 0,
  75. SELECT_CMD_INIT,
  76. SELECT_CMD_CLEANUP,
  77. } select_cmd_t;
  78. /* FD_SET used for select */
  79. /* Define the maximum number of filedescriptors handled by select at the same
  80. time. This value is FOPEN_MAX rounded to the next higher power of two.
  81. */
  82. #define FD_SETSIZE ((FOPEN_MAX+7) & (~0x07))
  83. /* File descriptor set manipulation and test functions */
  84. #define FD_SET(n, p) ((p)->fd_bits[(n) >> 3] |= (1 << ((n) & 0x07)))
  85. #define FD_CLR(n, p) ((p)->fd_bits[(n) >> 3] &= ~(1 << ((n) & 0x07)))
  86. #define FD_ISSET(n,p) ((p)->fd_bits[(n) >> 3] & (1 << ((n) & 0x07)))
  87. #define FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
  88. typedef struct fd_set {
  89. uint8_t fd_bits [(FD_SETSIZE+7)/8];
  90. } fd_set;
  91. /*@}*/
  92. #ifndef CRT_DISABLE_SELECT_POLL
  93. extern void NutSelectWakeup(WQLIST *wq_list, uint_fast8_t flags);
  94. extern void NutSelectWakeupFromIrq(WQLIST *wq_list, uint_fast8_t flags);
  95. extern void NutSelectManageWq(WQLIST **wq_list, HANDLE *wq, int flags, select_cmd_t cmd);
  96. extern int select(int n, fd_set *rfds, fd_set *wfds, fd_set *exfds, struct timeval *timeout);
  97. #else
  98. #define NutSelectWakeup(wq_list, flags) {}
  99. #define NutSelectWakeupFromIrq(wq_list, flags) {}
  100. #define NutSelectManageWq(wq_list, wq, flags, cmd) {}
  101. #endif
  102. #endif